Azure动态资源清册中未应用Ansible add_主机

Azure动态资源清册中未应用Ansible add_主机,azure,ansible,ansible-inventory,Azure,Ansible,Ansible Inventory,我正在尝试在运行时将Ansible中的主机添加到Azure动态资源清册 首先,我记录“ansible\u play\u hosts\u all”清单变量的“before add\u host”内容 然后我使用“add_host”添加一个新主机 当我记录“ansible\u play\u hosts\u all”清单变量的“after add\u host”内容时,我可以看到我的新主机被添加到列表中 但是当playbook中的下一个任务运行时,它不会在新添加的主机上运行 我也尝试过使用“meta:

我正在尝试在运行时将Ansible中的主机添加到Azure动态资源清册

首先,我记录“ansible\u play\u hosts\u all”清单变量的“before add\u host”内容 然后我使用“add_host”添加一个新主机 当我记录“ansible\u play\u hosts\u all”清单变量的“after add\u host”内容时,我可以看到我的新主机被添加到列表中

但是当playbook中的下一个任务运行时,它不会在新添加的主机上运行

我也尝试过使用“meta:refresh_inventory”,但没有效果

非常感谢您的帮助-谢谢

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

我最终得到的解决方案是: 我删除了
add_host
并使用了
meta:refresh_inventory
(在我等待连接并确认可以shh到新服务器后)

刷新之后,我开始了一部新剧。这一点很重要,因为只有在新的重头戏中,我才看到新主机现在添加到动态库存组中

简而言之,现在看起来是这样的:

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

据我所知,一旦一个剧目的
-hosts:
被声明,就是这样,
add\u hosts:
只会影响针对该组的下一个剧目。只有在使用动态资源清册时,才能使用
refesh_inventory
,如果新添加的主机与动态资源清册匹配,这也是解决问题的合理方法