Execution
Date 22 Jul 2025 10:55:03 +0100
Duration 00:00:00.84
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
3 Tasks
3 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 card
  hosts: all
  gather_facts: true

  tasks:
    - name: Display primary network interface name and MAC address
      debug:
        msg: "The primary network interface is {{ ansible_default_ipv4.interface }} and its MAC address is {{ hostvars[inventory_hostname]['ansible_' + ansible_default_ipv4.interface]['macaddress'] }}"
      when: ansible_default_ipv4.interface is defined and hostvars[inventory_hostname]['ansible_' + ansible_default_ipv4.interface]['macaddress'] is defined

    - name: Set fact for primary MAC address
      set_fact:
        primary_mac_address: "{{ hostvars[inventory_hostname]['ansible_' + ansible_default_ipv4.interface]['macaddress'] }}"
      when: ansible_default_ipv4.interface is defined and hostvars[inventory_hostname]['ansible_' + ansible_default_ipv4.interface]['macaddress'] is defined

    - name: Print the stored primary MAC address (optional)
      debug:
        var: primary_mac_address
      when: primary_mac_address is defined