Execution
Date 22 Jul 2025 11:20:29 +0100
Duration 00:00:01.66
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
7 Tasks
7 Results
1 Plays
1 Files
0 Records

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

---
- name: Get MAC address of eno1 (or first physical interface)
  hosts: all
  gather_facts: true

  tasks:
    - name: Show all discovered interfaces (for debugging)
      ansible.builtin.debug:
        msg: "All interfaces found: {{ ansible_facts.interfaces }}"

    - name: Filter physical interfaces (en*, eth*, em*, ens*, eno*, enp*)
      ansible.builtin.set_fact:
        physical_interfaces: >-
          {{
            ansible_facts.interfaces
            | select('match', '^(en|eth|em|ens|eno|enp)')
            | map('extract', ansible_facts)
            | selectattr('macaddress', 'defined')
            | list
          }}

    - name: Show filtered physical interfaces
      ansible.builtin.debug:
        msg: "Filtered physical interfaces: {{ physical_interfaces | map(attribute='device') | list }}"

    - name: Check if any physical interfaces found
      ansible.builtin.assert:
        that:
          - "physical_interfaces | length > 0"
        fail_msg: "No physical network interfaces found"
        success_msg: "Found physical interfaces"

    - name: Try to find eno1 specifically
      ansible.builtin.set_fact:
        target_interface: >-
          {{
            physical_interfaces
            | selectattr('device', 'match', '^eno1$')
            | list
            | first
          }}

    - name: Use eno1 if found, otherwise fall back to first physical interface
      ansible.builtin.set_fact:
        target_interface: >-
          {{
            (target_interface is defined and target_interface)
            or physical_interfaces[0]
          }}

    - name: Show MAC address of selected interface
      ansible.builtin.debug:
        msg: "MAC Address of {{ target_interface.device }} is {{ target_interface.macaddress }}"