ventory_hostname和ansible_hostname有什么区别 [英] What's the difference between inventory_hostname and ansible_hostname

查看:1306
本文介绍了ventory_hostname和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 hostname ansible_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_hostnameansible_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发现. Ansible ssh进入了主机并收集了一些事实.作为事实的一部分,它还会发现存储在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 DNS
    • ansible_hostname - As discovered by ansible. Ansible ssh's into the host and gathers some facts. As part of the fact, it also discovers its hostname which is stored in ansible_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