FreeKB - Ansible inventory_hostname magic variable
Ansible - inventory_hostname magic variable

The inventory_hostname magic variable will return the hostname of a manage node as listed in your default hosts file or your own inventory file. The ansible_hostname magic variable will return the hostname of a managed node (e.g. the target system), just like the Linux hostname command. Or, the following can be used to invoke this module on the control node (that's your Ansible server). 

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

---
- hosts: all
  tasks:
    - debug
        msg: Ansible hostname is {{ ansible_hostname }}

    - debug
        msg: Inventory hostname is {{ inventory_hostname }}

    - debug
        msg: Short inventory hostname is {{ inventory_hostname_short }}

 

Running this playbook could return something like this.

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

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

TASK [debug]
ok: [server1.example.com] => {
    "msg: "Short inventory hostname is server1"
}


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 3c66a in the box below so that we can be sure you are a human.




Comments

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