Description
What happened?
Verify whether main_access_ip is present inside the hostvars dictionary when using the --limit option.
executing cluster.yml playbook with --limit(only etcd,kube_control_plane) getting below error on Task
Task: kubernetes/preinstall : Hosts | create hosts list from inventory
The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'main_access_ip'. 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'main_access_ip'
Please Confirm the presence of the main_access_ip variable in hostvars before access the variable.
Missing if condition to check the presence of main_access_ip in hostvars.
{{ hostvars[item]['main_access_ip'] }} {{ hostvars[item]['ansible_hostname'] | default(item) }}.{{ dns_domain }} {{ hostvars[item]['ansible_hostname'] | default(item) }}
PR https://github.com/kubernetes-sigs/kubespray/pull/11953
What did you expect to happen?
Need to verify the presence of main_access_ip in hostvars items before access the vars to avoid undefined variable error with limit option.
How can we reproduce it (as minimally and precisely as possible)?
Adding new etcd node to existing cluster and execute cluster.yml with --limit option.
OS
Rocky Linux 8
Version of Ansible
ansible --version
ansible [core 2.16.14]
ansible python module location = /usr/local/lib/python3.10/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.10.16 (main, Mar 17 2025, 23:21:14) [GCC 10.2.1 20210110] (/usr/local/bin/python3.10)
jinja version = 3.1.6
libyaml = True
Version of Python
python version = 3.10.16
Version of Kubespray (commit)
Network plugin used
calico
Full inventory with variables
"all": [
master-1.domain.local,
master-2.domain.local,
master-3.domain.local
node1.domain.local
],
"etcd": [
master-1.domain.local,
master-2.domain.local,
master-3.domain.local
],
"k8s_cluster": [
master-1.domain.local,
master-2.domain.local,
master-3.domain.local
node1.domain.local
],
"kube_control_plane": [
master-1.domain.local,
master-2.domain.local,
master-3.domain.local
],
"kube_node": [
node1.domain.local
],
Command used to invoke ansible
ansible-playbook --become --become-user=root cluster.yml --limit=etcd.kube_control_plane
Output of ansible run
TASK [kubernetes/preinstall : Hosts | create hosts list from inventory] ********
task path: /roles/kubernetes/preinstall/tasks/0090-etchosts.yml:2
fatal: [master-1.domain.local--> localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'main_access_ip'. 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'main_access_ip'
The error appears to be in '/roles/kubernetes/preinstall/tasks/0090-etchosts.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: Hosts | create hosts list from inventory
^ here
Anything else we need to know
No response