1

我想使用的变量ipaddressesvsphere_guest我想首先在 vSphere 中使用虚拟机的名称来获取其 IP 地址,然后使用该 IP 地址在该机器上运行 Ansible 播放。

到目前为止,我有:

- 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

我应该如何进行?

4

1 回答 1

3

假设你想在你获得变量的虚拟机上运行另一个游戏ipaddresses(并且有一个 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:
于 2017-01-17T10:01:47.100 回答