一、ansible管理任務計劃
# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
name指定任務計劃的名字,job指定它的命令是什麼,後面指定它的分時日月周,不定義就是*。
[root@yw02 ~]# crontab -l #Ansible: test cron * * * * 6 /bin/touch /tmp/1212.txt
第一行一定不能更改,一旦改了之後,就不能用這個工具對它去做操作了。
若要刪除該cron 只需要加一個字段 state=absent
# ansible testhost -m cron -a "name='test cron' state=absent"
其他的時間表示:分鐘 minute ,小時 hour ,日期 day ,月份 month 。
二、ansible安裝rpm包和管理服務
# ansible testhost -m yum -a "name=httpd"
在name後面還可以加上state=installed/removed 安裝/卸載
啓動httpd的服務,用到的模塊教service:
# ansible testhost -m service -a "name=httpd state=started enabled=yes"
啓動服務後,就可以到機器上ps aux看到了。
這裏的name是centos系統裏的服務名,可以通過chkconfig --list查到。
Ansible文檔的使用
ansible-doc -l 列出所有的模塊
ansible-doc cron 查看指定模塊的文檔,相當於系統裏面的man配置。
三、使用ansible的playbook
playbook說白了就是把所有的配置搞到一個配置文件裏去,然後執行這個配置文件就行了。
相當於把模塊寫入到配置文件裏面,例:
# vi /etc/ansible/test.yml //加入如下內容 --- - hosts: yw02 remote_user: root tasks: - name: test_playbook shell: touch /tmp/lishiming.txt
說明: 文件格式,後綴名是yml;第一行需要有三個槓,是固定格式;hosts參數指定了對哪些主機進行操作,如果是多臺機器可以用逗號作爲分隔,也可以使用主機組,在/etc/ansible/hosts裏定義;
user參數指定了使用什麼用戶登錄遠程主機操作;
tasks指定了一個任務,其下面的name參數同樣是對任務的描述,在執行過程中會打印出來,shell是ansible模塊名字。
執行:ansible-playbook test.yml
PLAY [yw02] ************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************** ok: [yw02] TASK [test_playbook] ***************************************************************************************************************** [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [yw02] PLAY RECAP *************************************************************************************************************************** yw02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
四、playbook裏的變量
再來一個創建用戶的例子:
# vi /etc/ansible/create_user.yml //加入如下內容 --- - name: create_user hosts: yw02 user: root gather_facts: false vars: - user: "test" tasks: - name: create user user: name="{{ user }}"
說明:
name參數對該playbook實現的功能做一個概述,後面執行過程中,會打印name變量的值 ,可以省略;
gather_facts參數指定了在以下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在後面的task會使用到setup獲取的信息時用到,如果不寫這行就會默認收集facts,如果機器過多,建議就不收集了,影響執行效率,也會給ansible壓力。
vars參數,指定了變量,這裏指定一個user變量,其值爲test ,需要注意的是,變量值一定要用引號引住;
tasks下的user指定了調用user模塊,name是user模塊裏的一個參數,而增加的用戶名字調用了上面user變量的值,{{user}}=test。
[root@fuxi01 ansible]# ansible-playbook create_user.yml PLAY [create_user] ******************************************************************************************************************* TASK [create user] ******************************************************************************************************************* changed: [yw03] PLAY RECAP *************************************************************************************************************************** yw03 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如果這個test用戶已經存在了,就不會創建了,changed=0。