gpt4 book ai didi

ansible - inventory_hostname 和 ansible_hostname 有什么区别

转载 作者:行者123 更新时间:2023-12-03 09:06:06 25 4
gpt4 key购买 nike

我只能从 the docs 找到这个:

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.


inventory_hostname之间是否有任何实际差异?和 ansible_hostname Ansible 中的变量?如果是这样,那么我应该使用哪一种以及何时使用?

最佳答案

  • inventory_hostname - 在 ansible list 文件中配置(例如: /etc/ansible/hosts )。可以是IP地址,也可以是DNS可以解析的名称
  • ansible_hostname - 由 ansible 发现. Ansible ssh进入主持人并收集一些事实。作为事实的一部分,它还发现了存储在 ansible_hostname 中的主机名。 .

  • 你应该使用哪一个?
    hostvars是一个字典,其中包含每个库存主机的条目。如果要访问主机信息,需要使用 inventory_hostname .如果你想使用/打印主机上配置的主机名,你应该使用 ansible_hostname因为 IP 很可能会在 list 文件中使用。

    重要 : 使用 ansible_hostname ,你需要收集事实:
    gather_facts: true

    否则,您将收到一条消息 ansible_hostname没有定义。
    "ansible_hostname": "VARIABLE IS NOT DEFINED!"

    与一位主机一起尝试以了解差异
      tasks:
    - debug: var=inventory_hostname
    - debug: var=ansible_hostname
    - debug: var=hostvars

    关于ansible - inventory_hostname 和 ansible_hostname 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45908067/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com