What is the difference between `inventory_hostname`, `ansible_hostname` and `ansible_host`?

IntermediateConceptInventory
Answer
  • inventory_hostname — the name of the host as written in the inventory (alias).
  • ansible_host — the address Ansible connects to; defaults to the inventory name.
  • ansible_hostname (or ansible_facts['hostname']) — the machine's actual hostname, discovered by fact gathering.

They can all differ. A host named web1 in inventory with ansible_host=10.0.1.11 may report ip-10-0-1-11 as its real hostname. Use inventory_hostname for logic about the inventory, and facts for what the machine says about itself.

All Ansible questions Roadmap: Ansible

More on Inventory