首页 > 解决方案 > 如何从特定主机的清单中访问 ip_address?

问题描述

库存看起来像:

[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 oh '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}}"

标签: ansibleansible-inventoryansible-template

解决方案


请作为提示:

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

推荐阅读