Rename Network Interface

This is untested, but maybe instead of:

ATTR{address}=="{{ ansible_facts[item]['perm_macaddress'] | default(ansible_facts[item]['macaddress']) }}",

what about something like this:

ATTR{address}=="{{ ansible_facts[item]['perm_macaddress']
                   | default(ansible_facts[item]['macaddress']
                             | default(ansible_facts[item | regex_replace('^(.*)_\d+$', '\1')]['macaddress']
                                      )
                            )
                }}",

or another way to spell it, also untested:

ATTR{address}=="{{ ansible_facts[item]['perm_macaddress']
                   | default(ansible_facts[item
                                           if ansible_facts[item]['macaddress'] is defined
                                           else item | regex_replace('^(.*)_\d+$', '\1')
                                          ]['macaddress']
                            )
                }}",

I don’t like to post untested code, but I think one can work out the details of what I’m trying to do. :slight_smile:

3 Likes

Nowadays those file has been created to first interface eth0 >> eno0 It means udev rules is created but got a looping when try secondary interface… drop udev and don’t work on ifcfg file… I’ll be happy just if I can just bring down secondary interfaces (some hosts I have more than one :slight_smile: ) or just ignore…
Is these ATTR line just related rules right ? I guess feasible to work with (with_items or add something with (with_list) ?

Hi Todd

Looks like first option will work… not sure secondary …
[root@lit-vatst2-q002 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8d:b1:b5 brd ff:ff:ff:ff:ff:ff
inet 10.132.78.194/23 brd 10.132.79.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.132.78.195/23 brd 10.132.79.255 scope global secondary eth0:1
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8d:ef:82 brd ff:ff:ff:ff:ff:ff
inet 10.132.196.253/23 brd 10.132.197.255 scope global eth1
valid_lft forever preferred_lft forever
[root@lit-vatst2-q002 ~]# cd /etc/sysconfig/network-scripts/
[root@lit-vatst2-q002 network-scripts]# ls
ifcfg-eno0 ifcfg-eth0.bak ifdown ifdown-ippp ifdown-post ifdown-sit ifdown-tunnel ifup-bnep ifup-ipv6 ifup-plusb ifup-routes ifup-TeamPort init.ipv6-global
ifcfg-eno1 ifcfg-eth1.bak ifdown-bnep ifdown-ipv6 ifdown-ppp ifdown-Team ifup ifup-eth ifup-isdn ifup-post ifup-sit ifup-tunnel network-functions
ifcfg-eth0:1 ifcfg-lo ifdown-eth ifdown-isdn ifdown-routes ifdown-TeamPort ifup-aliases ifup-ippp ifup-plip ifup-ppp ifup-Team ifup-wireless network-functions-ipv6
[root@lit-vatst2-q002 network-scripts]# cat /etc/udev/rules.d/
69-vdo-start-by-dev.rules 70-rhosp-persistent-net.rules
[root@lit-vatst2-q002 network-scripts]# cat /etc/udev/rules.d/70-rhosp-persistent-net.rules
SUBSYSTEM==“net”, ACTION==“add”, DRIVERS==“?“, ATTR{address}==“00:50:56:8d:b1:b5”, NAME=“eno0”
SUBSYSTEM==“net”, ACTION==“add”, DRIVERS==”?
”, ATTR{address}==“00:50:56:8d:b1:b5”, NAME=“eno0_1”
SUBSYSTEM==“net”, ACTION==“add”, DRIVERS==“?*”, ATTR{address}==“00:50:56:8d:ef:82”, NAME=“eno1”
[root@lit-vatst2-q002 network-scripts]#

ok: [10.132.78.194]
META: ran handlers

TASK [Debug message] ***********************************************************
task path: /app/awxtmp/bwrap_2947910_zlz459co/awx_2947910_kd8_l28h/project/replace_nic_ethx.yml:12
ok: [10.132.78.194] => {
“src_interfaces”: [
“eth0”,
“eth0_1”,
“eth1”
]
}

changed: [10.132.78.194] => (item=eth0) => {
“backup”: “”,
“changed”: true,
“diff”: [
{
“after”: “”,
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”,
“before”: “”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”
},
{
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”
}
],
“invocation”: {
“module_args”: {
“attributes”: null,
“backrefs”: false,
“backup”: false,
“content”: null,
“create”: true,
“delimiter”: null,
“directory_mode”: null,
“firstmatch”: false,
“follow”: false,
“force”: null,
“group”: null,
“insertafter”: null,
“insertbefore”: null,
“line”: “SUBSYSTEM==\“net\”, ACTION==\“add\”, DRIVERS==\”?*\“, ATTR{address}==\“00:50:56:8d:b1:b5\”, NAME=\“eno0\””,
“mode”: null,
“owner”: null,
“path”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules”,
“regexp”: null,
“remote_src”: null,
“selevel”: null,
“serole”: null,
“setype”: null,
“seuser”: null,
“src”: null,
“state”: “present”,
“unsafe_writes”: null,
“validate”: null
}
},
“item”: “eth0”,
“msg”: “line added”
}

\\“?\\“, ATTR{address}==\\“00:50:56:8d:b1:b5\\”, NAME=\\“eno0_1\\””, “src”: null, “seuser”: null, “delimiter”: null, “mode”: null, “firstmatch”: false, “attributes”: null, “backup”: false}}}\n’, ‘’)
changed: [10.132.78.194] => (item=eth0_1) => {
“backup”: “”,
“changed”: true,
“diff”: [
{
“after”: “”,
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”,
“before”: “”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”
},
{
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”
}
],
“invocation”: {
“module_args”: {
“attributes”: null,
“backrefs”: false,
“backup”: false,
“content”: null,
“create”: true,
“delimiter”: null,
“directory_mode”: null,
“firstmatch”: false,
“follow”: false,
“force”: null,
“group”: null,
“insertafter”: null,
“insertbefore”: null,
“line”: “SUBSYSTEM==\“net\”, ACTION==\“add\”, DRIVERS==\”?
\”, ATTR{address}==\“00:50:56:8d:b1:b5\”, NAME=\“eno0_1\”",
“mode”: null,
“owner”: null,
“path”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules”,
“regexp”: null,
“remote_src”: null,
“selevel”: null,
“serole”: null,
“setype”: null,
“seuser”: null,
“src”: null,
“state”: “present”,
“unsafe_writes”: null,
“validate”: null
}
},
“item”: “eth0_1”,
“msg”: “line added”
}

