Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

Ansible: How to Refresh Host Inventory at Runtime and Add a Pause while Executing Playbook?

How to Refresh Host Inventory at Runtime and Add a Pause while Executing Playbook

On Crunchify, we have published so number of tutorials on Ansible and this one is going to focus on How to Refresh Host Inventory at Runtime and Add a Pause while Executing Tasks.

Let’s consider this simple Amazon EC2 VM creation scenario:

  • You are running Amazon EC2 Ansible script which spawns a VM and you are dynamically capturing Public IPs in script.
  • During execution – you are adding Public IP of that VM to hosts file under [crunchify] group.
  • In subsequent task, use the newly added Public IP which was added to hosts file and install Java on it.
  • If you don’t use refresh_inventory then it wont pick it up newly added IP and you will see failure same as below.
PLAY [crunchify] *************************************
skipping: no hosts matched

In addition, do you have any of below questions?

  • How to Reload Ansible’s dynamic inventory
  • Ansible dynamic inventory refresh steps
  • Can I update the hosts inventory and use new hosts in same playbook?
  • Ansible Dynamic Inventory fails to get the latest EC2 information – why?

Let’s get started:

Step-1

Completely follow tutorial on how to spawn Amazon EC2 VM instance remotely using Ansible?

How to Create, Start and Configure Amazon EC2 instance using simple Ansible Script? (spawn VM remotely)

Step-2

Look for a below task in crunchify-ec2.yml file.

- name: Add the newly created EC2 instance(s) to the local host group
  local_action: lineinfile 
                path=hosts
                regexp= 
                insertafter="[crunchify]" line=
  with_items: ''

As you see here, after creating Amazon EC2 VM, we are capturing VM’s public IP and storing it into hosts file 🙂

If you have some additional tasks in the same .yml file then IPs wont be loaded automatically. Just add below line to your Ansible Playbook and it will automatically refresh all of your inventory list.

- meta: refresh_inventory

Same way if you want to add wait just add below line:

- pause:
    minutes: 1

Above tag will introduce a minute long wait to your Ansible playbook.

Updated Ansible Playbook for Amazon EC2 with Wait and Meta Refresh

Updated Ansible Playbook for Amazon EC2 with Wait and Meta Refresh - Crunchify Tips

Here is my sample test Ansible Playbook:

File name: crunchify-refresh-hostfile-pause-1min.yml

---
- 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

Output:

Just execute your Ansible Playbook again and you will see successful result as below.

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

That’s it. Congratulations. You have successfully ran Ansible Playbook.

If you are looking for all of Ansible tutorials then please follow Ansible Archive page.

The post Ansible: How to Refresh Host Inventory at Runtime and Add a Pause while Executing Playbook? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires