自动化运维工具—Ansible常用模块Lineinfile

文件操作模块—lineinfile

用于确保特定行在一个文件中,或者使用正则表达式替换现有一个行

常用选项:

名称    必选    默认值    可选值    备注
backrefs    no    no    yes/no    如果打开这个标记,backrefs会改变模块的一些操作:insertbefore和insertafter参数会被忽略。当regexp不匹配文件中的任何行时,文件不会做任何修改,否则 使用扩展的line参数 替换 最后一个匹配正则表达式的行
backup    no    no    yes/no    用于创建一个包含时间戳信息的备份文件。以便在错误的修改了文件的时候,能够找回原始的文件
create    no    no    yes/no    与state=present一起使用。如果指定了这个参数,当要修改的文件不存在的时候,会创建它。否则会报错。
group    no            设置文件/目录的所属组
insertafter    no    EOF    EOF/*regex*    当regexp不匹配文件中的任何行的时候,会将新行插入到其所指定的正则表达式匹配的行中的最后一行的后面。insertafter也支持一个特殊的值:EOF(代表文件的末尾)。若没有匹配的行,那么就会插入EOF
insertbefore    no        BOF/*regex*    当regexp不匹配文件中的任何行的时候,会将line参数所指定的行,插入到insertbefore所指定的正则表达式匹配的行中的最后一行的前面,当insertbefore所指定的正则表达式不匹配任何行时,会插入到文件的末尾,同时insertbefore还可以是一个特殊的值:BOF(代表文件的开始);否则,会使用line参数所指定的行替换regexp所匹配的行中的最后一行。
line    no            要插入或者替换的行。如果设置了backrefs参数,那么line中可以包含位置分组或命名分组,lineinfile模块会使用regexp捕获的分组填充它们
mode    no            设置文件权限,模式实际上是八进制数字(如0644),少了前面的零可能会有意想不到的结果。从版本1.8开始,可以将模式指定为符号模式(例如u+rwx或u=rw,g=r,o=r)
others    no            file模块的其他参数可以在这里使用
owner    no            设置文件/目录的所属用户
path    yes            要修改的文件,也可以使用dest,destfile,name
regexp    no            用于搜索文件中的每一行的正则表达式。对于state=present,这个正则表达式所匹配的行中的最后一行会被替换;对于state=present,会删除所有匹配的行
state    no    present    present/absent    用于设置 新增或替换一行,还是删除行
unsafe_writes    no        yes/no    是否以不安全的方式进行,可能导致数据损坏
validate    no    None        复制前是否检验需要复制目的地的路径

示例:

文本替换:

将Selinux配置/etc/selinux/config文件中所有匹配^SELINUX= 正则表达式的行中的最后一行使用SELINUX=disabled替换;

如果regexp不匹配文件中的任何一行,则将line所指定的行插入到文件末尾

[root@ansible ~]# ansible node -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
192.168.30.39 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
192.168.30.38 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
[root@ansible ~]# ansible node -a 'cat /etc/selinux/config'
192.168.30.39 | SUCCESS | rc=0 >>

# 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=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

192.168.30.38 | SUCCESS | rc=0 >>

# 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=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

[root@ansible ~]#

删除行:

删除文件中所有匹配到的行

[root@ansible ~]# ansible node -m lineinfile -a 'path=/tmp/test.sh  regexp="^pwd"  state=absent'

插入行:insertafter  insertbefore

当文件中没有匹配正则表达式^Listen80的行时,会将Listen 80插入到^#Listen所匹配的最后一行的后面。

[root@centos7 ~]# ansible test -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen80' insertafter='^#Listen' line='Listen 80'"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line added"
}

# insertbefore的使用方法类似
[root@centos7 ~]# ansible test -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^#Listen80' insertbefore='^Listen 80' line='#Listen 80'"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line added"
}
直接在文件中新增一行(如果line不存在则会插入),而不通过正则表达式进行匹配。
 

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

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

[root@centos7 ~]# ansible test -m lineinfile -a "path=/root/test.sh line='liuhao test1' regexp='^liuhao' backrefs=yes"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
[root@centos7 ~]# ansible test -m lineinfile -a "path=/root/test.sh line='liuhao test1' regexp='^liuhao2' backrefs=yes"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": false, 
    "msg": ""
}

参考:<https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html>

posted @ 2018-08-30 17:57  Hito  阅读(529)  评论(0)    收藏  举报