Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (2024)

What is the difference between ansible_hostname vs inventory_hostname vs ansible_fqdn in a practical example with Ansible Playbook?

Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (1)

March 11, 2022

Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (2)

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 vs inventory_hostname vs ansible_fqdn

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

The Best Resources For Ansible

Certifications

  • Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (4)Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (5)Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription

Video Course

Printed Book

eBooks

demo

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
---- name: hostnames demo 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
foo.example.com ansible_host=192.168.0.190[all:vars]ansible_connection=sshansible_user=devopsansible_ssh_private_key_file=~/.ssh/id_rsa

execution

ansible-pilot $ ansible-playbook -i ansible\ statements/inventory ansible\ statements/hostnames.ymlPLAY [hostnames demo] *****************************************************************************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": "demo"}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=0ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i ansible\ statements/inventory ansible\ statements/hostnames.ymlPLAY [hostnames demo] *****************************************************************************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": "demo"}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=0ansible-pilot $

before execution

ansible-pilot $ ssh [emailprotected][devops@demo ~]$ uname -aLinux demo.example.com 4.18.0-348.el8.x86_64 #1 SMP Mon Oct 4 12:17:22 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux[devops@demo ~]$ uname -ndemo.example.com[devops@demo ~]$ hostnamedemo.example.com[devops@demo ~]$

Recap

Now you know more about the Ansible internal variables ansible_hostname, inventory_hostname and ansible_fqdn.You know how to use it based on your use case.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (22)

Donate

Want to keep this project going? Please donate

Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn (2024)
Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5639

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.