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"

相关文章
相关标签/搜索