I recently faced the challenge that I needed to modify the Ansible inventory (/etc/ansible/hosts) from within a playbook. My use-case was to build the node names and put them into the Ansible hosts file. (Note that for my simple use-case I did not want to use a dynamic inventory.)
While manipulating the hosts file quite simple from a Ansible playbook perspective, the interesting part lies in the fact that the inventory file is read before processing of plays starts. So what I needed was some way to dynamically update the node set, or simply reload the inventory from within my playbook.
The latter, as it turned out, is actually quite simple!
There is a meta task named refresh_inventory
for exactly that purpose!
See the example below:
|
|
Thanks to fredl for discovering this not-so-well-known Ansible task.