lineinfile:檔案内容修改、在某行前面添加一行、在某行後面添加一行、删除某一行、末尾加入一行、替換或添加某一行
1、檔案内容修改,其中regexp為要修改的源内容的正則比對,line為修改後的内容:ansible all -m lineinfile -a "dest=/root/test.txt regexp='bbb' line='bbbbbbb'"
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuUDZjdjZ5EGZxUjNmhzYiljNmVWOyYjYiBzMiFjY3kjNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
image.png
2、在某一行前面插入一行:ansible all -m lineinfile -a "dest=/root/test.txt insertbefore='aa(.*)' line='eeee'"
3、在某一行後面插入一行,insertafter
4、删除某一行:ansible all -m lineinfile -a "dest=/root/test.txt regexp='aa(.*)' state=absent"
5、末尾加入一行: ansible all -m lineinfile -a "dest=/root/test.txt line='hehe'"
6、替換或添加某一行:ansible all -m lineinfile -a "dest=/root/test.txt regexp='he(.*)' line='lllll' state=present"
7、也可以添加參數owner,group,mode