What's the difference between inventory_hostname and ansible_hostname

前端 未结 1 655
野趣味
野趣味 2021-01-31 02:20

All I could find was this from the docs:

Additionally, inventory_hostname is the name of the hostname as configured in Ansible’s inventory ho

相关标签:
1条回答
  • 2021-01-31 02:52
    • 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.

    Which one should you use?

    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.

    Important: To use ansible_hostname, you need to gather facts:

    gather_facts: true
    

    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
    
    0 讨论(0)
提交回复
热议问题