# How to find network device by macaddress ?

**URL:** <https://forum.ansible.com/t/how-to-find-network-device-by-macaddress/23077>\
**Category:** Ansible Project\
**Created:** [November 22, 2016, 8:18pm UTC](https://forum.ansible.com/t/how-to-find-network-device-by-macaddress/23077 "2016-11-22T20:18:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Don\_Jackson](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Don\_Jackson](https://forum.ansible.com/u/Don_Jackson)\
**Post date:** [November 22, 2016, 8:18pm UTC](https://forum.ansible.com/t/how-to-find-network-device-by-macaddress/23077/1 "2016-11-22T20:18:15Z")

</div>

Here is a truncated version of a fact for one network interface on a server:

> “ansible\_eth2”: {
> 
> “active”: true,
> 
> “device”: “eth2”,
> 
> “macaddress”: “30:0e:d5:cb:11:fe”,
> 
> “type”: “ether”
> 
> },

And there are similar facts for each network interface.

Given that, I need to be able to find the value for “device” for a specific macaddress value.

How would I do that?

---

<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:** [November 22, 2016, 9:05pm UTC](https://forum.ansible.com/t/how-to-find-network-device-by-macaddress/23077/2 "2016-11-22T21:05:38Z")

</div>

Something like this.

mac.yml

---

<div class="post-metadata">

**Author:** ![Don\_Jackson](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Don\_Jackson](https://forum.ansible.com/u/Don_Jackson)\
**Post date:** [November 23, 2016, 1:37am UTC](https://forum.ansible.com/t/how-to-find-network-device-by-macaddress/23077/3 "2016-11-23T01:37:21Z")

</div>

This worked great, thank you!

For the record, here is the current version of my test playbook:

> - hosts: [foo.example.com](http://foo.example.com)
> 
> vars:
> 
> target\_macaddr: “0c:c4:7a:17:9a:94”
> 
> tasks:
> 
> - debug:  
> var: hostvars[inventory\_hostname][‘ansible\_’ + item].device  
> when: hostvars[inventory\_hostname][‘ansible\_’ + item].macaddress is defined and hostvars[inventory\_hostname][‘ansible\_’ + item].macaddress == target\_macaddr  
> with\_items: ‘{{ ansible\_interfaces }}’

Which prints out the device whose macaddr is the target.
