Skip to content

bug: Fix keepalived template to handle missing IPv6 #362

@aleaz

Description

@aleaz

Summary

The keepalived.conf.j2 template fails on hosts where IPv6 is disabled or not configured. Both unicast_src_ip and interface fields access ansible_default_ipv6 attributes directly.

  • Jinja2 evaluates both sides of the default() expression before continuing, so empty IPv6 dictionaries cause:
object of type 'HostVarsVars' has no attribute 'ansible_default_ipv6'

Proposed Solution:

  • Use safe checks (is defined / get()) for .address and .interface.
  • Optionally allow overriding via inventory variables like rke2_keepalived_src_ip and rke2_keepalived_interface.
  • For example, for interface:
interface {{ ansible_default_ipv4.interface | default(ansible_default_ipv6.get('interface', 'eth0')) }}
  • This ensures the role works on IPv4-only hosts and prevents template rendering failures.

Issue Type

Bug Report

Ansible Version

ansible [core 2.19.0]
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.13/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.13.7 (main, Aug 15 2025, 12:34:02) [GCC 15.2.1 20250813] (/usr/bin/python)
  jinja version = 3.1.6
  pyyaml version = 6.0.2 (with libyaml v0.2.5)

Steps to Reproduce

- name: RKE2 - Server/Agent - Install
  hosts: [rke2_server, rke2_agent]
  gather_facts: true
  become: true
  roles:
    - role: rke2

Expected Results

  • The playbook completes successfully.
  • keepalived.conf is generated correctly.
  • unicast_src_ip and interface fields are set using IPv4 (or fallback variables if configured).

Actual Results

TASK [rke2 : Create keepalived config file] ******************************************************************************
fatal: [rke2-server-01-c1.intranet]: FAILED! => {"changed": false, "msg": "Task failed: object of type 'HostVarsVars' has no attribute 'ansible_default_ipv6'"}

Debug output:

"ansible_default_ipv4": { "address": "172.22.48.150", "alias": "eth0", "broadcast": "172.22.48.255", "gateway": "172.22.48.1", "interface": "eth0", "macaddress": "bc:24:11:b4:44:2f", "mtu": 1500, "netmask": "255.255.255.0", "network": "172.22.48.0", "prefix": "24", "type": "ether" }
"ansible_default_ipv6": {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions