Execution
Date 22 Jul 2025 11:17:24 +0100
Duration 00:00:00.68
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
1 Hosts
2 Tasks
2 Results
1 Plays
1 Files
0 Records

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

---
- name: Get MAC address of first physical network interface
  hosts: all
  gather_facts: true
  vars:
    # List of prefixes commonly used for physical interfaces
    physical_prefixes:
      - "en"
      - "eth"
      - "em"
      - "ens"
      - "eno"
      - "enp"

  tasks:
    - name: Filter physical interfaces
      ansible.builtin.set_fact:
        physical_interfaces: >-
          {{
            ansible_facts.interfaces | select('match', '^(en|eth|em|ens|eno|enp)\\d+\\.?\\d*$')
            | map('extract', ansible_facts)
            | list
          }}

    - name: Check if physical interfaces were found
      ansible.builtin.assert:
        that:
          - "physical_interfaces | length > 0"
        fail_msg: "No physical network interfaces found matching {{ physical_prefixes }}*"
        success_msg: "Found physical interfaces"

    - name: Set first physical interface
      ansible.builtin.set_fact:
        first_physical_interface: "{{ physical_interfaces[0].device }}"

    - name: Show MAC address of first physical interface
      ansible.builtin.debug:
        msg: "MAC Address of {{ first_physical_interface }} is {{ physical_interfaces[0].macaddress }}"