From bed8aaaeb0b63b2072fbd030d76f8f928432e7b8 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelhalim Date: Mon, 11 Aug 2025 00:32:09 +0200 Subject: Add network ipv4 configurations --- host_vars/archlinux.local.yml | Bin 29 -> 104 bytes host_vars/archlinux.local.yml.example | 3 ++- host_vars/rpi5.local.yml | Bin 29 -> 104 bytes host_vars/rpi5.local.yml.example | 3 ++- host_vars/ubuntu.local.yml | Bin 29 -> 104 bytes host_vars/ubuntu.local.yml.example | 3 ++- roles/network/meta/argument_specs.yml | 6 ++++++ roles/network/tasks/main.yml | 8 ++++++++ roles/network/templates/eth0-connection.nmconnection.j2 | 7 ++++++- roles/network/templates/wifi-connection.nmconnection.j2 | 6 ++++++ 10 files changed, 32 insertions(+), 4 deletions(-) diff --git a/host_vars/archlinux.local.yml b/host_vars/archlinux.local.yml index f8331458..779ac449 100644 Binary files a/host_vars/archlinux.local.yml and b/host_vars/archlinux.local.yml differ diff --git a/host_vars/archlinux.local.yml.example b/host_vars/archlinux.local.yml.example index d9e1a256..90185c62 100644 --- a/host_vars/archlinux.local.yml.example +++ b/host_vars/archlinux.local.yml.example @@ -1,2 +1,3 @@ --- -{} +network_ipv4_address: "10.0.0.201" +network_ipv4_gateway: "10.0.0.1" diff --git a/host_vars/rpi5.local.yml b/host_vars/rpi5.local.yml index f8331458..d2c64414 100644 Binary files a/host_vars/rpi5.local.yml and b/host_vars/rpi5.local.yml differ diff --git a/host_vars/rpi5.local.yml.example b/host_vars/rpi5.local.yml.example index d9e1a256..aa9af763 100644 --- a/host_vars/rpi5.local.yml.example +++ b/host_vars/rpi5.local.yml.example @@ -1,2 +1,3 @@ --- -{} +network_ipv4_address: "10.0.0.202" +network_ipv4_gateway: "10.0.0.1" diff --git a/host_vars/ubuntu.local.yml b/host_vars/ubuntu.local.yml index f8331458..596d68e9 100644 Binary files a/host_vars/ubuntu.local.yml and b/host_vars/ubuntu.local.yml differ diff --git a/host_vars/ubuntu.local.yml.example b/host_vars/ubuntu.local.yml.example index d9e1a256..defd152d 100644 --- a/host_vars/ubuntu.local.yml.example +++ b/host_vars/ubuntu.local.yml.example @@ -1,2 +1,3 @@ --- -{} +network_ipv4_address: "10.0.0.203" +network_ipv4_gateway: "10.0.0.1" diff --git a/roles/network/meta/argument_specs.yml b/roles/network/meta/argument_specs.yml index 33561c8d..32eed955 100644 --- a/roles/network/meta/argument_specs.yml +++ b/roles/network/meta/argument_specs.yml @@ -24,3 +24,9 @@ argument_specs: type: "list" description: "The network services to enable" elements: "str" + network_ipv4_address: + type: "str" + description: "The network IP(4) address to configure when provided" + network_ipv4_gateway: + type: "str" + description: "The network IP(4) gateway to configure when provided" diff --git a/roles/network/tasks/main.yml b/roles/network/tasks/main.yml index c45cb6da..f96b2c41 100644 --- a/roles/network/tasks/main.yml +++ b/roles/network/tasks/main.yml @@ -5,6 +5,14 @@ - name: "Include OS-specific tasks" ansible.builtin.include_tasks: "install-{{ ansible_os_family | lower }}.yml" +- name: "Validate network_ipv4_* params" + ansible.builtin.assert: + that: + - network_ipv4_address is defined + - network_ipv4_gateway is defined + fail_msg: "network_ipv4_address and network_ipv4_gateway are required together" + when: network_ipv4_address is defined or network_ipv4_gateway is defined + - name: "(chroot): Ensure network_services are enabled" ansible.builtin.command: cmd: "systemctl enable {{ item }}" diff --git a/roles/network/templates/eth0-connection.nmconnection.j2 b/roles/network/templates/eth0-connection.nmconnection.j2 index b5cbe276..ae573cf1 100644 --- a/roles/network/templates/eth0-connection.nmconnection.j2 +++ b/roles/network/templates/eth0-connection.nmconnection.j2 @@ -2,12 +2,17 @@ id=ethernet type=ethernet autoconnect=true -interface-name=eth0 [ethernet] [ipv4] +{% if network_ipv4_address is defined %} +method=manual +addresses={{ network_ipv4_address }} +gateway={{ network_ipv4_gateway }} +{% else %} method=auto +{% endif %} [ipv6] addr-gen-mode=default diff --git a/roles/network/templates/wifi-connection.nmconnection.j2 b/roles/network/templates/wifi-connection.nmconnection.j2 index 9fffbd12..deec573d 100644 --- a/roles/network/templates/wifi-connection.nmconnection.j2 +++ b/roles/network/templates/wifi-connection.nmconnection.j2 @@ -14,7 +14,13 @@ key-mgmt=wpa-psk psk={{ network_wifi_pass }} [ipv4] +{% if network_ipv4_address is defined %} +method=manual +addresses={{ network_ipv4_address }} +gateway={{ network_ipv4_gateway }} +{% else %} method=auto +{% endif %} [ipv6] addr-gen-mode=default -- cgit v1.2.3