0

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!

  • 2
    Ansible already collects interface information for you in the `ansible_interfaces` variable and the various `ansible_` variables. Why not just use these instead of trying to re-discover the same information using `ip link`? – larsks Jul 03 '22 at 00:40
  • 1
    I'm also unclear on the relevance of the `networkinfo` variable: Is that...a list of MAC addresses of interfaces in which you are interested, and you want to ignore interfaces that aren't listed in that variable? Or something else? – larsks Jul 03 '22 at 00:41
  • Depending on the Linux distribution and version, `network-scripts` are already available under `/etc/sysconfig` for each network adapter. Can you therefore provide more information regarding your Linux distribution and version? Is it possible in your environment and setup to install additional packages via native OS package manager? – U880D Jul 04 '22 at 15:47
  • Is your (this) question about [_a certain number of servers with REHL 7.9 and every server has 2-3 network adapters_](https://stackoverflow.com/q/72837784/6771046)? – U880D Jul 04 '22 at 15:51

0 Answers0