I have to goals with my plabook:
a) get mac address from a specific VM running on vsphere
b) add it to my inventory file
My test environment is:
-Vsphere 6.5
-Ansible 2.7 running on Centos 7.6
I have been able to make point a) following this post How retrieve the name of specific dictionary - Ansible
Playbook:
# Deploy a guest from a template*
- hosts: 127.0.0.1
vars:
vcenter_hostname: virtvcsami1.virtlab.local
vcenter_user: administrator@vsphere.local
vcenter_pass: Esxilab!1
vcenter_datastore: vsanDatastore
vcenter_datacenter: DatacenterMI
tasks:
- name: Gather VMware guest facts
vmware_vm_facts:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
vm_type: vm
delegate_to: localhost
register: vm_guest_facts
- debug: msg="{{ item.value.mac_address }}"
loop: "{{ vm_guest_facts.virtual_machines|dict2items }}"
but now I still have two problems to solve:
Problem 1)
Playbook gets ALL virtual machines while I need to get just a VM named “testvm”
[root@nlnxmi1 testvmcdromiso]# ansible-playbook getvminfo.yml
PLAY [127.0.0.1] ***********************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [127.0.0.1]
TASK [Gather VMware guest facts] *******************************************************************************************************************************************************************************
ok: [127.0.0.1 → localhost]
TASK [debug] ***************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item={‘key’: u’kubemst01’, ‘value’: {u’guest_fullname’: u’CentOS 7 (64-bit)‘, u’vm_network’: {u’00:50:56:be:de:b9’: {u’ipv4’: [u’192.168.40.31’], u’ipv6’: [u’fe80::250:56ff:febe:deb9’]}, u’52:54:00:62:fe:fe’: {u’ipv4’: [u’192.168.122.1’], u’ipv6’: }}, u’cluster’: u’VirtlabMI’, u’esxi_hostname’: u’virtesxmi3.virtlab.local’, u’mac_address’: [u’00:50:56:be:de:b9’], u’power_state’: u’poweredOn’, u’ip_address’: u’192.168.40.31’, u’uuid’: u’423e7580-1ca4-a6ca-5cb4-5b8fa963d527’}}) => { “msg”: [ “00:50:56:be:de:b9” ] }ok: [127.0.0.1] => (item={‘key’: u’testvm’, ‘value’: {u’guest_fullname’: >u’CentOS 7 (64-bit)‘, u’vm_network’: {}, u’cluster’: u’VirtlabMI’, >u’esxi_hostname’: u’virtesxmi1.virtlab.local’, u’mac_address’: >[u’00:50:56:be:a3:a0’], u’power_state’: u’poweredOn’, u’ip_address’: u’‘, >u’uuid’: u’423e8645-ca2a-1097-2e1c-624810a461d1’}}) => { “msg”: [ “00:50:56:be:a3:a0” ] } …
Trying to solve this tedious problem I posted on stackoverflow (https://stackoverflow.com/questions/56243269/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-exist/56245634#56245634) and a user gave me this suggestion:
-----from stackoverflow----
That’s not a problem. It looks like the vmware_vm_facts
module returns a dictionary, so just ask for the vm you want:
- debug: msg="{{ vm_guest_facts.virtual_machines['testvm'].mac_address }}"