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

简介

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

模块参数

名称 必选 默认值 可选值 备注
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+rwxu=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 复制前是否检验需要复制目的地的路径

示例

文本替换

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[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的行删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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": ""
}

删除匹配行

替换行并设置文件权限

1
2
3
4
5
6
[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所匹配的最后一行的后面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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不存在则会插入),而不通过正则表达式进行匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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内容。
1
2
3
4
5
6
7
8
9
10
11
12
[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": ""
}
hoxis wechat
一个脱离了高级趣味的程序员,关注回复1024有惊喜~
赞赏一杯咖啡
0%