7wick
7wick

Reputation: 421

How to access ip_address from inventory for a specific host?

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}}"

Upvotes: 0

Views: 492

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Correct syntax

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

, or

"{{ hostvars.h2.ansible_host }}"

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

Upvotes: 1

itiic
itiic

Reputation: 3712

Please take it as a hint:

- name: host
  debug: msg="{{hostvars['h2']['ansible_host']}}"

Upvotes: 1

Related Questions