ansible 下lineinfile详细使用

时间 2016-12-13 18:02:31  51CTO推荐博文
原文  http://zouqingyun.blog.51cto.com/782246/1882367
主题 Ansible SELinux 正则表达式

一、简述

这几天在看了ansible官网,收获蛮多。截取一个lineinfile模块作一个总结。如果批量修改配置文件某一行时,在写playbook时lineinfile避免不了的。

根据官网说法:lineinfile - Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression.大意是说,针对文件特殊行,使用后端引用的正则表达式来替换

二、实践

playbook,我先定义前面common部分。

---
 - hosts: "{{host}}"
   remote_user: "{{user}}"  gather_facts: false  tasks:

由于我已经定义标签tags,执行playbook中某个特定任务时,只需执行到对应TAGNAME便可

ansible-playbook line1.yml --extra-vars "host=gitlab user=root" --tags "TAGNAME" -v

1、正则匹配,更改某个关键参数值

   - name: seline modify enforcing
      lineinfile:
         dest: /etc/selinux/config
         regexp: '^SELINUX='
 line: 'SELINUX=enforcing'

验证

[root@master test]# cat /etc/selinux/config# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted

2、在匹配的内容前或后增加一行

2.1 http.conf

[root@master test]# cat http.conf
#Listen 12.34.56.78:80 #Listen 80 #Port

2.2 insertbefore匹配内容在前面添加

    - name: httpd.conf modify 8080lineinfile:dest: /opt/playbook/test/http.confregexp: '^Listen' insertbefore: '^#Port' line: 'Listen 8080' tags: - http8080

验证

[root@master test]# cat http.conf
#Listen 12.34.56.78:80 #Listen 80 Listen 8080 #Port

2.3 insertafter匹配内容在后面添加

- name: httpd.conf modify 8080lineinfile:dest: /opt/playbook/test/http.confregexp: '^Listen' insertafter: '^#Port' line: 'Listen 8080' tags: - http8080

验证

[root@master test]# cat http.conf
#Listen 12.34.56.78:80 #Listen 80 #Port Listen 8080

3.修改文件内容和权限

3.1 原文件内容及权限

[root@master test]# cat hosts
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 192.168.1.2 foo.lab.net foo
[root@master test]# ls -l hosts
-rwxrwxr-x 1 root qingyun 111 12月 13 18:07 hosts

3.2 剧本

    - name: modify hosts
      lineinfile:
         dest: /opt/playbook/test/hosts
         regexp: '^127\.0\.0\.1'
 line: '127.0.0.1 localhosts'  owner: root  group: root  mode: 0644  tags:  - hosts

3.3 执行验证

[root@master test]# cat hosts
127.0.0.1 localhosts 192.168.1.2 foo.lab.net foo [root@master test]# ls -l hosts -rw-r--r-- 1 root root 49 12月 13 18:16 hosts

4、删除某一行内容

4.1 原文件

[root@master test]# cat hosts
127.0.0.1 localhosts 192.168.1.2 foo.lab.net foo

4.2 absent剧本

- name: delete 192.168.1.1
      lineinfile:
 dest: /opt/playbook/test/hosts  state: absent  regexp: '^192\.'  tags:  - delete192

4.3 验证

[root@master test]# cat hosts

127.0.0.1 localhosts

5、文件存在就添加一行

5.1原文件

[root@master test]# cat hosts
127.0.0.1 localhosts

5.2 剧本

    - name: add a linelineinfile:dest:  /opt/playbook/test/hostsline: '192.168.1.2 foo.lab.net foo' tags: - add_a_line

5.3 验证

[root@master test]# cat hosts
127.0.0.1 localhosts 192.168.1.2 foo.lab.net foo

6、如果匹配到,引用line这一行作为替换。如果没有匹配到,则完全引用line这一行作为添加

6.1 原文件

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL

