Execution
Date 22 Jul 2025 11:34:37 +0100
Duration 00:00:59.04
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
11 Tasks
297 Results
1 Plays
1 Files
0 Records

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

---
- name: Get MAC addresses for interfaces that are up
  hosts: all
  gather_facts: yes
  tasks:
    - name: Debug all ansible network facts
      debug:
        var: ansible_facts
      when: debug_facts is defined

    - name: Show all interface-related facts (for troubleshooting)
      debug:
        msg: "{{ item.key }}: active={{ item.value.active|default('undefined') }}, mac={{ item.value.macaddress|default('undefined') }}"
      loop: "{{ ansible_facts | dict2items }}"
      when: 
        - item.key is match("^ansible_")
        - item.key is match(".*[0-9]+$") or item.key in ['ansible_eth0', 'ansible_ens', 'ansible_enp', 'ansible_wlan0', 'ansible_em1']
      tags: debug

    - name: Method 1 - Get MAC addresses using standard approach
      set_fact:
        mac_addresses_method1: "{{ mac_addresses_method1|default([]) + [{'host': inventory_hostname, 'interface': item.key|regex_replace('^ansible_', ''), 'mac': item.value.macaddress, 'ipv4': item.value.ipv4.address|default('N/A')}] }}"
      loop: "{{ ansible_facts | dict2items }}"
      when:
        - item.key is match("^ansible_[^_]+$")
        - item.value.macaddress is defined
        - item.value.macaddress != ""
        - not (item.key == "ansible_lo")

    - name: Method 2 - Get all interfaces with MAC addresses (ignore active status)
      set_fact:
        mac_addresses_method2: "{{ mac_addresses_method2|default([]) + [{'host': inventory_hostname, 'interface': item.key|regex_replace('^ansible_', ''), 'mac': item.value.macaddress, 'ipv4': item.value.ipv4.address|default('N/A')}] }}"
      loop: "{{ ansible_facts | dict2items }}"
      when:
        - item.key is match("^ansible_")
        - item.value.macaddress is defined
        - item.value.macaddress != ""
        - item.value.macaddress != "00:00:00:00:00:00"
        - not (item.key == "ansible_lo")

    - name: Method 3 - Use command to get interface info on Linux
      shell: |
        ip -o link show | awk '/state UP/ {
          iface = $2; gsub(/:/, "", iface); 
          if (match($0, /link\/ether ([0-9a-f:]+)/)) {
            mac = substr($0, RSTART+11, RLENGTH-11);
            print iface "," mac
          }
        }'
      register: linux_interfaces
      failed_when: false
      changed_when: false
      when: ansible_system == "Linux"

    - name: Method 4 - Use ifconfig for other systems
      shell: |
        for iface in $(ifconfig -l 2>/dev/null || ifconfig | grep '^[a-zA-Z]' | cut -d: -f1); do
          if ifconfig "$iface" 2>/dev/null | grep -q "UP"; then
            mac=$(ifconfig "$iface" 2>/dev/null | grep -i ether | awk '{print $2}' | head -1)
            if [ -n "$mac" ] && [ "$mac" != "00:00:00:00:00:00" ]; then
              echo "$iface,$mac"
            fi
          fi
        done
      register: other_interfaces
      failed_when: false
      changed_when: false
      when: ansible_system != "Linux"

    - name: Display Method 1 results (active interfaces only)
      debug:
        msg: "Method 1 - {{ item.host }} - {{ item.interface }} - {{ item.mac }} - {{ item.ipv4 }}"
      loop: "{{ mac_addresses_method1|default([]) }}"
      when: mac_addresses_method1 is defined and mac_addresses_method1|length > 0

    - name: Display Method 2 results (all interfaces with MACs)
      debug:
        msg: "Method 2 - {{ item.host }} - {{ item.interface }} - {{ item.mac }} - {{ item.ipv4 }}"
      loop: "{{ mac_addresses_method2|default([]) }}"
      when: mac_addresses_method2 is defined and mac_addresses_method2|length > 0

    - name: Display Method 3 results (Linux command output)
      debug:
        msg: "Method 3 - {{ inventory_hostname }} - {{ item.split(',')[0] }} - {{ item.split(',')[1] }}"
      loop: "{{ linux_interfaces.stdout_lines|default([]) }}"
      when: linux_interfaces.stdout_lines is defined and linux_interfaces.stdout_lines|length > 0

    - name: Display Method 4 results (ifconfig output)
      debug:
        msg: "Method 4 - {{ inventory_hostname }} - {{ item.split(',')[0] }} - {{ item.split(',')[1] }}"
      loop: "{{ other_interfaces.stdout_lines|default([]) }}"
      when: other_interfaces.stdout_lines is defined and other_interfaces.stdout_lines|length > 0

    - name: Summary
      debug:
        msg: |
          Summary for {{ inventory_hostname }}:
          Method 1 (active facts): {{ mac_addresses_method1|default([])|length }} interfaces
          Method 2 (all facts): {{ mac_addresses_method2|default([])|length }} interfaces  
          Method 3 (Linux commands): {{ linux_interfaces.stdout_lines|default([])|length }} interfaces
          Method 4 (ifconfig): {{ other_interfaces.stdout_lines|default([])|length }} interfaces