ventory_hostname和ansible_hostname有什么区别 [英] What's the difference between inventory_hostname and ansible_hostname
问题描述
inventory_hostname
- As configured in the ansible inventory file (eg:/etc/ansible/hosts
). It can be an IP address or a name that can be resolved by the DNSansible_hostname
- As discovered byansible
. Ansiblessh
's into the host and gathers some facts. As part of the fact, it also discovers its hostname which is stored inansible_hostname
.
hostvars
是一个字典,其中包含每个清单主机的条目.如果要访问主机信息,则需要使用inventory_hostname
.如果要使用/打印主机上配置的主机名,则应使用ansible_hostname
,因为很可能在清单文件中使用IP.
hostvars
is a dictionary which has an entry for each inventory host. If you want to access host information, you need to use the inventory_hostname
. If you want to use/print the name of the host as configured on the host, you should use ansible_hostname
since most likely the IP will be used in the inventory file.
重要:要使用ansible_hostname
,您需要收集事实:
Important: To use ansible_hostname
, you need to gather facts:
gather_facts: true
否则,您将收到一条消息,提示未定义ansible_hostname
.
Otherwise, you will get a message that ansible_hostname
is not defined.
"ansible_hostname": "VARIABLE IS NOT DEFINED!"
与一位主持人一起尝试以了解差异
Try this with one host to understand the differences
tasks:
- debug: var=inventory_hostname
- debug: var=ansible_hostname
- debug: var=hostvars
这篇关于ventory_hostname和ansible_hostname有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!