From f2321e1f453f4f3fc9b8271a0dbe948bc22ece33 Mon Sep 17 00:00:00 2001 From: Adyrem Date: Sat, 16 May 2026 04:54:01 +0200 Subject: [PATCH] Persist static IPs via Ansible to survive VM reboots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit common role: add nmcli task that ensures the static IP, gateway, and DNS are configured in NetworkManager — idempotent, only applies when network_gateway is defined. Fixes Gitea going offline after reboot because the NM connection had reverted to DHCP. group_vars/infra: add network vars (10.10.1.0/24, gw 10.10.1.1) group_vars/claude_code_vms: add network vars (10.10.2.0/24, gw 10.10.2.1) also fix ProxyJump to use adyrem@:2222 (root login was disabled) Co-Authored-By: Claude Sonnet 4.6 --- .../group_vars/claude_code_vms/vars.yml | 7 ++++++- ansible/inventory/group_vars/infra/vars.yml | 5 +++++ ansible/roles/common/tasks/main.yml | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ansible/inventory/group_vars/claude_code_vms/vars.yml b/ansible/inventory/group_vars/claude_code_vms/vars.yml index 3944200..2f0d839 100644 --- a/ansible/inventory/group_vars/claude_code_vms/vars.yml +++ b/ansible/inventory/group_vars/claude_code_vms/vars.yml @@ -1,2 +1,7 @@ ansible_user: adyrem -ansible_ssh_common_args: "-o ProxyJump=root@192.168.1.10 -o StrictHostKeyChecking=accept-new" +ansible_ssh_common_args: "-o ProxyJump=adyrem@192.168.1.10:2222 -o StrictHostKeyChecking=accept-new" + +network_interface: ens18 +network_prefix: 24 +network_gateway: 10.10.2.1 +network_dns: 10.10.1.2 diff --git a/ansible/inventory/group_vars/infra/vars.yml b/ansible/inventory/group_vars/infra/vars.yml index 2c8b1ce..e18a1d2 100644 --- a/ansible/inventory/group_vars/infra/vars.yml +++ b/ansible/inventory/group_vars/infra/vars.yml @@ -1 +1,6 @@ ansible_user: adyrem + +network_interface: ens18 +network_prefix: 24 +network_gateway: 10.10.1.1 +network_dns: 10.10.1.2 diff --git a/ansible/roles/common/tasks/main.yml b/ansible/roles/common/tasks/main.yml index 636488f..3b540e1 100644 --- a/ansible/roles/common/tasks/main.yml +++ b/ansible/roles/common/tasks/main.yml @@ -16,6 +16,26 @@ key: "{{ admin_ssh_key }}" state: present +- name: Configure static IP via NetworkManager + ansible.builtin.shell: | + CURRENT=$(nmcli -t -f IP4.ADDRESS dev show {{ network_interface }} 2>/dev/null | cut -d: -f2) + EXPECTED="{{ ansible_host }}/{{ network_prefix }}" + if [ "$CURRENT" != "$EXPECTED" ]; then + CONN=$(nmcli -t -f NAME,DEVICE con show | grep ":{{ network_interface }}$" | cut -d: -f1 | head -1) + nmcli con mod "$CONN" \ + ipv4.method manual \ + ipv4.addresses "$EXPECTED" \ + ipv4.gateway {{ network_gateway }} \ + ipv4.dns {{ network_dns }} + nmcli con up "$CONN" + echo changed + else + echo ok + fi + register: _net + changed_when: "'changed' in _net.stdout" + when: network_gateway is defined + - name: Install base packages community.general.pacman: name: