如何从vsphere_guest使用ipaddresses变量导致playbook?

时间:2017-01-17 08:12:06

标签: ansible ansible-inventory ansible-facts

我想使用ipaddresses的变量vsphere_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

我该怎么办?

1 个答案:

答案 0 :(得分:3)

假设你想在你获得ipaddresses变量的虚拟机上运行另一个游戏(并且有一个IP地址或者可以使用列表中的第一个IP地址访问它),你可以继续玩你的游戏: / p>

- 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: