Persist static IPs via Ansible to survive VM reboots
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
ansible_user: adyrem
|
||||
|
||||
network_interface: ens18
|
||||
network_prefix: 24
|
||||
network_gateway: 10.10.1.1
|
||||
network_dns: 10.10.1.2
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user