Skip to content

Commit 6ce9dfc

Browse files
committed
Add hifis.netplan role
Signed-off-by: Norman Ziegner <[email protected]>
1 parent 3f45604 commit 6ce9dfc

File tree

16 files changed

+759
-9
lines changed

16 files changed

+759
-9
lines changed

.github/labeler.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ gitlab_runner:
1616
- "roles/gitlab_runner/**"
1717
- "molecule/gitlab_runner/**"
1818
- ".github/workflows/gitlab_runner.yml"
19+
netplan:
20+
- changed-files:
21+
- any-glob-to-any-file:
22+
- "roles/netplan/**"
23+
- "molecule/netplan/**"
24+
- ".github/workflows/netplan.yml"
1925
ssh_keys:
2026
- changed-files:
2127
- any-glob-to-any-file:

.github/workflows/netplan.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: Helmholtz Centre for Environmental Research (UFZ)
2+
# SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
---
7+
name: "hifis.toolkit.netplan"
8+
9+
on:
10+
pull_request:
11+
paths:
12+
- '.github/workflows/netplan.yml'
13+
- 'roles/netplan/**'
14+
- 'molecule/netplan/**'
15+
- 'Pipfile'
16+
- 'Pipfile.lock'
17+
push:
18+
branches:
19+
- "main"
20+
tags:
21+
- "v*.*.*"
22+
paths:
23+
- '.github/workflows/netplan.yml'
24+
- 'roles/netplan/**'
25+
- 'molecule/netplan/**'
26+
- 'Pipfile'
27+
- 'Pipfile.lock'
28+
schedule:
29+
- cron: "0 0 * * *"
30+
31+
jobs:
32+
33+
test:
34+
name: "Run Molecule tests."
35+
runs-on: "ubuntu-24.04"
36+
env:
37+
PY_COLORS: 1
38+
ANSIBLE_FORCE_COLOR: 1
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
image:
43+
- "ghcr.io/hifis-net/ubuntu-systemd:20.04"
44+
- "ghcr.io/hifis-net/ubuntu-systemd:22.04"
45+
46+
steps:
47+
- name: "Check out the codebase."
48+
uses: "actions/checkout@v4"
49+
with:
50+
path: "ansible_collections/hifis/toolkit"
51+
52+
- name: "Prepare the job environment."
53+
uses: "./ansible_collections/hifis/toolkit/.github/workflows/prepare-action"
54+
55+
# https://github.com/ansible/molecule/issues/3806
56+
- name: "Help molecule to find the dependencies"
57+
run: |
58+
mkdir -p /home/runner/.ansible
59+
ln -s /home/runner/work/ansible-collection-toolkit/ansible-collection-toolkit/ansible_collections/hifis/toolkit/roles \
60+
/home/runner/.ansible/roles
61+
62+
- name: "Create Podman network required for testing"
63+
run: "podman network create --subnet 10.123.0.0/24 netplan_network"
64+
65+
- name: "Run Molecule tests."
66+
run: "pipenv run molecule test -s netplan"
67+
env:
68+
MOLECULE_IMAGE: "${{ matrix.image }}"
69+
working-directory: "ansible_collections/hifis/toolkit"

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ansible-lint = "~=24.5.0"
1414
molecule = "~=24.2.0"
1515
molecule-plugins = {extras = ["podman"], version = "~=23.5.3"}
1616
reuse = "~=3.0.2"
17+
netaddr = "~=1.2.1"
1718

1819
[packages]
1920
ansible = "~=9.5.1"

Pipfile.lock

