Skip to content

Commit f7983c5

Browse files
author
Melnyk Roman
committed
fix: avoid crash when host has no default_ipv6 fact
Jinja's default() filter evaluates its fallback argument eagerly, even when the primary value already resolved. On hosts with no IPv6 default route, ansible_facts has no default_ipv6 key at all, so chaining ['address']/.address/.interface onto the missing key raises: 'dict object' has no attribute 'default_ipv6' ...even though default_ipv4 would have been used. This hit role argument validation (rke2_api_ip default in argument_specs.yml) on any IPv4-only host, before the role even started. Replace the dict['key']/.key chains with .get(key, {}).get(subkey), which returns None instead of raising when the key is absent, and use `or` instead of default() so None/empty still falls through to the IPv6 branch.
1 parent 0ef24b4 commit f7983c5

4 files changed

Lines changed: 6 additions & 6 deletions

File tree

defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rke2_ha_mode_kubevip: false
2222
# Kubernetes API and RKE2 registration IP address. The default Address is the IPv4 of the Server/Master node.
2323
# In HA mode choose a static IP which will be set as VIP in keepalived.
2424
# Or if the keepalived is disabled, use IP address of your LB.
25-
rke2_api_ip: "{{ hostvars[groups[rke2_servers_group_name].0]['ansible_default_ipv4']['address'] | default(hostvars[groups[rke2_servers_group_name].0]['ansible_default_ipv6']['address'] ) }}"
25+
rke2_api_ip: "{{ hostvars[groups[rke2_servers_group_name].0].get('ansible_default_ipv4', {}).get('address') or hostvars[groups[rke2_servers_group_name].0].get('ansible_default_ipv6', {}).get('address') }}"
2626

2727
# optional option for RKE2 Server to listen on a private IP address & port
2828
# rke2_api_private_ip:

meta/argument_specs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ argument_specs:
3939

4040
rke2_api_ip:
4141
type: str
42-
default: "{{ hostvars[groups[rke2_servers_group_name].0].ansible_facts['default_ipv4']['address'] | default(hostvars[groups[rke2_servers_group_name].0].ansible_facts['default_ipv6']['address'] ) }}"
42+
default: "{{ hostvars[groups[rke2_servers_group_name].0].ansible_facts.get('default_ipv4', {}).get('address') or hostvars[groups[rke2_servers_group_name].0].ansible_facts.get('default_ipv6', {}).get('address') }}"
4343
description: "Kubernetes API and RKE2 registration IP address. The default Address is the IPv4 of the Server/Master node. In HA mode choose a static IP which will be set as VIP in keepalived. Or if the keepalived is disabled, use IP address of your LB."
4444

4545
rke2_api_private_ip:

templates/keepalived.conf.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ vrrp_script chk_rke2server {
2828
}
2929

3030
vrrp_instance VI_1 {
31-
interface {{ ansible_facts.default_ipv4.interface | default(ansible_facts.default_ipv6.interface) }}
31+
interface {{ ansible_facts.get('default_ipv4', {}).get('interface') or ansible_facts.get('default_ipv6', {}).get('interface') }}
3232
virtual_router_id 11
3333
{% if groups[rke2_servers_group_name].0 == inventory_hostname|string() %}
3434
state MASTER
@@ -41,11 +41,11 @@ vrrp_instance VI_1 {
4141
{% endif -%}
4242
{% endfor %}
4343
advert_int 1
44-
unicast_src_ip {{ ansible_facts.default_ipv4.address | default(ansible_facts.default_ipv6.address) }}
44+
unicast_src_ip {{ ansible_facts.get('default_ipv4', {}).get('address') or ansible_facts.get('default_ipv6', {}).get('address') }}
4545
unicast_peer {
4646
{% for host in groups[rke2_servers_group_name] %}
4747
{% if host|string() != inventory_hostname|string() %}
48-
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] | default(hostvars[host]['ansible_facts']['default_ipv6']['address'] ) }}
48+
{{ hostvars[host]['ansible_facts'].get('default_ipv4', {}).get('address') or hostvars[host]['ansible_facts'].get('default_ipv6', {}).get('address') }}
4949
{% endif %}
5050
{% endfor %}
5151
}

templates/kube-vip/kube-vip.yml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ spec:
3737
fieldRef:
3838
fieldPath: spec.nodeName
3939
- name: vip_interface
40-
value: "{{ rke2_interface | default(ansible_facts.default_ipv4.interface | default(ansible_facts.default_ipv6.interface)) }}"
40+
value: "{{ rke2_interface | default(ansible_facts.get('default_ipv4', {}).get('interface') or ansible_facts.get('default_ipv6', {}).get('interface')) }}"
4141
- name: port
4242
value: "{{ rke2_api_port | default('6443') }}"
4343
- name: vip_cidr

0 commit comments

Comments
 (0)