ansible 下lineinfile详细使用

ansible 下lineinfile详细使用

时间 2016-12-13 18:02:31  51CTO推荐博文

原文  http://zouqingyun.blog.51cto.com/782246/1882367

主题 AnsibleSELinux正则表达式

一、简述

这几天在看了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‘

验证

[[email protected] 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

[[email protected] test]# cat http.conf
#Listen 12.34.56.78:80
#Listen 80
#Port

2.2 insertbefore匹配内容在前面添加

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

验证

[[email protected] test]# cat http.conf
#Listen 12.34.56.78:80
#Listen 80
Listen 8080
#Port

2.3 insertafter匹配内容在后面添加

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

验证

[[email protected] test]# cat http.conf
#Listen 12.34.56.78:80
#Listen 80
#Port
Listen 8080

3.修改文件内容和权限

3.1 原文件内容及权限

[[email protected] test]# cat hosts
127.0.0.1		localhost.localdomain localhost ::1		localhost6.localdomain6 localhost6
192.168.1.2 foo.lab.net foo
[[email protected] 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 执行验证

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

4、删除某一行内容

4.1 原文件

[[email protected] 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 验证

[[email protected] test]# cat hosts

127.0.0.1 localhosts

5、文件存在就添加一行

5.1原文件

[[email protected] test]# cat hosts
127.0.0.1 localhosts

5.2 剧本

    - name: add a line
      lineinfile:
         dest:  /opt/playbook/test/hosts
         line: ‘192.168.1.2 foo.lab.net foo‘
      tags:
       - add_a_line

5.3 验证

[[email protected] test]# cat hosts
127.0.0.1 localhosts
192.168.1.2 foo.lab.net foo

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

6.1 原文件

[[email protected] test]# cat testfile
# %wheel	ALL=(ALL)	ALL

6.2 剧本

    - name: Fully quoted a line
      lineinfile:
         dest: /opt/playbook/test/testfile
         state: present
         regexp: ‘^%wheel‘
         line: ‘%wheel  ALL=(ALL)       NOPASSWD: ALL‘

      tags:
        - testfile

6.3 验证

[[email protected] test]# cat testfile
# %wheel	ALL=(ALL)	ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL

6.4 原文件

[[email protected] test]# cat testfile
# %wheel	ALL=(ALL)	ALL
%wheel  1234  ALL =(all) NOPASSWD

6.5 验证

Using /etc/ansible/ansible.cfg as config file

PLAY [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   

[[email protected] 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 原文件

[[email protected] 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 验证

[[email protected] test]# cat testfile
# %wheel	ALL=(ALL)	ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL
bar

7.1.3 没有匹配

[[email protected] test]# cat testfile
# %wheel	ALL=(ALL)	ALL
%wheel  ALL=(ALL)       NOPASSWD: ALL

7.1.4 验证

Using /etc/ansible/ansible.cfg as config file

PLAY [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 file

PLAY [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可以查看详细用法。

时间: 2024-08-05 16:37:53

ansible 下lineinfile详细使用的相关文章

DOSbox汇编集成环境下的详细设置

alt+enter可以全屏幕,如果觉得游戏运行速度不合适,可以修改 cycles=3000 ,将3000适当调整大小. 3.运行 DOSBox,会打开两个 DOS 窗口,我们只需在如下窗口中键入 mount c h:\pal 此命令的作用为将 h:\pal 挂载为 C 盘,h:\pal 为<仙剑奇侠传>所在目录. 4.键入 c: 进入 C 盘,键入 pal.exe 即可进入游戏,如下图: 5.已经可以正常运行游戏了. 6.终极大法:将游戏的可执行程序直接拖到 DOXbox 的快捷方式上面,就可

Linux下udev详细介绍

每次在搭建OracleRAC环境中,遇到问题最多的就是关于ASM磁盘的的问题,通过查看网上许多搭建RAC的文档,发现Oracle10g RAC大家普遍的修改/etc/udev/rules.d/60-raw.rules,而一些搭建Oracle11R2的RAC,大家都在配置的是/etc/udev/rules.d/99-oracle-asmdevices.rules这个文件,面对这样一种情况,我不是很明白,这两个文件到底有什么区别,这个问题困扰了我很久,直到今天遇到这样一个问题:使用udev管理asm

ansible模块-lineinfile

lineinfile模块详解 lineinfile模块类似linux工具中的sed工具,但是网上的文章一般都只有简单的实例,复杂点的例子都没有 下面是我根据实际操作总结出来的lineinfile模块的常见例子,分享给大家参考 目录 ansible-doc lineinfile官方文档(英文)--看不懂跳过直接看例子 1.需求:替换匹配的目标值... 2.需求:在匹配值之前增加行... 3.需求:在匹配值之后增加行... 4.需求:匹配到就替换行,未匹配到就新增行... ansible-doc l

将javaweb项目部署到linux下的详细分析

以下是对将javaweb项目部署到linux下的方法进行了详细的分析介绍,需要的朋友可以过来参考下一般都在windows下开发的,现在部署到linux下:1,将项目达成war包(用eclipse,项目右键–>Export–>选择war file)2,将tomcat(用winSCP当然你也可以用secureCRT,用securCRT需要建立sftp(即上传文件的目录),用put tomcat命令)考到ilunx对应的目录下3,然后将项目的war包放到tomcat的webapps目录下4,启动to

MySql_x64免安装版在win10下的详细配置过程(一)

这里我介绍mySql的下载安装和配置全部过程,供有必要的朋友们参考.因个人电脑是win10_Pro_Technical_Preview_x64,所以我这里演示MySql的也是64位版. 第一步下载:相信很多IT的朋友都喜欢到官方网站下载最原始的东西,第三方网站广告太多,往往找不到下载的页面,而且还有可能被植入了不干净的东西.这里我给出其官方下载地址:http://dev.mysql.com/downloads/file.php?id=457534,打开后显示如下: 提示注册登录等信息,感兴趣的朋

Centos7下Apache详细安装配置及证书申请SSL配置介绍

首先说到Centos大家都已经非常熟悉了,所以我们也就不多介绍关于Centos具体发展了,我们首先知道在Centos7之前版本命令和ReadHat的命令完全一样的,可Centos从6.0版本升级到Centos7版本之后,命令及功能上有了很大的变化,最明显的差别就是从安装的操作界面及操作命令上已经有很大变化了,更不用说功能上的变化了,比如centos6.x版本的iptalbes到centos7的firewall的变化,当然,变化了很多,我也就不多说了,今天呢,主要给大家介绍一下再Centos7下A

jQuery上传插件Uploadify 3.2在.NET下的详细例子

项目中要使用Uploadify 3.2来实现图片上传并生成缩略通的功能,特此记下来,以供各位参考! Uploadify下载地址:http://www.uploadify.com/download/ 下载下来解压后估计里面很多文件,其实有用的也就jquery.uploadify.min.js.uploadify.css.uploadify.swf和uploadify-cancel.png这四个文件.你还得下载jQuery库,我这里用的是jquery-1.7.2.min.js,另外和大多数JQ插件一

centos7下oracle11g详细的安装与建表操作

一.oracle的安装,在官网下载oracle11g R2 1.在桌面单击右键,选择"在终端中打开",进入终端 输入命令:su 输入ROOT密码: 创建用户组oinstall:groupadd oinstall 创建用户组dba:groupadd dba 创建oracle用户,并加入到oinstall和dba用户组:useradd -g oinstall -g dba -m oracle 设置用户oracle登录密码,需要确认一次,注意两次密码要一样(注意:此处的密码是oracle账户

1.ionic新版本2.0beta36 window下的详细安装步骤(比1正式版好用)

1.首先安装nodejs: https://nodejs.org/en/download/ 2.管理员运行命令符:npm install -g cnpm --registry=https://registry.npm.taobao.org (这个是国内镜像  解决安装慢的问题) 3.cnpm -v  查看是否安装成功 4.cnpm install -g [email protected] (刚刚的cnpm镜像用上了  不会像npm那样卡) 5.要想ionic创建目录默认使用国内镜像的话要配置一下