--- - name: Skip static IP configuration when disabled ansible.builtin.meta: end_host when: not (netplan_static_enabled | default(false) | bool) - name: Require netplan_static_address ansible.builtin.assert: that: - netplan_static_address is defined - netplan_static_address | length > 0 fail_msg: "netplan_static_address is required when netplan_static_enabled=true (example: 10.0.0.6/24)." - name: Detect default interface ansible.builtin.shell: "ip route show default | awk '/^default/ {print $5; exit}'" register: _netplan_default_iface changed_when: false when: (netplan_static_iface | default('') | length) == 0 - name: Set effective interface ansible.builtin.set_fact: netplan_static_iface_effective: >- {{ (netplan_static_iface | default('')) if (netplan_static_iface | default('') | length) > 0 else (_netplan_default_iface.stdout | default('') | trim) }} - name: Require detected interface ansible.builtin.assert: that: - netplan_static_iface_effective | length > 0 fail_msg: "Unable to detect default interface for static IP. Set netplan_static_iface explicitly (e.g., eth0)." - name: Detect default gateway ansible.builtin.shell: "ip route show default | awk '/^default/ {print $3; exit}'" register: _netplan_default_gw changed_when: false when: (netplan_static_gateway4 | default('') | length) == 0 - name: Set effective gateway ansible.builtin.set_fact: netplan_static_gateway4_effective: >- {{ (netplan_static_gateway4 | default('')) if (netplan_static_gateway4 | default('') | length) > 0 else (_netplan_default_gw.stdout | default('') | trim) }} - name: Detect current DNS servers ansible.builtin.shell: "awk '/^nameserver[[:space:]]+/ {print $2}' /etc/resolv.conf | head -n 5" register: _netplan_dns_detect changed_when: false when: (netplan_static_nameservers | default([])) | length == 0 - name: Set effective DNS servers ansible.builtin.set_fact: netplan_static_nameservers_effective: >- {{ (netplan_static_nameservers | default([])) if ((netplan_static_nameservers | default([])) | length) > 0 else (_netplan_dns_detect.stdout_lines | default([])) }} - name: Write netplan static config (NetworkManager) ansible.builtin.template: src: 99-ansible-static.yaml.j2 dest: /etc/netplan/99-ansible-static.yaml owner: root group: root mode: "0644" register: _netplan_cfg - name: Validate netplan config ansible.builtin.command: netplan generate changed_when: false when: _netplan_cfg is changed - name: Apply netplan config ansible.builtin.command: netplan apply changed_when: false when: _netplan_cfg is changed