lineinfile模块的用法说明

命令参数说明

参数 含义
path 指定要操作的文件对象
regexp 匹配条件
insertbefore 在某行之前插入
insertafter 在某行之后插入
line 要写入文件的内容

1. 使用lineinfile模块在配置文件末行插入内容

[root@localhost ~]# ansible webservers -m lineinfile -a 'path=/root/nginx.conf insertafter="EOF" line="#nginx configuration file"'

2. 在文件首行插入内容

[root@localhost ~]# ansible webservers -m lineinfile -a 'path=/root/nginx.conf insertbefore="BOF" line="#this is configuration file for nginx" state=present'

3. 在指定行前/后插入内容

insertafter和insertbefore用法相同  只举一例  
需要注意的是  它们都要和state搭配使用 否则会执行失效

此操作的意思是 在配置文件#        root         /usr/share/nginx/html;行下插入一行内容#        root         /usr/local/nginx/html
[root@localhost ~]# ansible webservers -m lineinfile -a 'path=/root/nginx.conf insertafter="#        root         /usr/share/nginx/html;" line="#        root         /usr/local/nginx/html;" state=present'

4. 删除文件某行内容

[root@localhost ~]# ansible webservers -m lineinfile -a 'path=/root/nginx.conf regexp="#        root         /usr/share/nginx/html;" state=absent'
发布了67 篇原创文章 · 获赞 56 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43557605/article/details/102659689