Reload Ansible's dynamic inventory
With Ansible 2.0+, you can refresh your inventory mid-play by running the task:
- meta: refresh_inventory
I found the meta: refresh_inventory
to be insufficient.
I had to add an explicit call to ec2.py --refresh-cache
first.
- name: refresh inventory hosts: localhost connection: local gather_facts: False tasks: - name: Refresh EC2 cache command: /etc/ansible/ec2.py --refresh-cache - name: Refresh in-memory EC2 cache meta: refresh_inventory
Ansible currently doesn't support this. If you look at the source code of the ansible
or ansible-playbook
commands you'll see that the inventory is loaded first and then the inventory object is passed to the ansible command that runs the specified task or playbook. Moving the inventory processing so that it happens within the task/playbook handlers would probably be a pretty major undertaking for a number of reasons.
Your best bet when doing something like this is to simply break your playbook into two and wrap their calls in a shell script that you only have to invoke once.