$cat vmguest_facts.yml
---
- hosts: all
gather_facts: false
tasks:
- name: get vmguest facts
delegate_to: localhost
vsphere_guest:
vmware_guest_facts: true
vcenter_hostname: vmware
username: "{{ hostvars[inventory_hostname].ansible_vsphere_user }}"
password: "{{ hostvars[inventory_hostname].ansible_vsphere_pass }}"
guest: vmguest
register: vmguset_facts
tags: vmfacts
- name: debug facts
debug: msg="vmguest facts are {{ vmguest_facts }}"
tags: vmfacts
$ ansible-playbook -i hosts -l vmware vmguest_facts.yml
PLAY [vmware] *****************************************************************
TASK: [get vmguest facts] *****************************************************
ok: [192.168.x.y -> localhost]
TASK: [debug facts] ***********************************************************
ok: [192.168.x.y] => {
"msg": "vmguest facts are {'invocation': {'module_name': u'vsphere_guest', 'module_args': ''}, u'changed': False, u'ansible_facts': {u'hw_name': u'vmguest', u'hw_processor_count': 2, u'hw_memtotal_mb': 2048, u'module_hw': True, u'hw_guest_full_name': u'Ubuntu Linux (64-bit)', u'hw_guest_id': u'ubuntu64Guest', u'hw_eth0': {u'macaddress': u'00:0c:1f:7c:b7:1a', u'label': u'Network adapter 1', u'addresstype': u'generated', u'summary': u'VM Network', u'macaddress_dash': u'00-0c-1f-7c-b7-1a'}, u'hw_product_uuid': u'564df381-ef59-9100-3c11-ba2c29cf4bfe'}}"
}
PLAY RECAP ********************************************************************
192.168.x.y : ok=2 changed=0 unreachable=0 failed=0
$cat facts.py
#!/usr/bin/python
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from pysphere import VIServer
server = VIServer()
server.connect("192.168.x.y", "user", "password")
vm1 = server.get_vm_by_name("vmguest")
print vm1.get_property('net')
$ ./facts.py
[{'ip_addresses': ['192.168.x.z', 'fc00:::3a8e:54b4:4137:9b95', 'fe80::20c:2aff:fecf:9bf1'], 'connected': True, 'network': 'VM Network', 'mac_address': '00:0c:1f:7c:b7:1a'}]
---- name: Discover vSphere guest hosts: localhost tasks: - name: Gather instance facts vmware_guest_facts: hostname: "{{ ansible_env.VMWARE_SERVER }}" validate_certs: false username: "{{ ansible_env.VMWARE_USERNAME }}" password: "{{ ansible_env.VMWARE_PASSWORD }}" datacenter: "{{ nsible_env.VMWARE_DATACENTER }}" name: "{{ guest_name }}" register: instance_facts until: instance_facts != -1 changed_when: false retries: 30 delay: 2
- name: Fail when no running instances are found fail: msg: "No running instances named {{ guest_name }} found!" when: instance_facts.instance.ipv4 == "None"
- name: Add vSphere host to inventory add_host: name: "{{ instance_facts.instance.ipv4 }}" groups: vsphere_hosts
- name: Do stuff gather_facts: true hosts: vsphere_hosts
tasks: