至關於把模塊寫入到配置文件裏面,例: vi /etc/ansible/test.yml //加入以下內容 --- - hosts: cdn002 remote_user: root tasks: - name: test_playbook shell: touch /tmp/cdn002.txt 說明: 第一行須要有三個槓,hosts參數指定了對哪些主機進行參做,若是是多臺機器能夠用逗號做爲分隔,也可使用主機組,在/etc/ansible/hosts裏定義; user參數指定了使用什麼用戶登陸遠程主機操做; tasks指定了一個任務,其下面的name參數一樣是對任務的描述,在執行過程當中會打印出來,shell是ansible模塊名字 執行:ansible-playbook test.yml
[root@Dasoncheng ~]# vim /etc/ansible/test.yml [root@Dasoncheng ~]# cat !$ cat /etc/ansible/test.yml --- - hosts: cdn002 remote_user: root tasks: - name: test_playbook shell: touch /tmp/cdn002.txt [root@Dasoncheng ~]# ansible-playbook /etc/ansible/test.yml PLAY [cdn002] **************************************************************************************** TASK [Gathering Facts] ******************************************************************************* ok: [cdn002] 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: [cdn002] PLAY RECAP ******************************************************************************************* cdn002 : ok=2 changed=1 unreachable=0 failed=0 [root@Dasoncheng ~]# ansible cdn002 -m command -a "ls -l /tmp/cdn002.txt" cdn002 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 0 Apr 20 20:02 /tmp/cdn002.txt
再來一個建立用戶的例子: vi /etc/ansible/create_user.yml //加入以下內容 --- - name: create_user hosts: cdn002 user: root gather_facts: false vars: - user: "test" tasks: - name: create user user: name="{{ user }}" 說明:name參數對該playbook實現的功能作一個概述,後面執行過程當中,會打印 name變量的值 ,能夠省略;gather_facts參數指定了在如下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在後面的task會使用到setup獲取的信息時用到;vars參數,指定了變量,這裏指字一個user變量,其值爲test ,須要注意的是,變量值必定要用引號引住;user提定了調用user模塊,name是user模塊裏的一個參數,而增長的用戶名字調用了上面user變量的值。
[root@Dasoncheng ~]# vim /etc/ansible/create_user.yml [root@Dasoncheng ~]# cat !$ cat /etc/ansible/create_user.yml --- - name: create_user hosts: cdn002 user: root gather_facts: false vars: - user: "test" tasks: - name: create user user: name="{{ user }}" [root@Dasoncheng ~]# ansible-playbook /etc/ansible/create_user.yml PLAY [create_user] *********************************************************************************** TASK [create user] *********************************************************************************** changed: [cdn002] PLAY RECAP ******************************************************************************************* cdn002 : ok=1 changed=1 unreachable=0 failed=0 ##看到上面的changed=1了沒有?這樣就是執行成功了,我再執行一遍 changed=0看下面: [root@Dasoncheng ~]# ansible cdn002 -m command -a "id test" cdn002 | SUCCESS | rc=0 >> uid=1003(test) gid=1003(test) groups=1003(test) [root@Dasoncheng ~]# ansible-playbook /etc/ansible/create_user.yml PLAY [create_user] *********************************************************************************** TASK [create user] *********************************************************************************** ok: [cdn002] PLAY RECAP ******************************************************************************************* cdn002 : ok=1 changed=0 unreachable=0 failed=0
vi /etc/ansible/while.yml //加入以下內容 --- - hosts: testhost user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 with_items: - 1.txt - 2.txt - 3.txt 說明: with_items爲循環的對象 執行 ansible-playbook while.yml
[root@Dasoncheng ~]# vim /etc/ansible/while.yml [root@Dasoncheng ~]# cat /etc/ansible/while.yml --- - hosts: cdn002 user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} state=touch mode=600 with_items: - 1.txt - 2.txt - 3.txt [root@Dasoncheng ~]# ansible-playbook !$ ansible-playbook /etc/ansible/while.yml PLAY [cdn002] **************************************************************************************** TASK [Gathering Facts] ******************************************************************************* ok: [cdn002] TASK [change mode for files] ************************************************************************* changed: [cdn002] => (item=1.txt) changed: [cdn002] => (item=2.txt) changed: [cdn002] => (item=3.txt) PLAY RECAP ******************************************************************************************* cdn002 : ok=2 changed=1 unreachable=0 failed=0 [root@Dasoncheng ~]# ansible cdn002 -m shell -a 'ls -l /tmp/*.txt' cdn002 | SUCCESS | rc=0 >> -rw------- 1 root root 0 Apr 20 20:28 /tmp/1.txt -rw------- 1 root root 0 Apr 20 20:28 /tmp/2.txt -rw------- 1 root root 0 Apr 20 20:28 /tmp/3.txt -rw-r--r-- 1 root root 29 Apr 20 17:54 /tmp/ansible_test.txt -rw-r--r-- 1 root root 0 Apr 20 20:02 /tmp/cdn002.txt
vi /etc/ansible/when.yml //加入以下內容 --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_ens33.ipv4.address == "172.7.15.114「 說明:ansible cdn002 -m setup 能夠查看到全部的facter信息
[root@Dasoncheng ~]# vim /etc/ansible/when.sh [root@Dasoncheng ~]# cat !$ cat /etc/ansible/when.sh --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_ens33.ipv4.address == "192.168.60.12" [root@Dasoncheng ~]# ansible-playbook /etc/ansible/when.sh PLAY [testhost] ************************************************************************************** TASK [Gathering Facts] ******************************************************************************* ok: [127.0.0.1] ok: [cdn002] ok: [192.168.60.12] TASK [use when] ************************************************************************************** skipping: [127.0.0.1] [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: [192.168.60.12] changed: [cdn002] PLAY RECAP ******************************************************************************************* 127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 192.168.60.12 : ok=2 changed=1 unreachable=0 failed=0 cdn002 : ok=2 changed=1 unreachable=0 failed=0
這個handlers可用於好比像Nginx配置文件修改,須要從新加載的時候能夠這樣操做(相似於shell裏面的"&&")!mysql
執行task以後,服務器發生變化以後要執行的一些操做,好比咱們修改了配置文件後,須要重啓一下服務 vi /etc/ansible/hand.yml//加入以下內容 --- - name: handlers test hosts: cdn002 user: root tasks: - name: copy file copy: src=/etc/passwd dest=/tmp/aaa.txt notify: test handlers_custom handlers: - name: test handlers_custom ##這裏須要和上面的notify一致 shell: echo "11111111" >> /tmp/aaa.txt 說明,只有copy模塊真正執行後,纔會去調用下面的handlers相關的操做。也就是說若是1.txt和2.txt內容是同樣的,並不會去執行handlers裏面的shell相關命令。 這種比較適合配置文件發生更改後,重啓服務的操做。
[root@Dasoncheng ~]# vim /etc/ansible/hand.yml [root@Dasoncheng ~]# cat /etc/ansible/hand.yml --- - name: handlers test hosts: cdn002 user: root gather_facts: false tasks: - name: copy file copy: src=/etc/passwd dest=/tmp/aaa.txt notify: test handlers_custom handlers: - name: test handlers_custom shell: echo "11111111" >> /tmp/aaa.txt [root@Dasoncheng ~]# ansible-playbook /etc/ansible/hand.yml PLAY [handlers test] ********************************************************************************* TASK [copy file] ************************************************************************************* changed: [cdn002] RUNNING HANDLER [test handlers_custom] *************************************************************** changed: [cdn002] PLAY RECAP ******************************************************************************************* cdn002 : ok=2 changed=2 unreachable=0 failed=0 [root@Dason02 ~]# tail /tmp/aaa.txt systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin gecz:x:1000:0::/home/gecz:/bin/bash mysql:x:1001:1001::/home/mysql:/sbin/nologin jenkins:x:997:995:Jenkins Automation Server:/var/lib/jenkins:/bin/false 11111111