Ansible 如何在playbook中使用vsphere_来宾结果中的ipaddresses变量?

Ansible 如何在playbook中使用vsphere_来宾结果中的ipaddresses变量?,ansible,ansible-inventory,ansible-facts,Ansible,Ansible Inventory,Ansible Facts,我想使用vsphere\u guest的变量ipaddress。我想先使用vSphere中虚拟机的名称获取其IP地址,然后使用该IP地址在该机器上运行Ansible plays 到目前为止,我已经: - hosts: localhost gather_facts: false vars_prompt: - name: "inventory_hostname" prompt: "Enter virtual machine name" private: no

我想使用
vsphere\u guest
的变量
ipaddress
。我想先使用vSphere中虚拟机的名称获取其IP地址,然后使用该IP地址在该机器上运行Ansible plays

到目前为止,我已经:

- hosts: localhost
  gather_facts: false

  vars_prompt:
    - name: "inventory_hostname"
      prompt: "Enter virtual machine name"
      private: no
      default: "ansible-test"


  vars:
    vcenter_hostname: '192.168.250.1'
    vcenter_user: 'root'
    vcenter_pass: 'pass'

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        guest: "{{ inventory_hostname }}"
        vmware_guest_facts: yes
        validate_certs: no
      register: vsphere_facts

如何继续?

假设您希望在获得
ipaddress
变量的虚拟机上运行另一个播放(并且有一个IP地址或可以使用列表上的第一个IP地址访问),您可以继续播放:

- hosts: localhost
  gather_facts: false

  vars_prompt:
    - name: "inventory_hostname"
      prompt: "Enter virtual machine name"
      private: no
      default: "ansible-test"

  vars:
    vcenter_hostname: '192.168.250.1'
    vcenter_user: 'root'
    vcenter_pass: 'pass'

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        guest: "{{ inventory_hostname }}"
        vmware_guest_facts: yes
        validate_certs: no
      register: vsphere_facts

    - name: Ensure virtual machine is in the dynamic inventory
      add_host:
        name: "{{ vsphere_facts.ansible_facts.hw_eth0.ipaddresses[0] }}"
        ansible_user: _______
        ansible_ssh_private_key_file: _______
        groups: virtual_machines

- name: A play to be run on the virtual machine
  hosts: virtual_machines
  tasks:
    - debug:

请参阅启动实例,从
-debug:var=vsphere\u facts.ipAddresss
运行一些任务示例,然后查看
-debug:var=vsphere\u facts.ipAddresss[0]
我将您的建议合并到了答案中并回滚了问题-如果问题包含答案,那就有点混乱了。很高兴你终于成功了。感谢您配合调试,我目前没有vSphere可供测试。