0

Inventory looks like:

[host-1]
h1    ansible_ssh_private_key_file="*/*/*"    ansible_host=a.b.c.d
h2    ansible_ssh_private_key_file="*/*/*"    ansible_host=a.b.c.t

[host-2]
h3    ansible_ssh_private_key_file="*/*/*"    ansible_host=a.b.c.r

I want to use ansible_host oh 'h2' in templates but I am getting ansible.errors.AnsibleUndefinedVariable error. How to resolve it?

I have already tried these:

1. "{{hostvars['host-2'][h2][ansible_host]}}"
2. "{{hostvars[host-2][h2][ansible_host]}}"
3. "{{hostvars['host-2'][h2].ansible_host}}"
4. "{{hostvars[groups['host-2'][h2]].ansible_host}}"
7wick
  • 411
  • 4
  • 17

2 Answers2

1

Please take it as a hint:

- name: host
  debug: msg="{{hostvars['h2']['ansible_host']}}"
itiic
  • 3,284
  • 4
  • 20
  • 31
1

Correct syntax

"{{ hostvars['h2']['ansible_host'] }}"

, or

"{{ hostvars.h2.ansible_host }}"

Hint: Take a look at "{{ hostvars }}" and "{{ groups }}".

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63