標籤:ansible 自動化工具 ansible-playbook
例子:
1、ping模組所有cce組主機存活率
[[email protected] ~]# ansible cce -m ping
2、使用command執行命令
[[email protected] ~]# ansible cce -m command -a ‘cat /etc/redhat-release‘
3、使用shell模組執行命令
[[email protected] ~]# ansible cce -m shell -a ‘cat /etc/redhat-release‘
4、使用copy模組複製檔案
[[email protected] ~]# ansible cce -m copy -a "src=/etc/hosts dest=/root/cce"
[[email protected] ~]# ansible cce -m copy -a "src=/etc/hosts dest=/root/cce owner=nobody"
[[email protected] ~]# ansible cce -m copy -a "src=/etc/hosts dest=/root/cce owner=nobody mode=755"
改變之前做備份
[[email protected] ~]# ansible cce -m copy -a "src=/etc/hosts dest=/root/cce owner=nobody mode=755 backup=yes"
5、cron模組任務計劃
[[email protected] ~]# ansible cce -m cron -a ‘month="*/5" job="/usr/sbin/ntpdate 172.16.0.1 &>/dev/null" name="timesync"‘
查看
[[email protected] ~]# ansible cce -m shell -a ‘crontab -l‘
172.16.253.131 | success | rc=0 >>
#Ansible: timesync
* * * */5 * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null
172.16.252.193 | success | rc=0 >>
#Ansible: timesync
* * * */5 * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null
刪除crontab
[[email protected] ~]# ansible cce -m cron -a ‘name="timesync" state="absent"‘
6、使用fetch模組將遠程主機上的目錄拉取到本地
[[email protected] ~]# ansible cce -m fetch -a ‘src=/root/cce dest=/root/cce‘
7、使用file模組,對遠程主機上的檔案進行管理
[[email protected] ~]# ansible cce -m file -a ‘path=/root/cce mode=777‘
[[email protected] ~]# ansible test -m file -a ‘src=/etc/fstab dest=/root/fstablink state=link‘
8、yum模組安裝apache
[[email protected] ~]# ansible cce -m yum -a ‘name=httpd state=present‘
9、使用service模組啟動服務
[[email protected] ~]# ansible cce -m service -a ‘name=httpd state=restarted‘
10、使用ansible添加使用者
[[email protected] ~]# ansible cce -m user -a ‘name=cce home=/tmp/cce shell=/bin/bash uid=2000 comment="test user"‘
ansible-playbook:劇本基本使用
hosts:執行的主機
remote_user:遠程執行指令碼的使用者
tasks:任務
1、使用劇本一鍵部署httpd
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/8C/2C/wKioL1hkoUrB17AiAAAp9eWY1K8761.png-wh_500x0-wm_3-wmp_4-s_2731401758.png" title="12.png" alt="wKioL1hkoUrB17AiAAAp9eWY1K8761.png-wh_50" />
執行
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8C/2C/wKioL1hkoWzxAEtQAABagvXyWXU174.png-wh_500x0-wm_3-wmp_4-s_47665331.png" title="123.png" alt="wKioL1hkoWzxAEtQAABagvXyWXU174.png-wh_50" />
2、使用notify、handlers來進行控制(注意:這個只能用在劇本的最後,否則,其他的都不予執行)
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/8C/2C/wKioL1hkoY6w6BZeAAAtREi9NuU510.png-wh_500x0-wm_3-wmp_4-s_4152052912.png" title="1234.png" alt="wKioL1hkoY6w6BZeAAAtREi9NuU510.png-wh_50" />
執行結果
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/8C/2C/wKioL1hkoauw62G7AABFTBdKbAU245.png-wh_500x0-wm_3-wmp_4-s_3109313747.png" title="213.png" alt="wKioL1hkoauw62G7AABFTBdKbAU245.png-wh_50" />
3、使用tags來進行特殊任務的進行
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8C/30/wKiom1hkoc7gBZ6QAAAkZxGx-Xc501.png-wh_500x0-wm_3-wmp_4-s_1986173008.png" style="float:none;" title="22.png" alt="wKiom1hkoc7gBZ6QAAAkZxGx-Xc501.png-wh_50" />
執行
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8C/2C/wKioL1hkoc6zf1g5AABcCyvONyg628.png-wh_500x0-wm_3-wmp_4-s_889191696.png" style="float:none;" title="33.png" alt="wKioL1hkoc6zf1g5AABcCyvONyg628.png-wh_50" />
本文出自 “Char” 部落格,謝絕轉載!
Ansible-playbook基本使用(三)