如何从特定主机的清单中访问 ip_address?

How to access ip_address from inventory for a specific host?

库存如下:

[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

我想在模板中使用ansible_host哦'h2'但是我得到ansible.errors.AnsibleUndefinedVariable错误。如何解决?

我已经尝试过这些:

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

请多多指教:

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

语法正确

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

,或

"{{ hostvars.h2.ansible_host }}"

提示:看看 "{{ hostvars }}""{{ groups }}"