【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+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 | 复制前是否检验需要复制目的地的路径 |
示例
文本替换
将/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" } |
当文件中没有匹配正则表达式^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": "" }
ansible middleware -m lineinfile -a "path=/etc/ssh/ssh_config regexp='# StrictHostKeyChecking ask' line='StrictHostKeyChecking no'" |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2018-12-06 用dockerfile构建基于centos系统的jar包的镜像
2017-12-06 CentOS 7 安装mysql