Ansible 二: playbook


playbook

playbook可以定義為一些列任務的配置集合。也稱為劇本,每一個playbook都包含一系列的任務,每個任務在Ansible中稱為play。Playbook的寫法采用縮排的方式呈現,結構通過縮進來表示,連續的項目通過減號 “-”來表示。Playbook的語法具有如下的特性:

1 需要用---開始,而且是頂行首寫

2 使用#注釋

3 縮進必須統一,不能使用空格和Tab

4 縮進的級別必須是統一的,同樣的縮進代表同樣的級別

5 一個完整的代碼塊功能需最少元素,需包括name:task

6 一個name只能包含一個task

下面是一個playbook的樣本。是采用file命令創建一個文件,使用方法是touch,mode=0755

---

- hosts: webservers

  remote_user: wuqi

  tasks:

   - name: for command test

file: path=/home/wuqi/go/ansible.go state=touch mode=0755

 

同樣的不采用file命令,僅僅采用shell命令也是可以做到的。但是要分成2個task來做。不能將兩個command寫在同一個task里面,這樣的話只會執行最后一個command

---

- hosts: webservers

  remote_user: wuqi

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/ansible1.go

   - name: change the mod

     command: chmod 755 /home/wuqi/go/ansible1.go

 

如果我們想看執行過程中的詳細輸出的話可以加上 –verbose命令。

ansible-playbook ./test2.yml –verbose

輸出如下:

Using /etc/ansible/ansible.cfg as config file

PLAY [webservers] **************************************************************

 

TASK [Gathering Facts] *********************************************************

[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 10.0.1.48 should use

/usr/bin/python3, but is using /usr/bin/python for backward compatibility with

prior Ansible releases. A future Ansible release will default to using the

discovered platform python for this host. See https://docs.ansible.com/ansible/

2.9/reference_appendices/interpreter_discovery.html for more information. This

feature will be removed in version 2.12. Deprecation warnings can be disabled

by setting deprecation_warnings=False in ansible.cfg.

ok: [10.0.1.48]

 

TASK [create a go file] ********************************************************

[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: [10.0.1.48] => {"changed": true, "cmd": ["touch", "/home/wuqi/go/ansible3.go"], "delta": "0:00:00.001532", "end": "2020-05-19 16:30:30.831893", "rc": 0, "start": "2020-05-19 16:30:30.830361", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

 

TASK [change the mod] **********************************************************

[WARNING]: Consider using the file module with mode rather than running

'chmod'.  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: [10.0.1.48] => {"changed": true, "cmd": ["chmod", "755", "/home/wuqi/go/ansible3.go"], "delta": "0:00:00.001517", "end": "2020-05-19 16:30:31.301749", "rc": 0, "start": "2020-05-19 16:30:31.300232", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

 

PLAY RECAP *********************************************************************

10.0.1.48                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

 

Handler:

可以把handlers理解成另一種tasks,handlers是另一種'任務列表',handlers中的任務會被tasks中的任務進行"調用",但是,被"調用"並不意味着一定會執行,只有當tasks中的任務"真正執行"以后(真正的進行實際操作,造成了實際的改變),handlers中被調用的任務才會執行,如果tasks中的任務並沒有做出任何實際的操作,那么handlers中的任務即使被'調用',也並不會執行。來看下面的例子:

---

- hosts: webservers

  remote_user: wuqi

  vars:

          file_name: ansible3.go

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/{{file_name}}

     #file: path=/home/wuqi/go/ansible.go state=touch mode=0755

   - name: change the mod

     command: chmod 755 /home/wuqi/go/{{file_name}}

     notify: rm

  handlers:

   - name: rm

     command: rm /home/wuqi/go/{{file_name}}

 

我們使用handlers關鍵字,指明哪些任務可以被'調用',之前說過,handlers是另一種任務列表,你可以把handlers理解成另外一種tasks,你可以理解成它們是'平級'的,所以,handlers與tasks是'對齊'的(縮進相同)。在上例中,首先是創建文件,然后是chmod。最后使用notify關鍵字調用handlers中的任務。對應的任務名也是rm的。然后刪掉之前創建的文件。

我們還可以在一個task中一次性notify多個handler,怎樣才能一次性notify多個handler呢?你可能會嘗試將多個handler使用相同的name,但是這樣並不可行,因為當多個handler的name相同時,只有一個handler會被執行,所以,我們並不能通過這種方式notify多個handler,如果想要一次notify多個handler,則需要借助另一個關鍵字,它就是'listen',你可以把listen理解成"組名",我們可以把多個handler分成"組",當我們需要一次性notify多個handler時,只要將多個handler分為"一組",使用相同的"組名"即可,當notify對應的值為"組名"時,"組"內的所有handler都會被notify。來看下面的這個例子:

---

- hosts: webservers

  remote_user: wuqi

  vars:

          file_name: ansible3.go

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/{{file_name}}

   - name: change the mod

     command: chmod 755 /home/wuqi/go/{{file_name}}

     notify: handler group1

  handlers:

   - name: rm

     listen: handler group1

     command: rm /home/wuqi/go/{{file_name}}

   - name: create again

     listen: handler group1

     command: touch /home/wuqi/go/{{file_name}}

 

在這里,handlers下面雖然有2個name不一樣的task,但是listen都是 handler group1,因此可以算是一組。當notify調用 handler group1的時候,兩個任務都會運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM