Dynamic inventories always evaluated when Ansible starts, does not matter if the inventory is a immutable file or a script which dynamically discovers a bunch of IP addresses. In my use case, I need to evaluate the inventory along the way, after provisioning virtual machines and before executing further other tasks.
However, nothing prevents you from spliting one big playbook into smaller ones and breaking the logic so that a certain playbook has chance to evaluate the environment when it (the playbook) starts and finds all hosts you expect to find.
The first playbook would be responsible for provisioning, waiting for the virtual machines to be reachable and retrieving their host keys (see notes below). Then the last thing the first playbook does is kicking off the second playbook, which is responsible for all further tasks, like this:
- name: Kicks off the second playbook, passing the IP address of the recently provisioned virtual machine
shell: "ansible-playbook -i '{{provisioned_host}},' second_playbook,yaml
This approach seems to work. This is a proof of concept:
file: first_playbook.yml
---
- name: This is the first playbook
hosts: localhost
tasks:
- name: set facts -- obtain hostname
shell: "cat /etc/hostname"
register: hostname
- name: debug
debug:
msg: "This is the first playbook: hostname is {{hostname.stdout}}"
- name: set facts -- provisioned_ip
set_fact:
provisioned_host: "192.168.3.152"
# see: ansible-doc -t inventory host_list
- name: Kicks off the second playbook, passing the IP address of the recently provisioned virtual machine
shell: "ansible-playbook -i '{{provisioned_host}},' {{playbook_dir}}/second_playbook.yml"
file: second_playbook.yml
---
- name: This is the second playbook
hosts: all
tasks:
- name: set facts -- obtain hostname
shell: "cat /etc/hostname"
register: hostname
- name: debug
debug:
msg: "This is the second playbook: hostname is {{hostname.stdout}}"
Now call first_playbook
:
ansible-playbook -vv first_playbook.yml
Notes:
- Ansible needs to know the host key of the virtual machines to be contacted. So, before the second playbook starts, you have to retrieve the host keys and insert them into your
~/.ssh/known_hosts
, otherwise the second playbook will fail to connect.