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

转载自:【Ansible学习】- 常用文件操作模块之lineinfile模块

简介

  • 该模块用于确保一个特定的行在一个文件中,或者使用一个正则表达式替换一个现有的行。
  • 如果想要改变文件中相似的多行,可以使用replace模块。如果想要插入/更新/删除一个行块,可以使用blockinfile模块。

模块参数

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

示例

文本替换

将/etc/selinux/config文件中所有匹配^SELINUX=正则表达式的行中的最后一行使用SELINUX=disabled替换;
如果regexp不匹配文件中的任何一行,则将line所指定的行插入到文件的末尾。

[root@centos7 templates]# ansible test -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}

# 查看结果
[root@centos7 templates]# ansible test -m command -a "cat /etc/selinux/config"
172.20.21.121 | 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 three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

删除行

将/tmp/test.sh文件中所有匹配^pwd的行删除

[root@centos7 templates]# ansible test -m lineinfile -a "path=/tmp/test.sh regexp='^pwd' state=absent"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "found": 3, 
    "msg": "3 line(s) removed"
}

# 再次运行,没有匹配行
[root@centos7 templates]# ansible test -m lineinfile -a "path=/tmp/test.sh regexp='^pwd' state=absent"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": false, 
    "found": 0, 
    "msg": ""
}

在这里插入图片描述

替换行并设置文件权限

[root@centos7 ~]# ansible test -m lineinfile -a "path=/etc/hosts regexp='^127.0.0.1' line='127.0.0.1 localhost' owner=root group=root mode=0644"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}

在这里插入图片描述

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不存在则会插入),而不通过正则表达式进行匹配。

[root@centos7 ~]# ansible test -m lineinfile -a "path=/root/test.sh line='liuhao test'"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": true, 
    "msg": "line added"
}

# 再次执行
[root@centos7 ~]# ansible test -m lineinfile -a "path=/root/test.sh line='liuhao test'"
172.20.21.121 | SUCCESS => {
    "backup": "", 
    "changed": false, 
    "msg": ""
}

backrefs用法

  • 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": ""
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值