feat(roles): add the systemd_hardening and network_interfaces roles
Copied from the environment repository's roles and scrubbed for the Collection: galaxy namespace mortis, molecule scenarios call the roles by their FQCN and take their paths from the workflow, README examples use mortis.kamino.* and documentation addresses, repository-local references (issue numbers, ADRs, inventory paths) are dropped. network_interfaces loses its Legacy name guard and the molecule play that tested it: the guard only protected the in-repo rename, and a new consumer never had the old names. mock_roles in .ansible-lint lists both FQCNs. Not verified locally: the molecule scenarios (they run on the kamino-molecule runner in CI) and ansible-test sanity. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
21dda27b00
commit
cf8f4b5bf0
32 files changed
+1044
No files matched your search
@@ -0,0 +1,60 @@
|
||||
# network_interfaces
|
||||
|
||||
Renders `/etc/network/interfaces` from a list of manual interfaces and bridges, validates it with `ifreload`, and restores the backup if validation fails. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
|
||||
|
||||
## Supported platforms
|
||||
|
||||
Debian-based hosts using `ifupdown2` (`ifreload`), such as Proxmox hosts. The molecule scenario (`molecule/default/`, CI only) runs on Ubuntu 24.04 without `ifupdown2`: the validate and reload commands are stubs, so it tests the rendered file, the backup, the single reload and the restore after a failing validation. It does not test `ifreload`, so no Debian release or `ifupdown2` version is verified by a test.
|
||||
|
||||
## Requirements and run order
|
||||
|
||||
- `ifupdown2` must be installed, because the validate and apply commands call `ifreload`.
|
||||
- Use it by its fully qualified name, `mortis.kamino.network_interfaces`.
|
||||
- Run it with `serial: 1` and the most important host last, so a bad change is caught on another host first. It rewrites the host's network configuration, so run it before roles that depend on the network.
|
||||
- Gathered facts are not read. The tasks use `become` where they write.
|
||||
- `network_interfaces_layout` normally comes from inventory (group_vars or host_vars), so each host can have its own layout.
|
||||
|
||||
## Role variables
|
||||
|
||||
| Variable | Default | Required | Description |
|
||||
|---|---|---|---|
|
||||
| `network_interfaces_layout` | `manual: []`, `bridges: []` | no | `manual` is a list of interface names. `bridges` is a list of mappings with `name`, `method`, `address`, `netmask`, `gateway`, `ports`, `stp`, `fd`, `vlan_aware`, `vlan_ids`. |
|
||||
| `network_interfaces_file` | `/etc/network/interfaces` | no | File rewritten. |
|
||||
| `network_interfaces_backup` | `/etc/network/interfaces.bak` | no | Backup path. |
|
||||
| `network_interfaces_validate_command` | `ifreload -s -a` | no | Validation command. |
|
||||
| `network_interfaces_apply_command` | `ifreload -a` | no | Reload command, run by the handler. |
|
||||
|
||||
The `address` sub-option is typed `raw` in the spec so that an inventory can template it, for example from `ansible_host`.
|
||||
|
||||
## Tags
|
||||
|
||||
None. The role does not tag its tasks, so only a play-level tag applies.
|
||||
|
||||
## Example play
|
||||
|
||||
```yaml
|
||||
- hosts: hypervisors
|
||||
serial: 1
|
||||
roles:
|
||||
- role: mortis.kamino.network_interfaces
|
||||
vars:
|
||||
network_interfaces_layout:
|
||||
manual: [nic0, nic1]
|
||||
bridges:
|
||||
- name: vmbr0
|
||||
method: static
|
||||
address: 192.0.2.10
|
||||
netmask: 255.255.255.0
|
||||
gateway: 192.0.2.1
|
||||
ports: nic0
|
||||
vlan_aware: true
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- The template replaces the whole file: any stanza not described by `network_interfaces_layout` is dropped. Interfaces carried by other consumers (for example a bridge that carries a router VM's WAN) must be listed.
|
||||
- Only a content difference triggers work. Idempotent runs change nothing.
|
||||
- On change the order is: copy the current file to `network_interfaces_backup`, write the new file (notifies `Reload Interfaces`), run the validate command, flush handlers. Any failure in that block restores the backup and fails with "Invalid network configuration detected. Previous config restored." The restore does not reload the interfaces.
|
||||
- The validate command runs after the file is written, and the reload runs right after it. A configuration that passes validation but breaks connectivity is not detected.
|
||||
- With `method: static`, `address` and `netmask` are required by the template.
|
||||
- `no_log` is not used: the role handles no secrets.
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
network_interfaces_layout:
|
||||
manual: []
|
||||
bridges: []
|
||||
network_interfaces_file: /etc/network/interfaces
|
||||
network_interfaces_backup: /etc/network/interfaces.bak
|
||||
|
||||
network_interfaces_validate_command: "ifreload -s -a"
|
||||
network_interfaces_apply_command: "ifreload -a"
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- name: Reload Interfaces
|
||||
become: true
|
||||
ansible.builtin.command: "{{ network_interfaces_apply_command }}"
|
||||
changed_when: true
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: Manage /etc/network/interfaces
|
||||
description:
|
||||
- >-
|
||||
Reads the interfaces file, renders it from C(network_interfaces_layout), and when the content differs backs up the current file, writes the new
|
||||
one, validates it and reloads. A failed validation restores the backup and fails the play.
|
||||
- Needs C(ifupdown2), because validation and reload use C(ifreload).
|
||||
options:
|
||||
network_interfaces_layout:
|
||||
type: dict
|
||||
default: {manual: [], bridges: []}
|
||||
description:
|
||||
- >-
|
||||
Desired network layout. The file is fully replaced with a loopback stanza, one C(iface <name> inet manual) per C(manual) entry and one
|
||||
stanza per bridge. Both keys are optional when the variable is set.
|
||||
options:
|
||||
manual:
|
||||
type: list
|
||||
elements: str
|
||||
description: Interface names configured as C(inet manual), without C(auto).
|
||||
bridges:
|
||||
type: list
|
||||
elements: dict
|
||||
description: Bridges, rendered in order with C(auto <name>).
|
||||
options:
|
||||
name:
|
||||
type: str
|
||||
required: true
|
||||
description: Bridge name, for example C(vmbr0).
|
||||
method:
|
||||
type: str
|
||||
description: Address method. The template default is C(manual). With C(static), C(address) and C(netmask) are read unconditionally.
|
||||
address:
|
||||
type: raw
|
||||
description: >-
|
||||
IP address, only used when C(method) is C(static). Typed raw so that an inventory can template it, for
|
||||
example from C(ansible_host).
|
||||
netmask:
|
||||
type: str
|
||||
description: Netmask, only used when C(method) is C(static).
|
||||
gateway:
|
||||
type: str
|
||||
description: Default gateway, only used when C(method) is C(static) and the key is present.
|
||||
ports:
|
||||
type: str
|
||||
description: Value of C(bridge_ports). The template default is C(none).
|
||||
stp:
|
||||
type: str
|
||||
description: Value of C(bridge_stp). The template default is C(off). Quote C("off"), because unquoted YAML turns it into a boolean.
|
||||
fd:
|
||||
type: raw
|
||||
description: Value of C(bridge_fd), written only when C(stp) is not C(off). The template default is 2.
|
||||
vlan_aware:
|
||||
type: bool
|
||||
description: Adds C(bridge_vlan_aware yes) and C(bridge_vids). The template default is false.
|
||||
vlan_ids:
|
||||
type: str
|
||||
description: Value of C(bridge_vids) when C(vlan_aware) is true. The template default is C(2-4094).
|
||||
network_interfaces_file:
|
||||
type: path
|
||||
default: /etc/network/interfaces
|
||||
description:
|
||||
- File that is read, compared and rewritten.
|
||||
network_interfaces_backup:
|
||||
type: path
|
||||
default: /etc/network/interfaces.bak
|
||||
description:
|
||||
- Where the current file is copied before it is replaced, and restored from when validation fails.
|
||||
network_interfaces_validate_command:
|
||||
type: str
|
||||
default: ifreload -s -a
|
||||
description:
|
||||
- Command run after the new file is written, in syntax-check mode. A failure triggers the restore.
|
||||
network_interfaces_apply_command:
|
||||
type: str
|
||||
default: ifreload -a
|
||||
description:
|
||||
- Command run by the C(Reload Interfaces) handler, which the role flushes right after validation.
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
galaxy_info:
|
||||
role_name: network_interfaces
|
||||
namespace: mortis
|
||||
author: Defame1297
|
||||
license: GPL-3.0-or-later
|
||||
description: Render /etc/network/interfaces for Proxmox hosts from manual and bridge definitions, validate with ifreload and restore the backup on failure.
|
||||
min_ansible_version: "2.19"
|
||||
platforms:
|
||||
- name: Debian
|
||||
versions:
|
||||
- all
|
||||
galaxy_tags:
|
||||
- network
|
||||
- bridge
|
||||
- proxmox
|
||||
dependencies: []
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
# ifupdown2 and its ifreload are not in the image, and a real reload could cut the
|
||||
# container's own network, so the validate command is `true` and the apply command appends
|
||||
# to a marker file. The role itself (read, render, compare, back up, write, validate,
|
||||
# flush handlers) runs unchanged.
|
||||
- name: Converge the role with a layout of manual interfaces and bridges
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
network_interfaces_validate_command: /bin/true
|
||||
network_interfaces_apply_command: '/bin/sh -c "echo reloaded >> /var/tmp/network-interfaces-reload.log"'
|
||||
network_interfaces_layout:
|
||||
manual: [eno1]
|
||||
bridges:
|
||||
- name: vmbr0
|
||||
method: static
|
||||
address: 10.0.0.2
|
||||
netmask: 255.255.255.0
|
||||
gateway: 10.0.0.1
|
||||
ports: eno1
|
||||
stp: "off"
|
||||
vlan_aware: true
|
||||
- name: vmbr1
|
||||
stp: "on"
|
||||
fd: 5
|
||||
roles:
|
||||
- role: mortis.kamino.network_interfaces
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
|
||||
# writable cgroup and the firewall tools need NET_ADMIN, nothing else.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: network-interfaces-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
capabilities:
|
||||
- NET_ADMIN
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
verifier:
|
||||
name: ansible
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
# The role slurps the interfaces file before it compares, so the file must exist, and the
|
||||
# image has no /etc/network. Both files get a legacy body that differs from anything the
|
||||
# template renders, so the converge has a change to make and a backup to take.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Create /etc/network
|
||||
ansible.builtin.file:
|
||||
path: /etc/network
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
|
||||
- name: Seed the interfaces files with a legacy body
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ item }}"
|
||||
content: |
|
||||
# legacy seed
|
||||
auto lo
|
||||
iface lo inet loopback
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
loop:
|
||||
- /etc/network/interfaces
|
||||
- /etc/network/interfaces.rollback
|
||||
@@ -0,0 +1,102 @@
|
||||
---
|
||||
# The rendered file, the backup of the legacy body and the single reload. The last play is the
|
||||
# role's lock-out protection: a failing validation must put the previous file back. It
|
||||
# changes the container, so it lives here and not in converge.yml, where the idempotence
|
||||
# run would see it change twice. It works on its own copy of the file.
|
||||
- name: Verify the converged layout
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the interfaces file, its backup and the reload marker
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ item }}"
|
||||
loop:
|
||||
- /etc/network/interfaces
|
||||
- /etc/network/interfaces.bak
|
||||
- /var/tmp/network-interfaces-reload.log
|
||||
register: network_interfaces_verify_files
|
||||
|
||||
- name: The file holds the loopback, the manual interface and both bridges
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- lines is contains('iface lo inet loopback')
|
||||
- lines is contains('iface eno1 inet manual')
|
||||
- lines is contains('auto vmbr0')
|
||||
- lines is contains('iface vmbr0 inet static')
|
||||
- lines is contains(' address 10.0.0.2')
|
||||
- lines is contains(' netmask 255.255.255.0')
|
||||
- lines is contains(' gateway 10.0.0.1')
|
||||
- lines is contains(' bridge_ports eno1')
|
||||
- lines is contains(' bridge_stp off')
|
||||
- lines is contains(' bridge_vlan_aware yes')
|
||||
- lines is contains(' bridge_vids 2-4094')
|
||||
- lines is contains('auto vmbr1')
|
||||
- lines is contains('iface vmbr1 inet manual')
|
||||
- lines is contains(' bridge_ports none')
|
||||
- lines is contains(' bridge_stp on')
|
||||
- lines is contains(' bridge_fd 5')
|
||||
- lines | select('search', 'bridge_fd') | list | length == 1
|
||||
- lines | select('search', 'bridge_vlan_aware') | list | length == 1
|
||||
- "'legacy seed' not in content"
|
||||
quiet: true
|
||||
vars:
|
||||
content: "{{ network_interfaces_verify_files.results[0].content | b64decode }}"
|
||||
lines: "{{ content.split('\n') }}"
|
||||
|
||||
- name: The backup is the legacy body and the reload ran once
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'# legacy seed' in (network_interfaces_verify_files.results[1].content | b64decode)"
|
||||
- "'vmbr0' not in (network_interfaces_verify_files.results[1].content | b64decode)"
|
||||
- (network_interfaces_verify_files.results[2].content | b64decode).split('\n') | select | list | length == 1
|
||||
quiet: true
|
||||
|
||||
- name: A failing validation restores the previous file
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
network_interfaces_file: /etc/network/interfaces.rollback
|
||||
network_interfaces_backup: /etc/network/interfaces.rollback.bak
|
||||
network_interfaces_validate_command: /bin/false
|
||||
# A handler notified by the write is still pending when the block is rescued, and it
|
||||
# runs at the end of the play, so it must not touch the reload marker.
|
||||
network_interfaces_apply_command: /bin/true
|
||||
network_interfaces_layout:
|
||||
manual: [eno1]
|
||||
bridges:
|
||||
- name: vmbr9
|
||||
tasks:
|
||||
- name: Read the file before
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/network/interfaces.rollback
|
||||
register: network_interfaces_verify_before
|
||||
|
||||
- name: Apply the role with a validation that fails
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.network_interfaces
|
||||
- name: Fail when the invalid configuration was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The role reverted and refused
|
||||
ansible.builtin.assert:
|
||||
that: "'Previous config restored' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: Read the file and the backup after
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ item }}"
|
||||
loop:
|
||||
- /etc/network/interfaces.rollback
|
||||
- /etc/network/interfaces.rollback.bak
|
||||
register: network_interfaces_verify_after
|
||||
|
||||
- name: The previous file is back and the bad one is gone
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- network_interfaces_verify_after.results[0].content == network_interfaces_verify_before.content
|
||||
- network_interfaces_verify_after.results[1].content == network_interfaces_verify_before.content
|
||||
- "'vmbr9' not in (network_interfaces_verify_after.results[0].content | b64decode)"
|
||||
quiet: true
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
- name: Apply network configuration safely
|
||||
become: true
|
||||
when: network_interfaces_changed
|
||||
block:
|
||||
- name: Backup current interfaces file
|
||||
ansible.builtin.copy:
|
||||
src: "{{ network_interfaces_file }}"
|
||||
dest: "{{ network_interfaces_backup }}"
|
||||
remote_src: true
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
|
||||
- name: Write new interfaces configuration
|
||||
ansible.builtin.copy:
|
||||
content: "{{ network_interfaces_rendered }}"
|
||||
dest: "{{ network_interfaces_file }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Reload Interfaces
|
||||
|
||||
- name: Validate network configuration
|
||||
ansible.builtin.command: "{{ network_interfaces_validate_command }}"
|
||||
changed_when: false
|
||||
|
||||
- name: Trigger network reload
|
||||
ansible.builtin.meta: flush_handlers
|
||||
|
||||
rescue:
|
||||
- name: Revert interfaces
|
||||
ansible.builtin.import_tasks: revert.yml
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Read current network configuration
|
||||
ansible.builtin.import_tasks: read.yml
|
||||
|
||||
- name: Render desired network configuration
|
||||
ansible.builtin.import_tasks: render.yml
|
||||
|
||||
- name: Apply network configuration if changed
|
||||
ansible.builtin.import_tasks: apply.yml
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- name: Read current interfaces config
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ network_interfaces_file }}"
|
||||
register: network_interfaces_current
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Render interfaces template
|
||||
ansible.builtin.set_fact:
|
||||
network_interfaces_rendered: "{{ lookup('template', 'interfaces.j2') }}"
|
||||
|
||||
- name: Detect interface configuration changes
|
||||
ansible.builtin.set_fact:
|
||||
network_interfaces_changed: >-
|
||||
{{ network_interfaces_rendered != (network_interfaces_current.content | b64decode) }}
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- name: Restore previous interfaces configuration
|
||||
ansible.builtin.copy:
|
||||
src: "{{ network_interfaces_backup }}"
|
||||
dest: "{{ network_interfaces_file }}"
|
||||
remote_src: true
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
|
||||
- name: Abort due to invalid network configuration
|
||||
ansible.builtin.fail:
|
||||
msg: "Invalid network configuration detected. Previous config restored."
|
||||
@@ -0,0 +1,36 @@
|
||||
# Managed by Ansible – DO NOT EDIT
|
||||
|
||||
# Loopback
|
||||
auto lo
|
||||
iface lo inet loopback
|
||||
|
||||
# Manual interfaces
|
||||
{% for iface in network_interfaces_layout.manual | default([]) %}
|
||||
iface {{ iface }} inet manual
|
||||
{% endfor %}
|
||||
|
||||
# Bridges
|
||||
{% for br in network_interfaces_layout.bridges | default([]) %}
|
||||
auto {{ br.name }}
|
||||
iface {{ br.name }} inet {{ br.method | default('manual') }}
|
||||
{% if br.method | default('manual') == 'static' %}
|
||||
address {{ br.address }}
|
||||
netmask {{ br.netmask }}
|
||||
{% if br.gateway is defined %}
|
||||
gateway {{ br.gateway }}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if br.ports is defined %}
|
||||
bridge_ports {{ br.ports }}
|
||||
{% else %}
|
||||
bridge_ports none
|
||||
{% endif %}
|
||||
bridge_stp {{ br.stp | default('off') }}
|
||||
{% if br.stp | default('off') != 'off' %}
|
||||
bridge_fd {{ br.fd | default(2) }}
|
||||
{% endif %}
|
||||
{% if br.vlan_aware | default(false) %}
|
||||
bridge_vlan_aware yes
|
||||
bridge_vids {{ br.vlan_ids | default('2-4094') }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
Reference in new issue
Block a user