Execution
Date 22 Jul 2025 10:47:57 +0100
Duration 00:00:15.63
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.11
ara 1.7.2 / 1.7.2
Python 3.10.10
Summary
27 Hosts
3 Tasks
81 Results
1 Plays
1 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/get_mac_addresses.yaml

---
- name: Get MAC address of primary physical network interface
  hosts: all
  gather_facts: yes
  tasks:

    - name: Filter physical interfaces
      set_fact:
        physical_interfaces: >-
          {{
            ansible_interfaces
            | select('match', '^(eth|eno|ens|em)[0-9]+$')
            | list
          }}

    - name: Find first matching physical interface with MAC
      set_fact:
        primary_physical_iface: >-
          {{
            physical_interfaces
            | select('in', ansible_facts.keys())
            | select('search', '^ansible_')
            | map('regex_replace', '^ansible_', '')
            | select('match', '^(eth|eno|ens|em)[0-9]+$')
            | list
            | first
          }}

    - name: Get MAC address of the selected interface
      debug:
        msg: >-
          MAC address of primary physical interface ({{ primary_physical_iface }}):
          {{ ansible_facts['ansible_' + primary_physical_iface]['macaddress']
             if 'ansible_' + primary_physical_iface in ansible_facts
             else 'MAC not found' }}