AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible inventory_hostname vs ansible_hostname vs ansible_fqdn (Explained) — Video Tutorial
Difference between Ansible inventory_hostname, ansible_hostname, ansible_host, and ansible_fqdn variables. When to use each, practical examples, common mistakes explained.
What You'll Learn
- What is the difference between ansible_hostname vs inventory_hostname?
- ansible_hostname and ansible_fqdn
- inventory_hostname
- Playbook
- code
- execution
- idempotency
- before execution
- Conclusion
- Quick Comparison
Full Tutorial Content
What is the difference between ansible_hostname vs inventory_hostname?
These two ansible internal variables sometimes confuse one for another but they're fundamentally different.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
ansible_hostname and ansible_fqdn
Read from the target machine hostname from the facts:
- `ansible_hostname` read the hostname from the facts collected during the `gather_facts`
- Same as the `uname -n` or `hostname` command-line
- Need `gather_facts` enabled, otherwise the `ansible_facts` variable would be unavailable to use in your playbook
- Same as hostname of the target host
- As this is based on the `gather_facts` step. ansible_hostname not available in ad-hoc command
inventory_hostname
Read from Ansible inventory or hosts files:
- `inventory_hostname` read the hostname from the inventory configuration or the hosts file. Could be different from the hostname configuration of the remote system. It could be only a name on the controller machine
- `inventory_hostname` is always available to use in your playbook.
- Could be different from the hostname of the target host
- Available for both playbook and ad-hoc command
Playbook
Let me show you the difference between `ansible_hostname` vs `inventory_hostname` vs `ansible_fqdn` internal variables in a simple Ansible Playbook.
code
- hostnames.yml
```yaml
---
- name: hostnames Playbook
hosts: all
gather_facts: true
tasks:
- name: print inventory_hostname
ansible.builtin.debug:
var: inventory_hostname
- name: print ansible_hostname
ansible.builtin.debug:
var: ansible_hostname
- name: print ansible_fqdn
ansible.builtin.debug:
var: ansible_fqdn
```
- inventory
```yaml
foo.example.com ansible_host=192.168.0.190
[all:vars]
ansible_connection=ssh
ansible_user=devops
ansible_ssh_private_key_file=~/.ssh/id_rsa
```
execution
```bash
ansible-pilot $ ansible-playbook -i ansible\ statements/inventory ansible\ statements/hostnames.yml
PLAY [hostnames Playbook] *****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [foo.example.com]
TASK [print inventory_hostname] *******************************************************************
ok: [foo.example.com] => {
"inventory_hostname": "foo.example.com"
}
TASK [print ansible_hostname] *********************************************************************
ok: [foo.example.com] => {
"ansible_hostname": "Playbook"
}
TASK [print ansible_fqdn] *************************************************************************
ok: [foo.example.com] => {
"ansible_fqdn": "demo.example.com"
}
PLAY RECAP ****************************************************************************************
foo.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency
```
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 5 min
- Category: troubleshooting
Read the full written article: Ansible inventory_hostname vs ansible_hostname vs ansible_fqdn (Explained)
Related Video Tutorials
- ansible.cfg Configuration File: Complete Settings Guide (2026) — Complete guide to ansible.cfg configuration file. Every section explained — defaults, privilege_escalation, ssh_connection, inventory, galaxy. Plus ansible-config list, dump, view, and init commands with examples.
- Ansible Magic Variables: Complete Reference with Examples — Complete reference for Ansible magic variables. Use inventory_hostname, hostvars, groups, play_hosts, ansible_facts, and special variables in playbooks.
- Ansible vs ansible-core: Package Differences Explained (2026) — Understand the difference between ansible and ansible-core packages. Compare contents, versions, installation, and choose the right package for your needs.
- Ansible Fix 'VARIABLE IS NOT DEFINED' Error: Undefined Variables — Fix Ansible VARIABLE IS NOT DEFINED error. Troubleshoot undefined variables, missing facts, gather_facts issues, and use default filter for safe access.
- Ansible Multi-Line Strings: Literal (|) & Folded (>) Block Scalars Guide — How to break strings over multiple lines in Ansible YAML. Use literal block scalar (|) to preserve newlines and folded block scalar (>) to join lines. Practical playbook examples.
- Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — How to use Ansible's playbook_dir magic variable to get the current playbook directory path. Includes role_path, inventory_dir, and all magic variables with examples.