{"id":5439,"sha1":"e772c7491fd5cf3dbef3ea7528e7c4f33c64ccd8","playbook":{"id":3319,"items":{"plays":1,"tasks":11,"results":297,"hosts":27,"files":1,"records":0},"arguments":{"version":null,"verbosity":0,"private_key_file":"/home/ssh-gateway/.ssh/id_rsa","remote_user":"root","connection":"ssh","timeout":null,"ssh_common_args":null,"sftp_extra_args":null,"scp_extra_args":null,"ssh_extra_args":null,"ask_pass":false,"connection_password_file":null,"force_handlers":false,"flush_cache":false,"become":false,"become_method":"sudo","become_user":null,"become_ask_pass":false,"become_password_file":null,"tags":["all"],"skip_tags":[],"check":false,"diff":false,"inventory":["/home/ssh-gateway/ansible/kuly/hosts-kvm-nodes"],"listhosts":false,"subset":null,"extra_vars":"Not saved by ARA as configured by 'ignored_arguments'","vault_ids":[],"ask_vault_pass":false,"vault_password_files":[],"forks":20,"module_path":null,"syntax":false,"listtasks":false,"listtags":false,"step":false,"start_at_task":null,"args":["get_mac_addresses.yml"]},"labels":[{"id":1,"name":"remote_user:root"},{"id":2,"name":"check:False"},{"id":3,"name":"tags:all"}],"started":"2025-07-22T11:34:37.540023+01:00","ended":"2025-07-22T11:35:36.581020+01:00","duration":"00:00:59.040997","name":null,"ansible_version":"2.16.11","client_version":"1.7.2","python_version":"3.10.10","server_version":"1.7.2","status":"completed","path":"/home/ssh-gateway/ansible/kuly/get_mac_addresses.yml","controller":"ssh-gw-4.layershift.com","user":"root"},"content":"---\n- name: Get MAC addresses for interfaces that are up\n  hosts: all\n  gather_facts: yes\n  tasks:\n    - name: Debug all ansible network facts\n      debug:\n        var: ansible_facts\n      when: debug_facts is defined\n\n    - name: Show all interface-related facts (for troubleshooting)\n      debug:\n        msg: \"{{ item.key }}: active={{ item.value.active|default('undefined') }}, mac={{ item.value.macaddress|default('undefined') }}\"\n      loop: \"{{ ansible_facts | dict2items }}\"\n      when: \n        - item.key is match(\"^ansible_\")\n        - item.key is match(\".*[0-9]+$\") or item.key in ['ansible_eth0', 'ansible_ens', 'ansible_enp', 'ansible_wlan0', 'ansible_em1']\n      tags: debug\n\n    - name: Method 1 - Get MAC addresses using standard approach\n      set_fact:\n        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')}] }}\"\n      loop: \"{{ ansible_facts | dict2items }}\"\n      when:\n        - item.key is match(\"^ansible_[^_]+$\")\n        - item.value.macaddress is defined\n        - item.value.macaddress != \"\"\n        - not (item.key == \"ansible_lo\")\n\n    - name: Method 2 - Get all interfaces with MAC addresses (ignore active status)\n      set_fact:\n        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')}] }}\"\n      loop: \"{{ ansible_facts | dict2items }}\"\n      when:\n        - item.key is match(\"^ansible_\")\n        - item.value.macaddress is defined\n        - item.value.macaddress != \"\"\n        - item.value.macaddress != \"00:00:00:00:00:00\"\n        - not (item.key == \"ansible_lo\")\n\n    - name: Method 3 - Use command to get interface info on Linux\n      shell: |\n        ip -o link show | awk '/state UP/ {\n          iface = $2; gsub(/:/, \"\", iface); \n          if (match($0, /link\\/ether ([0-9a-f:]+)/)) {\n            mac = substr($0, RSTART+11, RLENGTH-11);\n            print iface \",\" mac\n          }\n        }'\n      register: linux_interfaces\n      failed_when: false\n      changed_when: false\n      when: ansible_system == \"Linux\"\n\n    - name: Method 4 - Use ifconfig for other systems\n      shell: |\n        for iface in $(ifconfig -l 2>/dev/null || ifconfig | grep '^[a-zA-Z]' | cut -d: -f1); do\n          if ifconfig \"$iface\" 2>/dev/null | grep -q \"UP\"; then\n            mac=$(ifconfig \"$iface\" 2>/dev/null | grep -i ether | awk '{print $2}' | head -1)\n            if [ -n \"$mac\" ] && [ \"$mac\" != \"00:00:00:00:00:00\" ]; then\n              echo \"$iface,$mac\"\n            fi\n          fi\n        done\n      register: other_interfaces\n      failed_when: false\n      changed_when: false\n      when: ansible_system != \"Linux\"\n\n    - name: Display Method 1 results (active interfaces only)\n      debug:\n        msg: \"Method 1 - {{ item.host }} - {{ item.interface }} - {{ item.mac }} - {{ item.ipv4 }}\"\n      loop: \"{{ mac_addresses_method1|default([]) }}\"\n      when: mac_addresses_method1 is defined and mac_addresses_method1|length > 0\n\n    - name: Display Method 2 results (all interfaces with MACs)\n      debug:\n        msg: \"Method 2 - {{ item.host }} - {{ item.interface }} - {{ item.mac }} - {{ item.ipv4 }}\"\n      loop: \"{{ mac_addresses_method2|default([]) }}\"\n      when: mac_addresses_method2 is defined and mac_addresses_method2|length > 0\n\n    - name: Display Method 3 results (Linux command output)\n      debug:\n        msg: \"Method 3 - {{ inventory_hostname }} - {{ item.split(',')[0] }} - {{ item.split(',')[1] }}\"\n      loop: \"{{ linux_interfaces.stdout_lines|default([]) }}\"\n      when: linux_interfaces.stdout_lines is defined and linux_interfaces.stdout_lines|length > 0\n\n    - name: Display Method 4 results (ifconfig output)\n      debug:\n        msg: \"Method 4 - {{ inventory_hostname }} - {{ item.split(',')[0] }} - {{ item.split(',')[1] }}\"\n      loop: \"{{ other_interfaces.stdout_lines|default([]) }}\"\n      when: other_interfaces.stdout_lines is defined and other_interfaces.stdout_lines|length > 0\n\n    - name: Summary\n      debug:\n        msg: |\n          Summary for {{ inventory_hostname }}:\n          Method 1 (active facts): {{ mac_addresses_method1|default([])|length }} interfaces\n          Method 2 (all facts): {{ mac_addresses_method2|default([])|length }} interfaces  \n          Method 3 (Linux commands): {{ linux_interfaces.stdout_lines|default([])|length }} interfaces\n          Method 4 (ifconfig): {{ other_interfaces.stdout_lines|default([])|length }} interfaces\n","created":"2025-07-22T11:34:37.558592+01:00","updated":"2025-07-22T11:34:37.558630+01:00","path":"/home/ssh-gateway/ansible/kuly/get_mac_addresses.yml"}