How to find network device by macaddress ?

118 views
Skip to first unread message

Don Jackson

unread,
Nov 22, 2016, 3:18:15 PM11/22/16
to Ansible Project

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?


Kai Stian Olstad

unread,
Nov 22, 2016, 4:05:58 PM11/22/16
to ansible...@googlegroups.com
Something like this.

mac.yml
---
- debug: msg="This is the mac"
when: hostvars[inventory_hostname]['ansible_' + item].macaddress |
default() == "xx:xx:xx:xx:xx:xx"


play.yml
---
- hosts: all
tasks:
- include: mac.yml
with_items: "{{ ansible_interfaces }}"


--
Kai Stian Olstad

Don Jackson

unread,
Nov 22, 2016, 8:37:21 PM11/22/16
to Ansible Project, ansible-pr...@olstad.com
This worked great, thank you!

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


  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.


Reply all
Reply to author
Forward
0 new messages