Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: adapt role to deal with cloud-init/netplan #46

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions roles/net/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Net role

(Re-)configures host networking using ifupdown, optionally renaming interfaces
based on MAC address.

Note: this role disables cloud-init handling of network configuration.

See /usr/share/doc/udev/README.Debian.gz for details on how interface renaming
is handled.

## Troubleshooting

As distributions adopt "modern" automatic configuration tools things might
break, in particular udev rules tend to get clobbered.

These commands can help debug why the changes are not being applied:

```
SYSTEMD_LOG_LEVEL=debug \
udevadm test-builtin net_setup_link /sys/class/net/<iface>
```

```
SYSTEMD_LOG_LEVEL=debug \
udevadm test -a add /sys/class/net/<iface>
```

Note: the `udevadm test` command seems to apply changes and not just test.
22 changes: 11 additions & 11 deletions roles/net/handlers/main.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# vim:ts=2:sw=2:et:ai:sts=2
---
- name: _tina_restart_udev
ansible.builtin.service:
name: udev
state: restarted
listen: _tina_restart_net_all

- name: _tina_reconfigure_network
ansible.builtin.shell: # noqa: risky-shell-pipe
cmd: >-
ifdown --all --force;
for dev in `ip -o link show | sed -n 's/[0-9]*: \(\S\+\):.*/\1/p'`; do
networkctl reload;
ifdown --verbose --all --force;
for dev in `ls /sys/class/net`; do
ip link set dev $dev down;
done;
udevadm trigger -c add -s net;
udevadm control --reload;
udevadm trigger --action add --subsystem-match net;
ip addr flush scope global;
ifup --all --force;
listen: _tina_restart_net_all
ifup --verbose --all --force;
changed_when: true

- name: _tina_update_initramfs
ansible.builtin.command:
cmd: update-initramfs -u -k all
changed_when: true
69 changes: 48 additions & 21 deletions roles/net/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,63 @@
# vim:ts=2:sw=2:et:ai:sts=2
---

- name: Check cloud-init presence
ansible.builtin.stat:
path: /etc/cloud/cloud.cfg
register: _cloud_init_cfg
check_mode: false

- name: Disable cloud-init network configuration
ansible.builtin.copy:
dest: /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
mode: '0644'
content: |
network: {config: disabled}
when: _cloud_init_cfg.stat.exists

- name: Find cloud-init generated netplan configuration files
ansible.builtin.find:
paths:
- /etc/netplan
patterns:
- ??-cloud-init.yaml
register: _netplan_cfg
when: _cloud_init_cfg.stat.exists

- name: Find netplan generated configuration files
ansible.builtin.find:
paths:
- /run/systemd/network
- /run/udev/rules.d
patterns:
- ??-netplan-*.link
- ??-netplan-*.network
- ??-netplan-*.rules
register: _networkd_cfg
when: _cloud_init_cfg.stat.exists

- name: Remove generated configuration files
ansible.builtin.file:
path: '{{ item.path }}'
state: absent
loop: '{{ _networkd_cfg.files + _netplan_cfg.files }}'
when: _cloud_init_cfg.stat.exists

- name: Ensure ifupdown is present
ansible.builtin.apt:
ansible.builtin.package:
name: ifupdown
state: present

- name: Gather facts about services
ansible.builtin.service_facts:
# Run normally even in check mode.
check_mode: false
when: ansible_service_mgr == 'systemd'

- name: Stop and disable conflicting services
ansible.builtin.service:
name: '{{ item }}'
state: stopped
enabled: false
masked: true
when: ansible_service_mgr == 'systemd' and item in services
with_items:
- systemd-network-generator.service
- systemd-networkd.service
notify: _tina_reconfigure_network

- name: Create udev persistent rules
ansible.builtin.template:
src: 70-persistent-net.rules.j2
dest: /etc/udev/rules.d/70-persistent-net.rules
owner: root
group: root
mode: '0644'
notify: _tina_restart_net_all
notify:
- _tina_reconfigure_network
- _tina_update_initramfs
when: net__use_udev_persistent_rules | bool

- name: Configure interfaces
Expand All @@ -41,7 +67,8 @@
owner: root
group: root
mode: '0644'
notify: _tina_reconfigure_network
notify:
- _tina_reconfigure_network
when: net__interfaces

- name: Apply configuration
Expand Down