Lines changed: 17 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SPDX-License-Identifier: Apache-2.0
1010
[![Latest release](https://img.shields.io/github/v/release/hifis-net/ansible-collection-toolkit)](https://github.com/hifis-net/ansible-collection-toolkit/releases)
1111
[![hifis.gitlab_runner](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/gitlab_runner.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/gitlab_runner.yml)
1212
[![hifis.keepalived](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/keepalived.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/keepalived.yml)
13+
[![hifis.netplan](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/netplan.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/netplan.yml)
1314
[![hifis.ssh_keys](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/ssh_keys.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/ssh_keys.yml)
1415
[![hifis.unattended_upgrades](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/unattended_upgrades.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/unattended_upgrades.yml)
1516
[![hifis.zammad](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/zammad.yml/badge.svg)](https://github.com/hifis-net/ansible-collection-toolkit/actions/workflows/zammad.yml)
@@ -31,7 +32,7 @@ software engineers, but not exclusively. The following use cases are supported:
3132
* [**Keepalived**](roles/keepalived)
3233
* OS-related:
3334
* [**unattended-upgrades**](roles/unattended_upgrades)
34-
* [netplan](https://github.com/hifis-net/ansible-role-netplan) (**coming soon!**)
35+
* [**netplan**](roles/netplan)
3536
* distribute authorized [**SSH keys**](role/ssh_keys) to users
3637

3738
## Looking for the unattended_upgrades role?

molecule/netplan/converge.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: Helmholtz Centre for Environmental Research (UFZ)
2+
# SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
---
7+
- name: "Converge"
8+
hosts: "all"
9+
vars:
10+
netplan_ethernets:
11+
- interface_name: "eth0"
12+
dhcp4: "no"
13+
gateway4: "10.123.0.1"
14+
addresses:
15+
- "10.123.0.10/24"
16+
nameservers:
17+
addresses:
18+
- "8.8.8.8"
19+
- "9.9.9.9"
20+
search:
21+
- "domain.local"
22+
- "domain.name"
23+
tasks:
24+
- name: "Include netplan_role"
25+
ansible.builtin.include_role:
26+
name: "hifis.toolkit.netplan"
27+
28+
...

molecule/netplan/molecule.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: Helmholtz Centre for Environmental Research (UFZ)
2+
# SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
---
7+
dependency:
8+
name: "galaxy"
9+
driver:
10+
name: "podman"
11+
platforms:
12+
- name: "instancenetplan"
13+
image: "${MOLECULE_IMAGE:-ghcr.io/hifis-net/ubuntu-systemd:22.04}"
14+
pre_build_image: true
15+
override_command: false
16+
privileged: true
17+
systemd: true
18+
tty: true
19+
extra_opts:
20+
- "--network=netplan_network"
21+
provisioner:
22+
name: "ansible"
23+
playbooks:
24+
prepare: "prepare.yml"
25+
converge: "converge.yml"
26+
verify: "verify.yml"
27+
verifier:
28+
name: "ansible"
29+
30+
...

molecule/netplan/prepare.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: Helmholtz Centre for Environmental Research (UFZ)
2+
# SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
---
7+
- name: "Prepare"
8+
hosts: "all"
9+
tasks:
10+
- name: "Install dependencies"
11+
ansible.builtin.apt:
12+
name:
13+
- "sudo" # for `become` privilege escalation
14+
- "iproute2" # for gathering network facts
15+
- "udev" # needed for netplan
16+
update_cache: true
17+
state: "present"
18+
19+
...

molecule/netplan/verify.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: Helmholtz Centre for Environmental Research (UFZ)
2+
# SPDX-FileCopyrightText: Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
---
7+
- name: "Verify"
8+
hosts: "all"
9+
gather_facts: true
10+
vars:
11+
netplan_ethernets:
12+
- interface_name: "eth0"
13+
dhcp4: "no"
14+
gateway4: "10.123.0.1"
15+
addresses:
16+
- "10.123.0.10/24"
17+
nameservers:
18+
addresses:
19+
- "8.8.8.8"
20+
- "9.9.9.9"
21+
search:
22+
- "domain.local"
23+
- "domain.name"
24+
tasks:
25+
- name: "Check network settings if no DHCP is set."
26+
when: "netplan_ethernets is defined and netplan_ethernets | length > 0 and netplan_ethernets[0]['dhcp4'] == 'no'"
27+
block:
28+
- name: "Store actual and target network settings."
29+
ansible.builtin.set_fact:
30+
actual_interface_name: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['interface'] }}"
31+
target_interface_name: "{{ netplan_ethernets[0]['interface_name'] }}"
32+
actual_ip_addresses: "{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'] | ansible.utils.ipaddr('address') }}"
33+
target_ip_address: "{{ netplan_ethernets[0]['addresses'][0] | ansible.utils.ipaddr('address') }}"
34+
actual_subnet: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['netmask'] }}"
35+
target_subnet: "{{ netplan_ethernets[0]['addresses'][0] | ansible.utils.ipaddr('netmask') }}"
36+
37+
- name: "Check that network settings are correct."
38+
ansible.builtin.assert:
39+
that:
40+
- "actual_ip_addresses | ansible.utils.ipaddr('bool')"
41+
- "target_ip_address | ansible.utils.ipaddr('bool')"
42+
- "actual_interface_name == target_interface_name"
43+
- "target_ip_address in actual_ip_addresses"
44+
- "actual_subnet == target_subnet"
45+
46+
...

0 commit comments

Comments
 (0)