转自

ansible 下lineinfile详细使用 - 散人 - 51CTO技术博客
http://zouqingyun.blog.51cto.com/782246/1882367

一、简述

这几天在看了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:
#Listen
#Port

2.2 insertbefore匹配内容在前面添加

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

验证

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

2.3 insertafter匹配内容在后面添加

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

验证

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

3.修改文件内容和权限

3.1 原文件内容及权限

[root@master test]# cat hosts
127.0.0.1 localhost.localdomain localhost :: localhost6.localdomain6 localhost6
192.168.1.2 foo.lab.net foo
[root@master test]# ls -l hosts
-rwxrwxr-x root qingyun 12月 : 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:
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-- root root 12月 : 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 line
lineinfile:
dest: /opt/playbook/test/hosts
line: '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 line
lineinfile:
dest: /opt/playbook/test/testfile
state: present
regexp: '^%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 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= changed= unreachable= failed= [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 file

PLAY [gitlab] ******************************************************************

TASK [test backrefs] ***********************************************************
ok: [master] => {"backup": "", "changed": false, "msg": ""} PLAY RECAP *********************************************************************
master : ok= changed= unreachable= failed=

文件保持不变

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= changed= unreachable= failed=
 

三、总结

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

本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1882367

ansible 下lineinfile详细使用 【转】的更多相关文章

  1. ansible 下lineinfile详细使用

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

  2. Linux下DedeCMS详细安全设置

    经常会听到使用dedecms的站长抱怨,网站又被挂马了,dedecms真的很不安全.dedecms可能存在某些漏洞这不假,但主要责任真的是dedecms吗?我们知道,一个黑客想上传木马,首先得可以找到 ...

  3. ansible模块lineinfile

    示列: sshd_set.yaml --- - hosts: test remote_user: root gather_facts: False tasks: - name: set hostnam ...

  4. mysql-5.7.14-winx64免安装版在win10下的详细配置过程

    1.配置文件 如果使用mysql的默认配置,在使用的过程中会出现很多问题,如汉字是乱码等. 在mysql的根目录(如:D:\mysql\mysql-5.7.14-winx64\)下,新建配置文件my. ...

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

    项目中要使用Uploadify 3.2来实现图片上传并生成缩略通的功能,特此记下来,以供各位参考! Uploadify下载地址:http://www.uploadify.com/download/ 下 ...

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

    一.oracle的安装,在官网下载oracle11g R2 1.在桌面单击右键,选择“在终端中打开”,进入终端 输入命令:su 输入ROOT密码: 创建用户组oinstall:groupadd oin ...

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

    转载请注明原文地址:https://www.cnblogs.com/litou/p/15004877.html 目录 一.介绍 二.编译准备 三.编译SQLite 四.编译LibTiff 五.编译PR ...

  8. KeyBord事件从Activtiy层往下分发详细过程代码示例

    step1:调用Activity成员函数dispatchKeyEvent public boolean dispatchKeyEvent(KeyEvent event) { // Let action ...

  9. linux/centos下安装nginx(rpm安装和源码安装)详细步骤

    Centos下安装nginx rpm包                                                                                 ...

随机推荐

  1. FZU2121_神庙逃亡

    水题.直接解二次方程判断点的高度即可. #include <iostream> #include <cstring> #include <cstdio> #incl ...

  2. luogu 1360 阵容均衡(前缀和+差分+hash)

    要求一段最大的区间里每个能力的增长值是一样的. 我们首先求一遍前缀和,发现,如果区间内[l,r]每个能力的增长值是一样的话,那么前缀和[r]和[l-1]的差分也应该是一样的. 那么我们把前缀和的差分h ...

  3. Android四大组件之contentProvider

    Activity,Service,broadcast and Contentprovider android 4 大组件. ContentProvider:使用: public class Image ...

  4. 【HLSDK系列】怎么增加一种新实体

    你平常肯定接触到很多比如 info_player_start hostage info_target 之类的实体,这里就解释一下怎么创建一种新的实体. 首先建立一个新的 .h 文件(当然你写在现有的文 ...

  5. 【JavaScript】获取项目路径地址

    在jsp页面顶上面定义 <% String path = request.getContextPath(); String basePath = request.getScheme() + &q ...

  6. Find the hotel HDU - 3193(RMQ)

    题意: 有n个旅馆,从这n个旅馆中找出若干个旅馆,使得这若干个旅馆满足这样的条件:不能从其它和剩下的旅馆中找到一个价格和距离都小于这个旅馆的旅馆... 解析: 按price 排序,若price相同, ...

  7. 【比赛】HNOI2018 转盘

    通过这题,我发现了我最大的缺陷,就是题目中重要的性质发现不了,所以导致后期根本做不了.还是要多做题,培养思维 对于这道题,来发现性质吧 对于每一条路线,因为它有用的就是最终的时刻,所以我们都可以把它变 ...

  8. NLP度量指标BELU真的完美么?

    摘要: NLP重要评价准则之一——BLEU,真的完美无缺么? 刚接触自然语言处理的朋友通常会问我:当系统的输出是文本,而非对输入文本进行某种分类,如何对该系统进行评估.当模型的输入是文本信息,输出也是 ...

  9. 洛谷 P3924 康娜的线段树 解题报告

    P3924 康娜的线段树 题目描述 小林是个程序媛,不可避免地康娜对这种人类的"魔法"产生了浓厚的兴趣,于是小林开始教她\(OI\). 今天康娜学习了一种叫做线段树的神奇魔法,这种 ...

  10. 【bzoj2759】一个动态树好题

    Portal -->bzoj2759 Solution 哇我感觉这题真的qwq是很好的一题呀qwq 很神qwq反正我真的是自己想怎么想都想不到就是了qwq 首先先考虑一下简化版的问题应该怎么解决 ...