示列:

sshd_set.yaml

---
- hosts: test
remote_user: root
gather_facts: False
tasks:
- name: set hostname
lineinfile: dest=/etc/sysconfig/network backrefs=yes regexp='^(HOSTNAME=).*' line='\1{{ inventory_hostname }}'
notify: reboot
handlers:
- name: reboot
shell: /sbin/reboot

iptables_add.yml

---
- hosts: test
gather_facts: false
tasks:
- name: iptables test
lineinfile: dest=/etc/sysconfig/iptables insertafter='^-A INPUT -i lo' line='-A INPUT -s 192.168.0.8 -m state --state NEW -m tcp -p tcp --dport 10050 -j ACCEPT'
notify: reload iptables
handlers:
- name: reload iptables
service: name=iptables state=reloaded

iptables_add.sh(iptables_add.yml的shell版)

#!/bin/bash

line="\-A INPUT \-s 192.168.0.8 \-m state \-\-state NEW \-m tcp \-p tcp \-\-dport 10050 \-j ACCEPT"
iptables_conf="/etc/sysconfig/iptables" grep -s "$line" $iptables_conf
if [ $? != ];then
sed -i "/^-A INPUT -i lo/a $line" $iptables_conf
/etc/init.d/iptables reload
fi

说明:

ansible-doc lineinfile

替换 移除文件的单行  # 多行替换 移除参考replace模块

Options: (= is mandatory)(= 后面的参数是强制要有的)

- backrefs(default=no)

  与state=present一起使用

  一、支持反向引用

  二、稍微改变了模块的操作方式

  1、前插'insertbefore',后插'insertafter'失效

  2、如果匹配到 替换最后一个匹配结果

  3、如果未匹配到 不做任何改变

= dest

  被编辑的文件

- insertafter

  需要声明 state=present

  在匹配行后插入,如果未匹配到则默认为EOF。ps:如果line存在则不会再插入,不管regexp有没有匹配到

- insertbefore

  需要声明 state=present

  在匹配行前插入,如果未匹配到则默认为BOF。ps:如果line存在则不会再插入,不管regexp有没有匹配到

- line

  需要声明 state=present

  插入或替换的字符串

- regexp

  使用正则匹配

- state(default=present)

  present 如果匹配到就替换(最后一个匹配结果) #如果未设置backrefs=yes 未匹配到也会在最后插入line

  absent 移除匹配行(所有匹配到的)

ansible模块lineinfile的更多相关文章

  1. ansible 下lineinfile详细使用

    ansible 下lineinfile详细使用 时间 2016-12-13 18:02:31  51CTO推荐博文 原文  http://zouqingyun.blog.51cto.com/78224 ...

  2. ansible 下lineinfile详细使用 【转】

    转自 ansible 下lineinfile详细使用 - 散人 - 51CTO技术博客http://zouqingyun.blog.51cto.com/782246/1882367 一.简述 这几天在 ...

  3. ansible模块

    ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...

  4. ansible笔记(3):ansible模块的基本使用

    ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...

  5. 第4天:Ansible模块

    Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...

  6. ansible模块command、shell、raw、script

    简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...

  7. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  8. ansible 模块 分享

    A a10_server 管理A10 Networks AX / SoftAX / Thunder / vThunder设备 a10_service_group 管理A10网络设备的服务组 a10_v ...

  9. ansible模块文件操作

    Ansible常用模块文件操作 [root@tiandong etc]# ansible-doc -l   列出ansible所支持的模块 [root@tiandong ~]# ansible-doc ...

随机推荐

  1. 【转】aspx与aspx.cs的关系

    原文地址: http://www.cnblogs.com/axzxs2001/archive/2009/01/19/1378383.html 在vs中,有很多朋友问起,在一个网站项目中的aspx和as ...

  2. Python3.1-标准库之Numpy

    这系列用来介绍Python的标准库的支持Numpy部分.资料来自http://wiki.scipy.org/Tentative_NumPy_Tutorial,页面有许多链接,这里是直接翻译,所以会无法 ...

  3. opencv6.4-imgproc图像处理模块之直方图与模板

    接opencv6.3-imgproc图像处理模块之边缘检测 九.直方图的相关操作 直方图是图像中像素强度分布的图形表达方式:它统计了每一个强度值所具有的像素个数 上图是一个灰色图像,通过对图像的每个不 ...

  4. TM4C123G红外触摸屏:开发板好不容易实现了原理,放到专家设计的板子上无法运行,于是专家跑路项目黄了

    使用TI的TM4C123G LaunchPad开发板,USB接口,来对同样的芯片进行烧写. 我们只用烧写那一块功能,不用另外一个芯片的开发功能,需要跳线   源码项目:   从官方网站TM4C123G ...

  5. Android开发环境部署

    引言   在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...

  6. 重叠(Overlapping) NAT

    当内部网络也使用公网注册地址(或者是外网合法地址)时,如果仍使用标准的静态或者动态NAT转换,则可能使得转换的内网地址与外网中合法地址冲突,使数据包又返回到了本地网络,这肯定是不行的.这时我们就要使用 ...

  7. 前端框架——AmazeUI学习

    AmazeUI官网: http://amazeui.org/ 前后台模板下载:链接:链接:http://pan.baidu.com/s/1c2uVfk0 密码:zuva 十大前端框架参考链接:http ...

  8. 取消Git代理设置

    昨天由于在用sourceTree上传下拉代码的时候,速度实在太慢,就照着百度上的方法设置了代理,结果导致sourceTree无法访问服务器,经检查排除发现可能是因为公司网络不能使用代理,被防火墙挡住了 ...

  9. PHP-- 获取http请求头信息

    看官方文档: http://php.net/manual/zh/function.apache-request-headers.php http://php.net/manual/zh/functio ...

  10. oracle数据匹配merge into

    来源于:http://blog.csdn.net/vtopqx/article/details/50633865 前言: 很久之前,估计在2010年左右在使用Oralce,当时有个需求就是需要对两个表 ...