One of my play is running on group nodes in a cluster and im setting a variable based on certain condition like set a variable as “Master” when a node is master with in the cluster.
- name: Determine master node in the cluster
hosts: cluster
gather_facts: no
tasks:
- name: Get the Master node hostname
shell: <command>
register: host_info
set_fact:
master_node: {{ host_info.stdout }}
when: host_info has "master". #Sample when condition to understand
Im need to access the host_info master_node variable from other host
- name: Add async standby node
hosts: new_node
become: yes
tasks:
- debug:
var: hostvars[item]['master_node']
loop: "{{ groups['cluster'] }}"
Inventory:
[cluster]
cluster_01
cluster_02
[new_node]
new_node_01
when i execute this the debug task shows
TASK [debug] *********************************************************************************************************************************************************************************
ok: [new_node_01] => (item=cluster_01) => {
"ansible_loop_var": "item",
"hostvars[item]['primary_node']": "cluster_01",
"item": "cluster_01"
}
ok: [new_node_01] => (item=cluster_02) => {
"ansible_loop_var": "item",
"hostvars[item]['primary_node']": "VARIABLE IS NOT DEFINED!",
"item": "cluster_02"
}
How can we access only the loop item which is defined?