ansible模块lineinfile
示列:
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的更多相关文章
- ansible 下lineinfile详细使用
ansible 下lineinfile详细使用 时间 2016-12-13 18:02:31 51CTO推荐博文 原文 http://zouqingyun.blog.51cto.com/78224 ...
- ansible 下lineinfile详细使用 【转】
转自 ansible 下lineinfile详细使用 - 散人 - 51CTO技术博客http://zouqingyun.blog.51cto.com/782246/1882367 一.简述 这几天在 ...
- ansible模块
ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...
- ansible笔记(3):ansible模块的基本使用
ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...
- 第4天:Ansible模块
Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...
- ansible模块command、shell、raw、script
简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...
- win10的pycharm中安装ansible模块过程
前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...
- ansible 模块 分享
A a10_server 管理A10 Networks AX / SoftAX / Thunder / vThunder设备 a10_service_group 管理A10网络设备的服务组 a10_v ...
- ansible模块文件操作
Ansible常用模块文件操作 [root@tiandong etc]# ansible-doc -l 列出ansible所支持的模块 [root@tiandong ~]# ansible-doc ...
随机推荐
- swift导航栏导航按钮添加多个按钮事件
//导航左边返回按钮 let button1 = UIButton(frame:CGRectMake(0, 0, 18, 18)) button1.setImage(Constant.Image.Na ...
- Qt5 新特性
Qt 5 已经临近发布,其最大的特点就是模块化.将原来庞大的模块更细分为不同的部分,同时,一个大版本的升级,当然少不了添加.删除各个功能类.文本简单介绍 Qt5 的特性,其具体内容来自 Qt5 官方 ...
- WPF 绑定枚举值
前台Xaml <ComboBox x:Name=" HorizontalAlignment="Left" Margin="5 0 0 0" Se ...
- Expression Blend4经验分享:制作一个简单的图片按钮样式
这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...
- 开源Asp.Net Core小型社区系统
源码地址:Github 前言 盼星星盼月亮,Asp.Net Core终于发布啦!! Asp.Net发布时我还在上初中,没有赶上.但是Asp.Net Core我从beta版本便一直关注.最初项目名叫As ...
- 基于DDD的.NET开发框架 - ABP的Entity设计思想
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- JS导出Excel 代码笔记
var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '< ...
- ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看另一种验证的方式:digest authentication,即摘要认 ...
- mutex与semaphore的区别
网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...
- Windows系统防火墙用法
1.按下“Win+X”组合键呼出系统快捷菜单,点击打开“控制面板”: 2.将“查看方式”修改为[大图标],然后点击“Windows 防火墙”: 3.在防火墙窗口左侧点击“高级设置”:(若防火墙未开启, ...