# Ansible vmware\_vm\_facts: get mac address for a specific VM and write it to existing inventory

**URL:** <https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [May 22, 2019, 8:55pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403 "2019-05-22T20:55:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Reale](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Marco\_Reale](https://forum.ansible.com/u/Marco_Reale)\
**Post date:** [May 22, 2019, 8:55pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/1 "2019-05-22T20:55:15Z")

</div>

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](https://stackoverflow.com/questions/53453718/how-retrieve-the-name-of-specific-dictionary-ansible)

Playbook:

```auto
# 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@nlnxmi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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> 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](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:

```auto
  - debug: msg="{{ vm_guest_facts.virtual_machines['testvm'].mac_address }}"  

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 22, 2019, 9:48pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/2 "2019-05-22T21:48:22Z")

</div>

> Problem 1)
> 
> # Deploy a guest from a template\*  
> - hosts: 127.0.0.1  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;vcenter\_hostname: virtvcsami1.virtlab.local  
> &nbsp;&nbsp;&nbsp;&nbsp;vcenter\_user: administrator@vsphere.local  
> &nbsp;&nbsp;&nbsp;&nbsp;vcenter\_pass: Esxilab!1  
> &nbsp;&nbsp;&nbsp;&nbsp;vcenter\_datastore: vsanDatastore  
> &nbsp;&nbsp;&nbsp;&nbsp;vcenter\_datacenter: DatacenterMI  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: Gather VMware guest facts  
> &nbsp;&nbsp;&nbsp;&nbsp;vmware\_vm\_facts:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: False  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ vcenter\_hostname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ vcenter\_user }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ vcenter\_pass }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_type: vm  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;register: vm\_guest\_facts
> 
> # - debug: msg="{{ item.value.mac\_address }}"  
> # loop: "{{ vm\_guest\_facts.virtual\_machines|dict2items }}"  
> &nbsp;&nbsp;&nbsp;- debug: msg="{{ vm\_guest\_facts.virtual\_machines['testvm'].mac\_address }}"
> 
> but is this way I get an error:
> 
> [root@nlnxmi1 testvmcdromiso]# ansible-playbook getvminfo.yml  
> ERROR! Syntax Error while loading YAML.  
> did not find expected key

Your debug is indented to far, remove one space.

> Problem 2)
> 
> Add mac address to existing inventory file or, if not possible, at least in  
> some file
> 
> Just as a stupid test I tried adding the following code at the end:
> 
> - set\_fact: vm\_mac\_address="prova"
> 
> &nbsp;&nbsp;- name: Register host to Inventory  
> &nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/ansible/testvmcdromiso/inventory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '(testvm)'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '\1 macaddres={{ vm\_mac\_address }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes [root@nlnxmi1 testvmcdromiso]# cat inventory  
> &nbsp;&nbsp;[testhost]  
> &nbsp;&nbsp;testvm macaddress=prova
> 
> but as you can see I just used a "fixed" string instead I need to get the  
> mac address from the running vm but never figure it out even after 2 days  
> of attempts ☹
> 
> I'm just a beginner with ansible. Could you please help me?

Just remove the set\_fact and use the variable directly

&nbsp;&nbsp;&nbsp;line: '\1 macaddress={{ vm\_guest\_facts.virtual\_machines["\1"].mac\_address }}'

---

<div class="post-metadata">

**Author:** ![Marco\_Reale](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Marco\_Reale](https://forum.ansible.com/u/Marco_Reale)\
**Post date:** [May 23, 2019, 7:38am UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/3 "2019-05-23T07:38:25Z")

</div>

Hi Kai

sorry, I just did a bad copy and paste; in reality my playbook is correct about spaces and indentation in fact as I wrote (I posted the complete output) it works using the “loop” statement but, in this way, I’m not able to filter by vm name.  
Instead using:

- debug: msg=“{{  
vm\_guest\_facts.virtual\_machines[‘testvm’].mac\_address }}”

the playbook gives me an error.

This is my 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 }}”

- debug: msg=“{{  
vm\_guest\_facts.virtual\_machines[‘testvm’].mac\_address }}”

Could someone test it against a vsphere environment?

---

<div class="post-metadata">

