# Set a fact based on a fact name in a fact

**URL:** <https://forum.ansible.com/t/set-a-fact-based-on-a-fact-name-in-a-fact/22538>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [September 13, 2016, 9:55pm UTC](https://forum.ansible.com/t/set-a-fact-based-on-a-fact-name-in-a-fact/22538 "2016-09-13T21:55:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lise\_Mattice](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@Lise\_Mattice](https://forum.ansible.com/u/Lise_Mattice)\
**Post date:** [September 13, 2016, 9:55pm UTC](https://forum.ansible.com/t/set-a-fact-based-on-a-fact-name-in-a-fact/22538/1 "2016-09-13T21:55:16Z")

</div>

I have a situation where I am trying to dynamically get the MAC address on the first NIC in a new VM created by Ansible. I could assume the HW Interface will be “eth0”, but it would be better to figure it out at runtime.

Register variable “create\_vm\_results” is:

ok: [localhost] =\> {  
“create\_vm\_results”: {  
“ansible\_facts”: {  
“hw\_eth0”: {  
“addresstype”: “assigned”,  
“ipaddresses”: null,  
“label”: “Network adapter 1”,  
“macaddress”: “MAC”,  
“macaddress\_dash”: “Dash-MAC”,  
“summary”: “VM Network”  
},  
“hw\_guest\_full\_name”: “Red Hat Enterprise Linux 6 (64-bit)”,  
“hw\_guest\_id”: “rhel6\_64Guest”,  
“hw\_interfaces”: [  
“eth0”  
],  
“hw\_memtotal\_mb”: 2048,  
“hw\_name”: “NAME”,  
“hw\_power\_status”: “POWERED ON”,  
“hw\_processor\_count”: 1,  
“hw\_product\_uuid”: “UUID”,  
“module\_hw”: true  
},  
“changed”: true,  
“changes”: “Created VM NAME”  
}  
}

Now set facts based on this portion of my playbook:

- name: VMWARE - Identify NIC of primary interface  
set\_fact:  
nic: “{{ create\_vm\_results.ansible\_facts.hw\_interfaces[0] }}”

- name: VMWARE - Show Created VM Primary NIC (DEBUG)  
debug: var=nic

- name: VMWARE - Identify Adapter of primary interface  
set\_fact:  
adptr: “create\_vm\_results.ansible\_facts.hw\_{{ nic }}.macaddress”

- name: VMWARE - Show Created VM Primary Adapter (DEBUG)  
debug: var=adptr

- name: VMWARE - Identify MAC Address of primary interface  
set\_fact:  
mac: “{{ adptr }}”

- name: VMWARE - Show Created VM Primary MAC (DEBUG)  
debug: var=mac

This produced this output:

TASK [vm\_create : VMWARE - Identify NIC of primary interface] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [vm\_create : VMWARE - Show Created VM Primary NIC (DEBUG)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“nic”: “eth0”  
}

TASK [vm\_create : VMWARE - Identify Adapter of primary interface] \*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [vm\_create : VMWARE - Show Created VM Primary Adapter (DEBUG)] \*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“adptr”: “create\_vm\_results.ansible\_facts.hw\_eth0.macaddress”  
}

TASK [vm\_create : VMWARE - Identify MAC Address of primary interface] \*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=create\_vm\_results.ansible\_facts.hw\_eth0.macaddress)

TASK [vm\_create : VMWARE - Show Created VM Primary MAC (DEBUG)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mac”: “create\_vm\_results.ansible\_facts.hw\_eth0.macaddress”  
}

The objective was to get the MAC, but instead it is the “fact” name. Is there a way to look up the value of this fact?

Any assistance would be appreciated.

Thanks

---

<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:** [September 14, 2016, 6:06pm UTC](https://forum.ansible.com/t/set-a-fact-based-on-a-fact-name-in-a-fact/22538/2 "2016-09-14T18:06:29Z")

</div>

[snip]

> - name: VMWARE - Identify Adapter of primary interface  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;adptr: "create\_vm\_results.ansible\_facts.hw\_{{ nic }}.macaddress"
> 
> - name: VMWARE - Show Created VM Primary Adapter (DEBUG)  
> &nbsp;&nbsp;debug: var=adptr

[snip]

> TASK [vm\_create : VMWARE - Show Created VM Primary Adapter (DEBUG)]  
> \*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"adptr": "create\_vm\_results.ansible\_facts.hw\_eth0.macaddress"  
> }

[snip]

> The objective was to get the MAC, but instead it is the "fact" name. Is  
> there a way to look up the value of this fact?
> 
> Any assistance would be appreciated.

To use variables you need to use hostvars to make it work

{{ hostvars[inventory\_hostname]['create\_vm\_results']['ansible\_facts']['hw\_' + nic]['macaddress'] }}

---

<div class="post-metadata">

**Author:** ![Lise\_Mattice](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@Lise\_Mattice](https://forum.ansible.com/u/Lise_Mattice)\
**Post date:** [September 17, 2016, 12:18pm UTC](https://forum.ansible.com/t/set-a-fact-based-on-a-fact-name-in-a-fact/22538/3 "2016-09-17T12:18:45Z")

</div>

> Thanks Kai!

That worked perfectly!

Lise