6.2 剧本

    - name: Fully quoted a linelineinfile:dest: /opt/playbook/test/testfilestate: presentregexp: '^%wheel'line: '%wheel ALL=(ALL) NOPASSWD: ALL' tags: - testfile

6.3 验证

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL

6.4 原文件

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL
%wheel  1234  ALL =(all) NOPASSWD

6.5 验证

Using /etc/ansible/ansible.cfg as config filePLAY [gitlab] ****************************************************************** TASK [Fully quoted a line] ***************************************************** changed: [master] => {"backup": "", "changed": true, "msg": "line replaced"} PLAY RECAP ********************************************************************* master : ok=1 changed=1 unreachable=0 failed=0 [root@master test]# cat testfile # %wheel ALL=(ALL) ALL %wheel ALL=(ALL) NOPASSWD: ALL

7、关于参数backrefs,backup使用。

  • backrefs为no时,如果没有匹配,则添加一行line。如果匹配了,则把匹配内容替被换为line内容。

  • backrefs为yes时,如果没有匹配,则文件保持不变。如果匹配了,把匹配内容替被换为line内容。

  • backup为no时,没有匹配,则添加。如果匹配了,则替换

  • backup为yes时,没有匹配,添加,如果匹配了,则替换

7.1 需要关心的,backrefs为yes时情景

7.1.1 原文件

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL
#?bar

7.1.2 剧本

    - name: test backrefs
      lineinfile:
#          backup: yes
          state: present
          dest: /opt/playbook/test/testfile
 regexp: '^#\?bar'  backrefs: yes  line: 'bar'  tags:  - test_backrefs

7.1.3 验证

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL
bar

7.1.3 没有匹配

[root@master test]# cat testfile
# %wheel    ALL=(ALL)  ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL

7.1.4 验证

Using /etc/ansible/ansible.cfg as config filePLAY [gitlab] ****************************************************************** TASK [test backrefs] *********************************************************** ok: [master] => {"backup": "", "changed": false, "msg": ""} PLAY RECAP ********************************************************************* master : ok=1 changed=0 unreachable=0 failed=0

文件保持不变

8、使用valiate参数,在保存sudoers文件前,验证语法,如果有错,执行时,会报出来,重新编辑playbook

8.1 剧本

- name: test validate
      lineinfile:
          dest: /etc/sudoers
          state: present
          regexp: '^%ADMIN ALL='  line: '%ADMIN ALL=(ALL)'  validate: 'visudo -cf %s'  tags:  - testsudo

8.2 执行验证就说语法不过关

Using /etc/ansible/ansible.cfg as config filePLAY [gitlab] ****************************************************************** TASK [test validate] *********************************************************** fatal: [master]: FAILED! => {"changed": false, "failed": true, "msg": "failed to validate: rc:1 error:visudo:>>> /tmp/tmpgQjHYM:syntax error 在行 114 附近<<<\n"} to retry, use: --limit @/opt/playbook/test/line1.retry PLAY RECAP ********************************************************************* master : ok=0 changed=0 unreachable=0 failed=1

三、总结

具体模块使用,ansible-doc可以查看详细用法。

转载于:https://www.cnblogs.com/lize3379/p/7025770.html