**Author:** ![Marco\_Reale](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Marco\_Reale](https://forum.ansible.com/u/Marco_Reale)\
**Post date:** [June 5, 2019, 9:31pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/4 "2019-06-05T21:31:43Z")

</div>

Hi guys

Is there someone that coul help on this?

---

<div class="post-metadata">

**Author:** ![Indian\_Velumani1](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@Indian\_Velumani1](https://forum.ansible.com/u/Indian_Velumani1)\
**Post date:** [June 6, 2019, 7:28am UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/5 "2019-06-06T07:28:13Z")

</div>

Hi Marco,

Correct if I am wrong.

Your base goal is to achieve adding a host based on the output of **vmware\_vm\_facts** in order to communicate or execute some  
a function on that VM via ansible.

For that, you can use dynamic host configuration in Ansible. That will add a host dynamically and execute the module based on  
your configuration.

If you want any further help regarding please let me know. I will help you.

Regards,  
Indian Velumani

---

<div class="post-metadata">

**Author:** ![Marco\_Reale](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Marco\_Reale](https://forum.ansible.com/u/Marco_Reale)\
**Post date:** [June 6, 2019, 7:06pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/6 "2019-06-06T19:06:25Z")

</div>

Hi indian

Before all thanks for your kind reply.  
If you read my post (I hope it is clear enough), basically I need to get ONLY the mac address of a specific VM “testvm”, put it in a variable and, using the regex, add it in a file.  
Do you have a vsphere to test my playbook?  
Please note that i did lot of internet searches and it seems that the hard part is to make working the folowing code that should do the trick:

```auto
debug: msg="{{ vm_guest_facts.virtual_machines['testvm'].mac_address }}" 

```

```auto
Best

```

```auto
Marco

```

---

<div class="post-metadata">

**Author:** ![Indian\_Velumani1](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@Indian\_Velumani1](https://forum.ansible.com/u/Indian_Velumani1)\
**Post date:** [June 6, 2019, 7:35pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/7 "2019-06-06T19:35:08Z")

</div>

Hi Marco,

Use this below module I already used this. It is working for me.  
[https://docs.ansible.com/ansible/2.5/modules/vmware\_guest\_facts\_module.html](https://docs.ansible.com/ansible/2.5/modules/vmware_guest_facts_module.html)

```auto
- name: Gather facts from standalone ESXi server having datacenter as 'ha-datacenter'
  vmware_guest_facts:
    hostname: 192.168.1.209
    username: administrator@vsphere.local
    password: vmware
    datacenter: ha-datacenter
    validate_certs: no
    uuid: 421e4592-c069-924d-ce20-7e7533fab926
  delegate_to: localhost
  register: facts

```

---

<div class="post-metadata">

**Author:** ![Marco\_Reale](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Marco\_Reale](https://forum.ansible.com/u/Marco_Reale)\
**Post date:** [June 7, 2019, 9:24am UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/8 "2019-06-07T09:24:26Z")

</div>

Hi Indian

ok but do you have any chance to give me the working code about what I’m asking for?

---

<div class="post-metadata">

**Author:** ![Indian\_Velumani1](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@Indian\_Velumani1](https://forum.ansible.com/u/Indian_Velumani1)\
**Post date:** [June 7, 2019, 9:35am UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/9 "2019-06-07T09:35:52Z")

</div>

Hi Marco,

Yeah, I can, But I need vm\_guest\_facts values.

Can you give me the output of below 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=“{{ vm\_guest\_facts }}”

Thanks,  
Indian velumani

---

<div class="post-metadata">

**Author:** ![Philip\_Isaac](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@Philip\_Isaac](https://forum.ansible.com/u/Philip_Isaac)\
**Post date:** [August 28, 2019, 12:59pm UTC](https://forum.ansible.com/t/ansible-vmware-vm-facts-get-mac-address-for-a-specific-vm-and-write-it-to-existing-inventory/30403/10 "2019-08-28T12:59:57Z")

</div>

I am trying to accomplish a similar task but to add an IP address to the inventory instead of mac.  
This works for me to get the IP address displayed in msg. How do you then take the result and send to inventory file? I see you setting fact for the MAC address.  
I also am not having any luck passing a variable of vm\_name to be used in the query. It can only take the hard coded name.
