inventory_hostname 和 ansible_hostname 有什么区别 [英] What's the difference between inventory_hostname and ansible_hostname
问题描述
我只能从 文档:
此外,inventory_hostname
是 Ansible 的清单主机文件中配置的主机名的名称.当您不想依赖发现的主机名 ansible_hostname
或出于其他神秘原因时,这可能很有用.如果您有一个长 FQDN,inventory_hostname_short
还包含直到第一个句点的部分,没有域的其余部分.
Additionally,
inventory_hostname
is the name of the hostname as configured in Ansible’s inventory host file. This can be useful for when you don’t want to rely on the discovered hostnameansible_hostname
or for other mysterious reasons. If you have a long FQDN,inventory_hostname_short
also contains the part up to the first period, without the rest of the domain.
Ansible 中的 inventory_hostname
和 ansible_hostname
变量之间有什么实际区别吗?如果是这样,那么我应该使用哪一种以及何时使用?
Is there any actual difference between inventory_hostname
and ansible_hostname
variables in Ansible? If so, then which one should I use and when?
推荐答案
inventory_hostname
- 在 ansible 清单文件中配置(例如:/etc/ansible/hosts
).可以是IP地址,也可以是DNS可以解析的名称ansible_hostname
- 由ansible
发现.Ansiblessh
进入主机并收集一些事实.作为事实的一部分,它还会发现其存储在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
这篇关于inventory_hostname 和 ansible_hostname 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!