\\“?\\“, ATTR{address}==\\“00:50:56:8d:ef:82\\”, NAME=\\“eno1\\””, “src”: null, “seuser”: null, “delimiter”: null, “mode”: null, “firstmatch”: false, “attributes”: null, “backup”: false}}}\n’, ‘’)
changed: [10.132.78.194] => (item=eth1) => {
“backup”: “”,
“changed”: true,
“diff”: [
{
“after”: “”,
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”,
“before”: “”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (content)”
},
{
“after_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”,
“before_header”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules (file attributes)”
}
],
“invocation”: {
“module_args”: {
“attributes”: null,
“backrefs”: false,
“backup”: false,
“content”: null,
“create”: true,
“delimiter”: null,
“directory_mode”: null,
“firstmatch”: false,
“follow”: false,
“force”: null,
“group”: null,
“insertafter”: null,
“insertbefore”: null,
“line”: “SUBSYSTEM==\“net\”, ACTION==\“add\”, DRIVERS==\”?
\”, ATTR{address}==\“00:50:56:8d:ef:82\”, NAME=\“eno1\”",
“mode”: null,
“owner”: null,
“path”: “/etc/udev/rules.d/70-rhosp-persistent-net.rules”,
“regexp”: null,
“remote_src”: null,
“selevel”: null,
“serole”: null,
“setype”: null,
“seuser”: null,
“src”: null,
“state”: “present”,
“unsafe_writes”: null,
“validate”: null
}
},
“item”: “eth1”,
“msg”: “line added”
}

TASK [Check that src_prefix files exists] **************************************

TASK [Replace interface name in config.json] ***********************************
task path: /app/awxtmp/bwrap_2947910_zlz459co/awx_2947910_kd8_l28h/project/replace_nic_ethx.yml:192
skipping: [10.132.78.194] => (item=eth0) => {
“changed”: false,
“item”: “eth0”,
“skip_reason”: “Conditional result was False”
}
skipping: [10.132.78.194] => (item=eth0_1) => {
“changed”: false,
“item”: “eth0_1”,
“skip_reason”: “Conditional result was False”
}
skipping: [10.132.78.194] => (item=eth1) => {
“changed”: false,
“item”: “eth1”,
“skip_reason”: “Conditional result was False”
}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
10.132.78.194 : ok=15 changed=7 unreachable=0 failed=0

1 Like

mmm first option messed up interface 1 after reboot eth0 >> eno0_1

Lemme test option 2

No luck option 2 doesn’t work at all… it is creating eno0 and eno0_1 into udev with same mac addr… but its faling on ifcfg-eno0 and ifcfg-eno0_1 that creating one device as eno0_1 and no secondary…

I guess the best way would be to ignore or bring down secondary interface…

:expressionless:

I’ve been reading up on device names vs interface names. (It’s a RHEL-8 doc, but similar enough at the higher levels I think.) We may be mixing up things. I think there’s only one device named initially eth0 but renamed via udev to eno0, and it has a MAC address. But there are two interfaces as described/defined in ifcfg-eno0 and ifcfg_eno0_1, both of which map to a single device.

I’d suggest dropping the udev rule for eno0_1 because it’s bogus. Use udev to define device eno0, and use ifcfg-* files to create the two interfaces, both of which are on the eno0 device.

If that’s right, then add a when: on your “Update udev rules” task:

when: ansible_facts[item]['macaddress'] is defined

You don’t want/need the ipv4_secondaries for the udev rules, but you will need them for the ifcfg-* interface definitions.

1 Like

Yeh Todd

You’re a rock star ! Its work to me

- name: Update udev rules
      tags:
        - udev
      when: ansible_facts[item]['macaddress'] is defined  
      lineinfile:
        line: >
          SUBSYSTEM=="net",
          ACTION=="add",
          DRIVERS=="?*",
          ATTR{address}=="{{ ansible_facts[item]['perm_macaddress'] | default(ansible_facts[item]['macaddress']) }}",
          NAME="{{ item | replace(src_prefix, dst_prefix) }}"
        path: /etc/udev/rules.d/70-rhosp-persistent-net.rules
        create: true
      with_items: "{{ src_interfaces | reject('match', '^.*\\..*$') | list }}"```

- name: Update udev rules
      tags:
        - udev
      when: ansible_facts[item]['macaddress'] is defined  
      lineinfile:
        line: >
          SUBSYSTEM=="net",
          ACTION=="add",
          DRIVERS=="?*",
          ATTR{address}=="{{ ansible_facts[item]['perm_macaddress'] | default(ansible_facts[item]['macaddress']) }}",
          NAME="{{ item | replace(src_prefix, dst_prefix) }}"
        path: /etc/udev/rules.d/70-rhosp-persistent-net.rules
        create: true
      with_items: "{{ src_interfaces | reject('match', '^.*\\..*$') | list }}"`

I just need to fix secondary IP after migration but ok :slight_smile:
3 Likes

Since it was @utoddl who got you on the right path, I’ve moved the Solution to his post. Solutions are a big part of how we recognise contributions in the Get Help category, so this seems right here.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.