示列:

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. Webwork 学习之路【05】请求跳转前 xwork.xml 的读取

    个人理解 WebWork 与 Struts2 都是将xml配置文件作为 Controler 跳转的基本依据,WebWork 跳转 Action 前 xml 文件的读取依赖 xwork-1.0.jar, ...

  2. Redis百亿级Key存储方案

    1 需求背景 该应用场景为DMP缓存存储需求,DMP需要管理非常多的第三方id数据,其中包括各媒体cookie与自身cookie(以下统称supperid)的mapping关系,还包括了supperi ...

  3. bloom filter

    Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员. 结    构 二进制 召回率 ...

  4. Nginx的配置文件

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  5. Android开发之补间动画、XML方式定义补间动画

    四种补间动画: 1.透明: 2.缩放: 3.位移: 4.旋转: //点击按钮 实现iv 透明的效果 动画 public void click1(View v) { //1.0意味着着完全不透明 0.0 ...

  6. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...

  7. OAuth in One Picture

    近年来,OAuth在各种开放平台的引领下变得非常流行,上图是OAuth协议认证的全过程,图本身已经比较详细,这里不再赘述. 从上图中可以看出,OAuth协议中有三个角色: User, Consumer ...

  8. 如何在移动端app中应用字体图标icon fonts (转)

    原文: http://www.cnblogs.com/willian/p/4166757.html?utm_source=tuicool&utm_medium=referral How to ...

  9. jquery 双击修改某项值

    双击修改某项值 $(function() { $('td.breakword').dblclick(function(){ $(this).addClass('input').html('<in ...

  10. jquery的ajax提交form表单

    $.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid').serialize(),// ...