ansible 下lineinfile详细使用相关推荐

  1. python windows系统_Windows系统下Python-Windows详细安装教程

    安装Python-Windows 在开始Python编程前,需要先安装Python环境.Python安装包可以到Python的官网下载,官网地址是,如果想直接跳过关于Python的介绍相关直接下载安装 ...

  2. k8s简介以及linux环境下的详细安装步骤

    k8s简介以及linux环境下的详细安装步骤 k8s是Kubernetes的简称,Kubernetes中间有8个单词,所以叫k8s,就是这么简单粗暴. 我们可以看到docker的图标是鲨鱼,k8s的图 ...

  3. linux 安装cvs,linux下cvs详细安装和配置.docx

    Linux 下cvs详细安装和配置 2009-03-18 14:37:12 标签:[推送到技术圈] 版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始岀处 作者信息和本声明.否则将追究 ...

  4. 【C#】C#中使用GDAL3(一):Windows下超详细编译C#版GDAL3.3.0(VS2015+.NET 4+32位/64位)

    转载请注明原文地址:https://www.cnblogs.com/litou/p/15004877.html 总目录 (一)Windows下超详细编译C#版GDAL3.3.0(VS2015+.NET ...

  5. 【转】Ansible 模块之 lineinfile 详细介绍

    转载自:[Ansible学习]- 常用文件操作模块之lineinfile模块 目录 简介 模块参数 示例 文本替换 删除行 替换行并设置文件权限 insertafter和insertbefore 为文 ...

  6. LINUX 下open*** 详细配置

    LINUX 下open*** 配置 一 ×××基础讲解 1.1什么是××× IP机制仿真出一个私有的广域网"是通过私有的隧道技术在公共数据网络上仿真一条点到点的专线技术.所谓虚拟,是指用户不 ...

  7. MySQL数据库的设计和命令行模式下建立详细过程

    1.数据表的设计 MySQL数据库管理系统(DBMS)中,包含的MySQL中定义数据字段的类型对你数据库的优化是非常重要的.MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类 ...

  8. Android app包下fragment详细使用

    文章目录 前言 一.什么是Fragment? 二.Fragment与Activity的区别与优势 1.生命周期不同 2.Fragment的使用优势 三.Fragment的生命周期 四.Fragment ...

  9. 【渗透实战】web渗透实战,相对高安全级别下,详细分析整个渗透过程以及介绍社工的巧妙性,拿一站得数十站,(漏洞已交)

    ''' 版权tanee 转发交流请备注 漏洞已经提交管理员 关键过程的截图和脚本代码已经略去.希望大家学习技术和思路就好,切勿进行违法犯罪的活动.本实战案例仅作为技术分享,切勿在未经许可的任何公网站点 ...

最新文章

  1. 这封以数字构写的蓝图,正在实现笔尖所触即世界
  2. anaconda下配置R子环境并配置jupyter notebook的R Kernel
  3. 搭建nginx服务器及文件的配置
  4. metrics_FlexyPool如何支持Dropwizard Metrics包重命名
  5. [解读REST] 3.基于网络应用的架构
  6. 因此,您是一名新软件工程师。 让我们面对一些事实,揭穿一些神话。
  7. 清理offset_关于 kafka 日志清理策略的问题
  8. 关于SSH使用的一些经验
  9. 解决 vs2003 无法启动调试 没有正确安装调试器
  10. 【iOS】Web Color 的 Swift 实现
  11. loj #6122. 「网络流 24 题」航空路线问题
  12. html中的排名怎么写,html制作畅销书排行榜
  13. NCTF-Writeup
  14. android dlna 服务器,安卓手机DLNA功能使用方法
  15. P.J. Plauger
  16. 安卓psp模拟器联机教程_谁知道手机版的ppsspp模拟器怎么联机啊?
  17. mysql errno 1146_Mysql学习MySQL复制出错 Last_SQL_Errno:1146的解决方法
  18. DataSec数据防泄密系统
  19. ajax提交表单序列化不进请求,表单序列化+ajax跨域提交
  20. 趣味三角——第2章——弦

热门文章

  1. 《51单片机应用开发从入门到精通》——2.6 中断控制功能的作用
  2. SQL Server-表表达式基础回顾(二十四)
  3. CCNP学习笔记2-路由部分--EIGRP
  4. zabbix的安装监控windows,linux操作流程
  5. laravel大型项目系列教程(四)之显示文章列表和用户修改文章
  6. 天池实验室|读取数据集的两种方式
  7. 解决append的div的事件失效问题
  8. 计算机组成原理国防科大课件,中科大计算机组成原理课件ppt.pdf
  9. MySQL+号的作用
  10. 为什么需要ORM 框架