
There are a number of different magic variable will return the hostname of a managed node (e.g. the target system).
- ansible_hostname will return the hostname of a managed node from facts
- ansible_host can be used when you have ansible_host defined in your default hosts file or your own inventory file.
- ansible_play_batch will return the hostname of a managed node, just like the Linux hostname command, in the form of an array of strings.
- ansible_play_hosts_all will return the hostname of a managed node, just like the Linux hostname command, in the form of an array of strings.
- ansible_limit will return the hostname passed to the -l or --limit command line option.
- inventory_hostname will return the hostname of a manage node as listed in your default hosts file or your own inventory file.
- inventory_hostname_short will return the short hostname of a manage node as listed in your default hosts file or your own inventory file.
- play_hosts will return the hostname of a managed node, just like the Linux hostname command, in the form of an array of strings. However, play_hosts is deprecated and will be removed from ansible-core in version 2.23 and the recommendation is to use ansible_play_batch instead of play_hosts.
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:
var: ansible_hostname
- debug:
var: ansible_host
- debug:
var: ansible_play_batch
- debug:
var: ansible_play_hosts_all
- debug:
var: inventory_hostname
- debug:
var: inventory_hostname_short
- debug:
var: play_hosts
- debug:
var: ansible_limit
when: ansible_limit is defined
...
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, ansible_hostname should return something like this.
ok: [server1.example.com] => {
"ansible_hostname": "server1"
}
ok: [server2.example.com] => {
"ansible_hostname": "server2"
}
ok: [server3.example.com] => {
"ansible_hostname": "server3"
}
ansible_host should return something like this.
ok: [server1.example.com] => {
"ansible_host": "server1"
}
ok: [server2.example.com] => {
"ansible_host": "server2"
}
ok: [server3.example.com] => {
"ansible_host": "server3"
}
ansible_play_batch should return something like this.
ok: [server1.example.com] => {
"ansible_play_batch": [
"server1.example.com",
"server2.example.com",
"server3.example.com"
]
}
ansible_play_batch_all should return something like this.
ok: [server1.example.com] => {
"ansible_play_batch_all": [
"server1.example.com",
"server2.example.com",
"server3.example.com"
]
}
If the --limit option was used, ansible_limit should return something like this.
ok: [server1.example.com] => {
"ansible_limit": "server1.example.com"
}
inventory_hostname should return something like this.
ok: [server1.example.com] => {
"inventory_hostname": "server1.example.com"
}
ok: [server2.example.com] => {
"inventory_hostname": "server2.example.com"
}
ok: [server3.example.com] => {
"inventory_hostname": "server3.example.com"
}
inventory_hostname_short should return something like this.
ok: [server1.example.com] => {
"msg: "inventory hostname_short = server1"
}
ok: [server2.example.com] => {
"msg: "inventory hostname_short = server3"
}
ok: [server3.example.com] => {
"msg: "inventory hostname_short = server3"
}
play_hosts should return something like this.
ok: [server1.example.com] => {
"play_hosts": [
"server1.example.com",
"server2.example.com",
"server3.example.com"
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at