ansible 相關命令備忘

常用的ansible命令,備忘。


一、修改/etc/ansible/hosts文件,將需要操作的主機添加進去:


vi /etc/ansible/hosts

[ywbhost]

192.16.1.[29:52] ansible_ssh_uer="root" ansible_ssh_pass="xxxxxx"


二、執行shell命令:

ansible ywbhost -m shell -a "/tmp/setdns.sh"


三、複製文件

複製文件到遠端主機copy:

ansible ywbhost -m copy -a "src=/etc/ansible/ywb/setdns.sh dest=/tmp/setdns.sh mode=755"

從遠端主機拷貝文件到本機,fetch:

ansible ywbhost -m fetch -a "src=/tmp/setdns.sh dest=/tmp/setdns.sh mode=755"


四、文本操作:

1、在文末添加一行

ansible ywbhost -m lineinfile -a "dest=/etc/chrony.conf line='server 192.168.1.33 iburst'"

2、替換一行

ansible all -m lineinfile -a "dest=/root/test.txt regexp='bbb' line='bbbbbbb'"

3、在某一行前面插入一行:

ansible all -m lineinfile -a "dest=/root/test.txt insertbefore='aa(.*)' line='eeee'"

4、在某一行後面插入一行,insertafter

ansible all -m lineinfile -a "dest=/root/test.txt insertafter='aa(.*)' line='eeee'"

5、刪除某一行:

ansible all -m lineinfile -a "dest=/root/test.txt regexp='aa(.*)' state=absent"

6、替換或添加某一行:ansible all -m lineinfile -a "dest=/root/test.txt regexp='he(.*)' line='lllll' state=present"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章