Ansible: วิธีรีเฟรชโฮสต์สินค้าคงคลังที่รันไทม์และเพิ่มการหยุดชั่วคราวขณะดำเนินการ Playbook
เผยแพร่แล้ว: 2022-02-14 ใน Crunchify เราได้เผยแพร่บทช่วยสอนจำนวนหนึ่งเกี่ยวกับ Ansible และบทนี้จะเน้นที่ How to Refresh Host Inventory
ที่รันไทม์และ Add a Pause
ขณะดำเนินการงาน
มาลองพิจารณาสถานการณ์การสร้าง Amazon EC2 VM อย่างง่ายนี้กัน:
- คุณกำลังเรียกใช้สคริปต์ Amazon EC2 Ansible ซึ่งวางไข่ VM และคุณกำลังจับ IP สาธารณะในสคริปต์แบบไดนามิก
- ระหว่างดำเนินการ – คุณกำลังเพิ่ม Public IP ของ VM นั้นไปยังไฟล์
hosts
ภายใต้กลุ่ม[crunchify]
- ในงานต่อมา ให้ใช้ Public IP ที่เพิ่มใหม่ซึ่งถูกเพิ่มในไฟล์โฮสต์และติดตั้ง Java บนมัน
- หากคุณไม่ได้ใช้
refresh_inventory
ระบบจะไม่รับ IP ที่เพิ่มใหม่ และคุณจะเห็นความล้มเหลวเช่นเดียวกับด้านล่าง
1 2 |
PLAY [ crunchify ] ************************************* skipping : no hosts matched |
นอกจากนี้ คุณมีคำถามด้านล่างหรือไม่?
- วิธีโหลด
dynamic inventory
ของ Ansible - ขั้นตอนการรีเฟรชสินค้าคงคลังแบบไดนามิก Ansible
- ฉันสามารถอัปเดตรายการโฮสต์และใช้โฮสต์ใหม่ใน
same playbook
ได้หรือไม่ - Ansible Dynamic Inventory ล้มเหลวในการรับข้อมูล EC2 ล่าสุด – เพราะเหตุใด
มาเริ่มกันเลย:
ขั้นตอนที่ 1
ปฏิบัติตามบทช่วยสอนเกี่ยวกับวิธีการ spawn Amazon EC2 VM instance remotely using Ansible
หรือไม่
จะสร้าง เริ่ม และกำหนดค่าอินสแตนซ์ Amazon EC2 โดยใช้ Ansible Script อย่างง่ายได้อย่างไร (วางไข่ VM จากระยะไกล)
ขั้นตอนที่ 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
แล้ว เรากำลังจับ IP สาธารณะของ VM และจัดเก็บไว้ในไฟล์ hosts
หากคุณมีงานเพิ่มเติมใน .yml file
เดียวกัน ระบบจะไม่โหลด IP โดยอัตโนมัติ เพียงเพิ่มบรรทัดด้านล่างลงใน Ansible Playbook แล้วระบบจะรีเฟรชรายการสินค้าคงคลังทั้งหมดของคุณโดยอัตโนมัติ
1 |
- meta : refresh_inventory |
วิธีเดียวกันถ้าคุณต้องการเพิ่มรอเพียงเพิ่มบรรทัดด้านล่าง:
1 2 |
- pause : minutes : 1 |
แท็กด้านบนจะแนะนำให้รอนานหนึ่งนาทีสำหรับ Ansible playbook ของคุณ
อัปเดต Ansible Playbook สำหรับ Amazon EC2 พร้อมการรอและรีเฟรช Meta
นี่คือตัวอย่างการทดสอบ 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 Archive