Last active
December 16, 2021 14:03
-
-
Save odyssey4me/fcf02190356e9b155a5c8ebee2930d51 to your computer and use it in GitHub Desktop.
Playbook to rename network interfaces - now published in https://opendev.org/openstack/tripleo-upgrade/src/commit/c864f47764b17a08e1417638f6043e56441c25ab/files/rename-nics.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# Final published location: https://access.redhat.com/solutions/4067471 | |
- name: Rename network interfaces | |
hosts: all | |
become: true | |
vars: | |
src_prefix: "eth" | |
dst_prefix: "em" | |
osnet_conf: "/etc/os-net-config/config.json" | |
src_interfaces: "{{ ansible_interfaces | select('match', src_prefix ~ '.*') | sort | list }}" | |
undercloud_conf: "~/undercloud.conf" | |
tasks: | |
- debug: | |
msg: "{{ src_interfaces }}" | |
- name: Update udev rules | |
tags: | |
- udev | |
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: Rename ifcfg files | |
tags: | |
- ifcfg | |
block: | |
- name: Check that src_prefix files exists | |
stat: | |
path: /etc/sysconfig/network-scripts/ifcfg-{{ item }} | |
register: nic_result | |
with_items: "{{ src_interfaces }}" | |
- name: Copy ifcfg files using dst_prefix | |
copy: | |
remote_src: true | |
src: "{{ item.stat.path }}" | |
dest: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}" | |
with_items: "{{ nic_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Edit NAME in new network-script files | |
lineinfile: | |
regexp: "^NAME=.*" | |
line: "NAME={{ item.item | replace(src_prefix, dst_prefix) }}" | |
path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}" | |
with_items: "{{ nic_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Edit DEVICE in new network-script files | |
lineinfile: | |
regexp: "^DEVICE=.*" | |
line: "DEVICE={{ item.item | replace(src_prefix, dst_prefix) }}" | |
path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}" | |
with_items: "{{ nic_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Backup old ifcfg network-script files | |
copy: | |
remote_src: true | |
src: "{{ item.stat.path }}" | |
dest: "{{ item.stat.path }}.bak" | |
with_items: "{{ nic_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Remove old ifcfg network-script files | |
file: | |
path: "{{ item.stat.path }}" | |
state: absent | |
with_items: "{{ nic_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Rename route files | |
tags: | |
- routes | |
block: | |
- name: Check that route files exists | |
stat: | |
path: /etc/sysconfig/network-scripts/route-{{ item }} | |
register: route_result | |
with_items: "{{ src_interfaces }}" | |
- name: Copy route files using dst_prefix | |
copy: | |
remote_src: true | |
src: "{{ item.stat.path }}" | |
dest: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}" | |
with_items: "{{ route_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Update dst_prefix in route files that use IP command arguments format | |
replace: | |
regexp: "{{ src_prefix }}" | |
replace: "{{ dst_prefix }}" | |
path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}" | |
with_items: "{{ route_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Backup old route files | |
copy: | |
remote_src: true | |
src: "{{ item.stat.path }}" | |
dest: "{{ item.stat.path }}.bak" | |
with_items: "{{ route_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Remove old route files | |
file: | |
path: "{{ item.stat.path }}" | |
state: absent | |
with_items: "{{ route_result.results }}" | |
when: item.stat.exists | |
loop_control: | |
label: "{{ item.item }}" | |
- name: Perform a final regex for any remaining src_prefix in ifcfg files | |
tags: | |
- ifcfg | |
block: | |
- name: Get a list of all ifcfg files | |
find: | |
paths: /etc/sysconfig/network-scripts/ | |
patterns: 'ifcfg-*' | |
excludes: '*.bak' | |
register: ifcfg_files | |
- name: Perform final regex on ifcfg files | |
replace: | |
path: "{{ item[0].path }}" | |
regexp: "{{ item[1] }}" | |
replace: "{{ item[1] | replace(src_prefix, dst_prefix) }}" | |
with_nested: | |
- "{{ ifcfg_files.files }}" | |
- "{{ src_interfaces }}" | |
loop_control: | |
label: "{{ item[1] }} -> {{ item[0].path }}" | |
- name: Replace interface name in TripleO configuration files | |
tags: | |
- tripleo | |
block: | |
- name: Check if undercloud.conf exists | |
stat: | |
path: "{{ undercloud_conf }}" | |
register: undercloud_conf_stat | |
- name: Replace interface name in undercloud.conf | |
replace: | |
path: "{{ undercloud_conf }}" | |
regexp: "{{ item }}" | |
replace: "{{ item | replace(src_prefix, dst_prefix) }}" | |
when: undercloud_conf_stat.stat.exists | |
with_items: "{{ src_interfaces }}" | |
- name: Check if os-net-config's config.json exists | |
stat: | |
path: "{{ osnet_conf }}" | |
register: osnet_conf_stat | |
- name: Replace interface name in config.json | |
replace: | |
path: "{{ osnet_conf }}" | |
regexp: "{{ item }}" | |
replace: "{{ item | replace(src_prefix, dst_prefix) }}" | |
when: osnet_conf_stat.stat.exists | |
with_items: "{{ src_interfaces }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment