# retrieving facter\_ec2\_metadata from hostvars - can't get element in macs

**URL:** <https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828>\
**Category:** Ansible Project\
**Created:** [March 5, 2019, 8:38pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828 "2019-03-05T20:38:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Jefferies](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Chris\_Jefferies](https://forum.ansible.com/u/Chris_Jefferies)\
**Post date:** [March 5, 2019, 8:38pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/1 "2019-03-05T20:38:53Z")

</div>

I can drill down into hostvars like this:

`

- debug:  
msg: “looking at interface macs: {{ hostvars[inventory\_hostname].facter\_ec2\_metadata.network.interfaces.macs }}”

`

And I get something like this:

`

ok: [db01.deva.ecm] =\> {  
“msg”: “looking at interface macs: {u’xx:xx:xx:xx:xx:xx’: {u’local-hostname’: u’ip-10-1-1-9.us-west-1.compute.internal’, u’security-groups’: u’sg\_me’, u’vpc-ipv4-cidr-blocks’: u’10.1.0.0/16’, u’subnet-id’: u’subnet-009ae259d26e44f23’, u’vpc-ipv4-cidr-block’: u’10.1.0.0/16’, u’interface-id’: u’eni-0f706387a1423f329’, u’mac’: u’xx:xx:xx:xx:xx:xx, u’security-group-ids’: u’sg-034d209d935486e41’, u’local-ipv4s’: u’10.1.1.10’, u’owner-id’: u’784710449476’, u’subnet-ipv4-cidr-block’: u’10.1.1.0/24’, u’device-number’: u’0’, u’vpc-id’: u’vpc-1710943aba582459a’}}”  
}

`

I get an error if I try to find the first/only mac address with the [0] approach like this:

`  
{{ hostvars[inventory\_hostname].facter\_ec2\_metadata.network.interfaces.macs[0] }}

`

or

`  
{{ hostvars[inventory\_hostname].facter\_ec2\_metadata.network.interfaces.macs.0 }}

`

What’s the trick to get to the values within the mac address?

Any tips would be appreciated, thanks,  
Chris.

---

<div class="post-metadata">

**Author:** ![Chris\_Jefferies](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Chris\_Jefferies](https://forum.ansible.com/u/Chris_Jefferies)\
**Post date:** [March 5, 2019, 10:35pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/2 "2019-03-05T22:35:12Z")

</div>

ansible 2.7.1  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/Users/cjefferies/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /Library/Python/2.7/site-packages/ansible  
executable location = /usr/local/bin/ansible  
python version = 2.7.10 (default, Aug 17 2018, 17:41:52) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)]

---

<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:** [March 7, 2019, 5:19pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/3 "2019-03-07T17:19:03Z")

</div>

macs is a dict and not a list. Therefor using 0 to get the fist list element wont work.

To get information out of a dict you need to use the key which is the mac address in you case.

---

<div class="post-metadata">

**Author:** ![Chris\_Jefferies](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Chris\_Jefferies](https://forum.ansible.com/u/Chris_Jefferies)\
**Post date:** [March 9, 2019, 1:47am UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/4 "2019-03-09T01:47:04Z")

</div>

That’s too bad because I don’t have the mac address before I need to read its attributes. How might a person get a list of the mac addresses associated with the interfaces?

Thanks for the info.  
Chris.

---

<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:** [March 11, 2019, 2:28pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/5 "2019-03-11T14:28:42Z")

</div>

You could try using python method to get the keys .....macs.keys() this will return the list of all keys in macs.  
So the fist one would be .....macs.keys()[0]

---

<div class="post-metadata">

**Author:** ![Chris\_Jefferies](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Chris\_Jefferies](https://forum.ansible.com/u/Chris_Jefferies)\
**Post date:** [March 12, 2019, 7:03pm UTC](https://forum.ansible.com/t/retrieving-facter-ec2-metadata-from-hostvars-cant-get-element-in-macs/29828/6 "2019-03-12T19:03:10Z")

</div>

That worked, Kai. Thank you.
