--- - name: IP Address Alias Testing hosts: all gather_facts: true vars: interface_file: /etc/sysconfig/network-scripts/ifcfg-eth0 default_ipv4: "{{ ansible_default_ipv4['address'] }}" iface_cfg_path: /etc/sysconfig/network-scripts tasks: # - name: Debug all vars # ansible.builtin.debug: # var: ansible_facts - name: Display the default IPV4 IP ansible.builtin.debug: msg: "The default IPv4 IP address is: {{ default_ipv4 }}" - name: Debug ansible_interfaces ansible.builtin.debug: var: ansible_interfaces - name: Find the interface for a given IP address ansible.builtin.set_fact: interface_name: "ansible_{{ ansible_interfaces | map('extract', ansible_facts) | selectattr('ipv4.address', 'equalto', default_ipv4) | map(attribute='device') | first }}" - name: Display the interface name ansible.builtin.debug: msg: "The interface for IP address {{ default_ipv4 }} is {{ interface_name }}" - name: Detect if the default interface has IP aliases defined ansible.builtin.debug: msg: "Aliases were defined for {{ inventory_hostname }}" when: - "interface_name['ipv4_secondaries'] is defined" - name: Capture how many aliases are attached to ifcfg-eth0 ansible.builtin.shell: cmd: | set -o pipefail grep -o 'IPADDR' {{ iface_cfg_path }}/ifcfg-{{ interface_name }} | wc -l ignore_errors: true register: ifcfg_alias_count - name: Display number of aliases ansible.builtin.debug: msg: "Found {{ ifcfg_alias_count.stdout }} aliases referenced in {{ interface_file }}." - name: Run ip command to list IP addresses ansible.builtin.command: ip addr show register: ip_output - name: Check for IP address aliases ansible.builtin.debug: msg: "IP address aliases found: {{ item }}" loop: "{{ ip_output.stdout_lines }}" when: item | regex_search('inet .* secondary')