Ansible: 런타임 시 호스트 인벤토리를 새로 고치고 플레이북을 실행하는 동안 일시 중지를 추가하는 방법은 무엇입니까?
게시 됨: 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
파일에 추가합니다. - 후속 작업에서는 호스트 파일에 추가된 새로 추가된 공용 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 인스턴스를 생성, 시작 및 구성하는 방법은 무엇입니까? (원격으로 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
을 생성한 후 VM의 공개 IP를 캡처하여 hosts
파일에 저장합니다.
동일한 .yml file
에 몇 가지 추가 작업이 있는 경우 IP가 자동으로 로드되지 않습니다. Ansible Playbook에 아래 줄을 추가하기만 하면 모든 인벤토리 목록이 자동으로 새로 고쳐집니다.
1 |
- meta : refresh_inventory |
대기를 추가하려면 같은 방식으로 다음 줄을 추가하십시오.
1 2 |
- pause : minutes : 1 |
위 태그는 Ansible 플레이북을 1분 동안 기다리게 합니다.
대기 및 메타 새로 고침으로 Amazon EC2용 업데이트된 Ansible 플레이북
다음은 내 샘플 테스트 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 페이지를 따르십시오.