I would like to determine the interface name of the hosts where ansible connects and use it as a variable, but the final goal is to create an ifcfg file config for every network interface on every host in the inventory. The problem is that I have hosts with multiple interfaces and mac address, so I've tried this to retrieve the interfaces:
shell: ip -o link | grep [MACADDR] | awk '{print $2}' | awk '{sub(/:/,X,$0);print}'
register: interface
shell: ip -o link | grep [MACADDR] | awk '{print $17}'
register: macaddr
But it gets more complicated because I have to write the ifcfg file for every interface on every host, like this (2-3 interfaces for every host) and this is what I've tried in my playbook:
- name: Create ifcfg file
blockinfile:
path: /etc/sysconfig/network-scripts/ifcfg-VLAN
create: yes
insertafter: EOF
block: |
DEVICE={{ interface }}
ONBOOT=yes
BOOTPROTO=static
IPADDR=xx.xxx.xxx.xx
NETMASK=255.255.255.0
DEFROUTE=no
HWADDR={{ macaddr }}
when: "{{ macaddr == item.network_1.macaddr }}"
with_items: "{{ networkinfo }}"
i have a vars_file with this:
networkinfo:
network_1:
macaddr: 00:50:56:a2:22:27
network_2:
macaddr: 00:50:56:a2:58:a5
But this doesn't work out, someone knows to adjust this mess?
Thanks in advance!