ansible使用技巧

ansible 的lineinfile模块使用-在某行前面添加一行、在某行后面添加一行、删除某一行、末尾加入一行、替换或添加某一行

1、文件内容修改,其中regexp为要修改的源内容的正则匹配,line为修改后的内容:ansible all -m lineinfile -a “dest=/root/test.txt regexp=’bbb’ line=’bbbbbbb'”
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”