I am trying to add a host in Ansible to the Azure dynamic inventory at run time.
First I log the "before add_host" contents of the 'ansible_play_hosts_all' inventory variable Then I use "add_host" to add a new host When I log the "after add_host" contents of the 'ansible_play_hosts_all' inventory variable, I can see my new host added to the list.
But when the next task in the playbook is run, it is not run on the newly added host.
I have also tried using "meta: refresh_inventory", but to no avail.
Any help is appreciated - thanks
- name: "Log the contents of the 'ansible_play_hosts_all' magic inventory variable before testing ssh connectivity"
debug:
msg: "{{ ansible_play_hosts_all }}"
# Wait for vm_to_be_added to become contactable via ssh, then refresh the inventory
- name: Waits for SSH port 22 of the EPMP host to become available
wait_for:
host: vm_to_be_added
port: 22
state: started
timeout: 600
# Add vm_to_be_added host to the dynamic inventory
- name: Add vm_to_be_addedhost to the dynamic inventory
add_host:
hostname: "vm_to_be_added"
group: tag_workspace_cluster
# Log the contents of the 'ansible_play_hosts_all' magic inventory variable after testing ssh connectivity
- name: "Log the contents of the 'ansible_play_hosts_all' magic inventory variable after testing ssh connectivity"
debug:
msg: "{{ ansible_play_hosts_all }}"
# Record the IP of the machine currently running(hosting) Ansible.
- set_fact: ANSIBLE_HOST_IP="{{lookup("pipe","hostname -I")}}"