Ansible add_host 未在 Azure 动态清单中应用

Ansible add_host not being applied in Azure dynamic inventory

我正在尝试在 运行 时间将 Ansible 中的主机添加到 Azure 动态清单。

首先我记录 'ansible_play_hosts_all' 清单变量的“add_host 之前”的内容 然后我使用“add_host”添加一个新主机 当我记录 'ansible_play_hosts_all' 清单变量的“add_host 之后”内容时,我可以看到我的新主机已添加到列表中。

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

我也试过使用“元: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")}}"