The issue is with the nested Jinja2 delimiters ({{ {{ ... }} }}). In Jinja2, you cannot nest {{ }} delimiters. When you're already inside a Jinja2 expression, you don't need to use {{ }} again.
Here's the corrected version:
- name: Generate nic parameters for flannel
ansible.builtin.set_fact:
_k3s_network_flannel_ipaddress: "169.254.{{ os_network.ip4.address.split('.')[2] | default('1') }}.{{ os_network.ip4.address.split('.')[3] | default('1') }}"
_k3s_network_flannel_ipprefix: "{{ os_network.ip4.cidr | default('24') }}"
What Changed
- Removed the nested
{{ }} around os_network.ip4.address inside the already-open Jinja2 expressions
- The expression is now:
{{ os_network.ip4.address.split('.')[2] | default('1') }} instead of {{ {{ os_network.ip4.address }}.split('.')[2] | default('1') }}
How It Works
os_network.ip4.address.split('.') splits the IP address into an array
[2] and [3] access the third and fourth octets
| default('1') provides a fallback value if the split fails or the index doesn't exist
- The result creates an IP like
169.254.X.Y where X and Y come from the original IP's last two octets
Alternative Approach
If you want to be more explicit or handle edge cases better, you could also do:
- name: Generate nic parameters for flannel
ansible.builtin.set_fact:
_k3s_network_flannel_ipaddress: "169.254.{{ (os_network.ip4.address | default('0.0.1.1')).split('.')[2] }}.{{ (os_network.ip4.address | default('0.0.1.1')).split('.')[3] }}"
_k3s_network_flannel_ipprefix: "{{ os_network.ip4.cidr | default('24') }}"
This ensures that os_network.ip4.address has a default value before trying to split it.