Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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")}}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
363 views
Welcome To Ask or Share your Answers For Others

1 Answer

The solution I have ended up with is: I removed add_host and used meta: refresh_inventory (after I had waited for a connection & confirmed I could shh to the new server )

After the refresh I then started a new play. This is important, as it is only in the new play I see the new host now added to the dynamic inventory group.

In short it now looks like this:

- name: Play 1
  hosts: localhost
  become: true
  pre_tasks:

     - name: Waits for SSH port 22 of the host to become available
       wait_for:
            host: vm_to_be_added
            port: 22
            state: started
            timeout: 600

     # Now that we know that the vm_to_be_added is contactable, refresh to have it included in the inventory.
     # The newly refreshed inventory will only be availble to the next play. 
     - meta: refresh_inventory
  
- name: Play 2
  hosts: dynamic_group
  become: true
  pre_tasks:

     # This task will be run against the newly added host 
     - set_fact: ANSIBLE_HOST_IP="{{lookup("pipe","hostname -I")}}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...