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