Ansible:如何在運行時刷新主機清單並在執行 Playbook 時添加暫停?
已發表: 2022-02-14 在 Crunchify 上,我們發布了很多關於 Ansible 的教程,本篇將重點介紹How to Refresh Host Inventory
和在執行任務時Add a Pause
。
讓我們考慮這個簡單的 Amazon EC2 VM 創建場景:
- 您正在運行生成 VM 的 Amazon EC2 Ansible 腳本,並且您正在腳本中動態捕獲公共 IP。
- 在執行期間 - 您將該 VM 的公共 IP 添加到
[crunchify]
組下的hosts
文件。 - 在後續任務中,使用添加到 hosts 文件中的新添加的公共 IP 並在其上安裝 Java。
- 如果您不使用
refresh_inventory
那麼它不會選擇新添加的 IP,您將看到與下面相同的失敗。
1 2 |
PLAY [ crunchify ] ************************************* skipping : no hosts matched |
另外,您有以下問題嗎?
- 如何重新加載 Ansible 的
dynamic inventory
- Ansible 動態庫存刷新步驟
- 我可以更新主機清單並在
same playbook
中使用新主機嗎? - Ansible Dynamic Inventory 無法獲取最新的 EC2 信息——為什麼?
讓我們開始吧:
第1步
完全遵循如何spawn Amazon EC2 VM instance remotely using Ansible
?
如何使用簡單的 Ansible 腳本創建、啟動和配置 Amazon EC2 實例? (遠程生成虛擬機)
第2步
在crunchify-ec2.yml
文件中查找以下任務。
1 2 3 4 5 6 |
- name : Add the newly created EC2 instance ( s ) to the local host group local_action : lineinfile path = hosts regexp = { { item . public_ip } } insertafter = "[crunchify]" line = { { item . public_ip } } with_items : '{{ec2_crunchify.instances}}' |
正如您在此處看到的,在創建Amazon EC2 VM
之後,我們正在捕獲 VM 的公共 IP 並將其存儲到hosts
文件中
如果您在同一個.yml file
中有一些額外的任務,那麼 IP 將不會自動加載。 只需將以下行添加到您的 Ansible Playbook,它就會自動刷新您的所有庫存列表。
1 |
- meta : refresh_inventory |
同樣,如果您想添加等待,只需添加以下行:
1 2 |
- pause : minutes : 1 |
上面的標籤會給你的 Ansible playbook 帶來一分鐘的等待。
使用等待和元刷新更新了適用於 Amazon EC2 的 Ansible Playbook
這是我的示例測試 Ansible Playbook:
文件名:crunchify-refresh-hostfile-pause-1min.yml
1 2 3 4 5 6 7 8 9 10 11 |
--- - name : Refresh Host Inventory at Runtime and Add a Pause while Executing Playbook hosts : local connection : local gather_facts : True tasks : - meta : refresh_inventory - pause : minutes : 1 |
輸出:
只需再次執行 Ansible Playbook,您將看到如下所示的成功結果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
bash - 3.2 $ ansible - playbook - vvv - i . / hosts crunchify - refresh - hostfile - pause - 1min.yml config file = None configured module search path = [ u '/Users/crunchify/.ansible/plugins/modules' , u '/usr/share/ansible/plugins/modules' ] ansible python module location = / Library / Python / 2.7 / site - packages / ansible executable location = / usr / local / bin / ansible - playbook python version = 2.7.16 ( default , Jul 14 2019 , 03 : 47 : 49 ) [ GCC 4.2.1 Compatible Apple LLVM 11.0.0 ( clang - 1100.0.32.4 ) ( - macos10 . 15 - objc - s No config file found ; using defaults / Users / crunchify / Documents / ansible / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / Users / crunchify / Documents / ansible / hosts did not meet script requirements , check plugin documentation if this is unexpected Parsed / Users / crunchify / Documents / ansible / hosts inventory source with ini plugin PLAYBOOK : crunchify - refresh - hostfile - pause - 1min.yml ************************************************************************************************************************ 1 plays in crunchify - refresh - hostfile - pause - 1min.yml PLAY [ Refresh Host Inventory at Runtime and Add a Pause while Executing Playbook . ] ***************************************************************************************************** TASK [ Gathering Facts ] ***************************************************************************************************************************************************** task path : / Users / crunchify / Documents / ansible / crunchify - refresh - hostfile - pause - 1min.yml : 2 < localhost > ESTABLISH LOCAL CONNECTION FOR USER : crunchify < localhost > EXEC / bin / sh - c 'echo ~crunchify && sleep 0' < localhost > EXEC / bin / sh - c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906 `" && echo ansible-tmp-1567083207.3-89685003930906="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906 `" ) && sleep 0' Using module file / Library / Python / 2.7 / site - packages / ansible / modules / system / setup . py < localhost > PUT / Users / crunchify / . ansible / tmp / ansible - local - 47439Jkp0pq / tmpa9O5B0 TO / Users / crunchify / . ansible / tmp / ansible - tmp - 1567083207.3 - 89685003930906 / AnsiballZ_setup . py < localhost > EXEC / bin / sh - c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567083207.3-89685003930906/ > /dev/null 2>&1 && sleep 0' ok : [ localhost ] META : ran handlers / Users / crunchify / Documents / ansible / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / Users / crunchify / Documents / ansible / hosts did not meet script requirements , check plugin documentation if this is unexpected Parsed / Users / crunchify / Documents / ansible / hosts inventory source with ini plugin META : inventory successfully refreshed TASK [ pause ] *************************************************************************************************************************************************************** task path : / Users / crunchify / Documents / ansible / crunchify - refresh - hostfile - pause - 1min.yml : 10 Pausing for 60 seconds ( ctrl + C then 'C' = continue early , ctrl + C then 'A' = abort ) ok : [ localhost ] = > { "changed" : false , "delta" : 60 , "echo" : true , "rc" : 0 , "start" : "2019-08-29 07:53:28.064115" , "stderr" : "" , "stdout" : "Paused for 1.0 minutes" , "stop" : "2019-08-29 07:54:28.064686" , "user_input" : "" } META : ran handlers META : ran handlers PLAY RECAP * **************************************************************************************************************************************************************** localhost : ok = 2 changed = 0 unreachable = 0 failed = 0 |
而已。 恭喜。 您已成功運行 Ansible Playbook。

如果您正在尋找所有 Ansible 教程,請關注 Ansible 存檔頁面。