FreeKB - Ansible inventory_hostname_short magic variable
Ansible - inventory_hostname_short magic variable

There are a number of different magic variable will return the hostname of a managed node.

Or, the lookup pipe hostname plugin can be used to return the hostname of your control node (that's your Ansible server). 

For example, let's say you have the following playbook. 

---
- hosts: all
  tasks:
    - debug:
        msg: ansible_hostname = {{ ansible_hostname }}

    - debug:
        msg: ansible_play_batch = {{ ansible_play_batch }}

    - debug:
        msg: ansible_play_hosts_all = {{ ansible_play_hosts_all }}

    - debug:
        msg: ansible_limit = {{ ansible_limit }}

    - debug:
        msg: inventory_hostname = {{ inventory_hostname }}

    - debug:
        msg: inventory_hostname_short = {{ inventory_hostname_short }}

    - debug:
        msg: play_hosts = {{ play_hosts }}

 

Since the ansible_hostname will return the hostname from facts, gather_facts must be true. If gather_facts is false, something like this will be returned.

TASK [debug] 
fatal: [server1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable.
The error was: 'ansible_hostname' is undefined\n\n
The error appears to be in '/home/john.doe/testing.yml': line 18, column 7, but may\n
be elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n
  tasks:\n
    - debug:\n
      ^ here\n"}

 

Assuming gather_facts is true, running this playbook should return something like this.

TASK [debug]
ok: [server1.example.com] => {
    "msg: "ansible_hostname = server1"
}
ok: [server2.example.com] => {
    "msg: "ansible_hostname = server2"
}
ok: [server3.example.com] => {
    "msg: "ansible_hostname = server3"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg": [
        "server1.example.com",
        "server2.example.com",
        "server3.example.com"
    ]
}

TASK [debug]
ok: [server1.example.com] => {
    "msg": [
        "server1.example.com",
        "server2.example.com",
        "server3.example.com"
    ]
}

TASK [debug]
ok: [server1.example.com] => {
    "msg: "ansible_limit = server1.example.com"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg: "inventory_hostname = server1.example.com"
}
ok: [server2.example.com] => {
    "msg: "inventory_hostname = server2.example.com"
}
ok: [server3.example.com] => {
    "msg: "inventory_hostname = server3.example.com"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg: "inventory hostname_short = server1"
}
ok: [server2.example.com] => {
    "msg: "inventory hostname_short = server2"
}
ok: [server3.example.com] => {
    "msg: "inventory hostname_short = server3"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg: "play_hosts = ['server1.example.com', 'server2.example.com', 'server3.example.com']"
}
ok: [server2.example.com] => {
    "msg: "play_hosts = ['server1.example.com', 'server2.example.com', 'server3.example.com']"
}
ok: [server3.example.com] => {
    "msg: "play_hosts = ['server1.example.com', 'server2.example.com', 'server3.example.com']"
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter ad905 in the box below so that we can be sure you are a human.




Comments


July 19th, 2021 by
looks like error output ok: [server2.example.com] => { "msg: "inventory hostname_short = server3" }


July 20th, 2021 by Jeremy (moderator)
Nice catch. I got this corrected.

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |