ansible 相關命令備忘

經常使用的ansible命令,備忘。shell


1、修改/etc/ansible/hosts文件,將須要操做的主機添加進去:app


vi /etc/ansible/hostsssh

[ywbhost]ide

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


2、執行shell命令:spa

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


3、複製文件server

複製文件到遠端主機copy:dns

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

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

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


4、文本操做:

一、在文末添加一行

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

二、替換一行

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

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

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

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

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

五、刪除某一行:

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

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

相關文章
相關標籤/搜索