ansible all -m ping #檢測主機(jī)連接
-m:指定模塊
copy file cron group user yum service script ping command raw get_url synchronize
默認(rèn)模塊command
ansible all -a "free -m" #執(zhí)行遠(yuǎn)程命令
等同于ansible '*' -a "free -m"
-a:模塊參數(shù)
*和all,表示定義在/etc/ansible/hosts的所有主機(jī)
ansible '*' -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root" # 指定節(jié)點(diǎn)上的權(quán)限,屬主和數(shù)組為root
ansible '*' -m file -a "dest=/tmp/test state=directory" # 創(chuàng)建文件夾
ansible '*' -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"' #指定節(jié)點(diǎn)上定義一個(gè)計(jì)劃任務(wù),每隔3分鐘到主控端更新一次時(shí)間
ansible all -m group -a 'gid=2017 name=a' # 指定節(jié)點(diǎn)上創(chuàng)建一個(gè)組名為aaa,gid為2017的組
ansible all -m user -a 'name=aaa groups=aaa state=present' # 在節(jié)點(diǎn)上創(chuàng)建一個(gè)用戶aaa,組為aaa
ansible all -m user -a 'name=aaa groups=aaa remove=yes' #刪除用戶示例
ansible all -m yum -a "state=present name=httpd" # 在節(jié)點(diǎn)上安裝httpd
ansible all -m service -a 'name=httpd state=started enabled=yes' # 在節(jié)點(diǎn)上啟動(dòng)服務(wù),并開機(jī)自啟動(dòng)
ansible '*' -m script -a '/root/test.sh' # 執(zhí)行主控端腳本
ansible '*' -m shell -a 'ps aux|grep zabbix' # 執(zhí)行遠(yuǎn)程主機(jī)的腳本
ansible '*' -m raw -a "ps aux|grep zabbix|awk '{print \$2}'" # 類似shell
ansible '*' -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link" # 創(chuàng)建軟鏈接
ansible '*' -m file -a "path=/tmp/resolv.conf state=absent" # 刪除軟鏈接
ansible '*' -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644" # 復(fù)制文件到遠(yuǎn)程服務(wù)器
ansible all -m raw -a 'hostname|tee' # 在節(jié)點(diǎn)上運(yùn)行hostname
ansible all -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp' # 將指定url上的文件下載到/tmp下
ynchronize模塊:
目的:將主控方/root/a目錄推送到指定節(jié)點(diǎn)的/tmp目錄下
命令:ansible all -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
執(zhí)行效果:
delete=yes 使兩邊的內(nèi)容一樣(即以推送方為主)
compress=yes 開啟壓縮,默認(rèn)為開啟
--exclude=.git 忽略同步.git結(jié)尾的文件
由于模塊,默認(rèn)都是推送push。因此,如果你在使用拉取pull功能的時(shí)候,可以參考如下來實(shí)現(xiàn)mode=pull 更改推送模式為拉取模式
目的:將10.1.1.113節(jié)點(diǎn)的/tmp/a目錄拉取到主控節(jié)點(diǎn)的/root目錄下
命令:ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
dest_port=22 # 指定目的主機(jī)的ssh端口,ansible配置文件中的 ansible_ssh_port 變量優(yōu)先級(jí)高于該 dest_port 變量
rsync_path # 指定 rsync 命令來在遠(yuǎn)程服務(wù)器上運(yùn)行。這個(gè)參考rsync命令的--rsync-path參數(shù),--rsync-path=PATH # 指定遠(yuǎn)程服務(wù)器上的rsync命令所在路徑信息
rsync_timeout # 指定 rsync 操作的 IP 超時(shí)時(shí)間,和rsync命令的 --timeout 參數(shù)效果一樣


