There are a number of different magic variable will return the hostname of a managed node.
- 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.
Or, the lookup pipe hostname plugin can be used to return the hostname of your control node (that's your Ansible server).
ansible_play_batch really only makes sense when using serial to set the number or percentage of target systems that tasks in a playbook will be run against.
Let's say you have the following playbook, which contains 2 tasks.
---
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
- name: remove /tmp/foo.txt
file:
path: /tmp/foo.txt
state: absent
...
Let's also say you have 4 target servers defined in your default hosts file or your own inventory file.
all:
hosts:
server1.example.com:
server2.example.com:
server3.example.com:
server4.example.com:
Without defining serial, the first task will run against all 4 servers, and then the second task will run against all 4 servers. The play recap should look something like this.
PLAY [all]
TASK [create /tmp/foo.txt]
changed: [server1.example.com]
changed: [server2.example.com]
changed: [server3.example.com]
changed: [server4.example.com]
TASK [remove /tmp/foo.txt]
changed: [server1.example.com]
changed: [server2.example.com]
changed: [server3.example.com]
changed: [server4.example.com]
PLAY RECAP
server1.example.com : ok=2 changed=2 unreachable=0 failed=0
server2.example.com : ok=2 changed=2 unreachable=0 failed=0
server3.example.com : ok=2 changed=2 unreachable=0 failed=0
server4.example.com : ok=2 changed=2 unreachable=0 failed=0
Now let's include serial: 2 in the playbook.
---
- hosts: all
serial: 2
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
- name: remove /tmp/foo.txt
file:
path: /tmp/foo.txt
state: absent
...
The play recap should look something like this. Notice that both tasks were first run against server1.example.com and server.example.com only, because serial was set to 2. Then the tasks were again run, against server3.example.com and server4.example.com.
PLAY [all]
TASK [create /tmp/foo.txt]
changed: [server1.example.com]
changed: [server2.example.com]
TASK [create /tmp/foo.txt]
changed: [server1.example.com]
changed: [server2.example.com]
PLAY [all]
TASK [remove /tmp/foo.txt]
changed: [server3.example.com]
changed: [server4.example.com]
TASK [remove /tmp/foo.txt]
changed: [server3.example.com]
changed: [server4.example.com]
PLAY RECAP
server1.example.com : ok=2 changed=2 unreachable=0 failed=0
server2.example.com : ok=2 changed=2 unreachable=0 failed=0
server3.example.com : ok=2 changed=2 unreachable=0 failed=0
server4.example.com : ok=2 changed=2 unreachable=0 failed=0
Additionally, serial can also take a percentage.
---
- hosts: all
serial: 25%
The ansible_play_batch variable will contain the target servers in each "batch".
---
- hosts: all
serial: 2
tasks:
- debug:
var: ansible_play_batch
...
Which should return something like this.
PLAY [all]
ok: [server1.example.com] => {
"ansible_play_batch": [
"server1.example.com",
"server2.example.com"
]
}
ok: [server2.example.com] => {
"ansible_play_batch": [
"server1.example.com",
"server2.example.com"
]
}
PLAY [all]
ok: [server3.example.com] => {
"ansible_play_batch": [
"server3.example.com",
"server4.example.com"
]
}
ok: [server4.example.com] => {
"ansible_play_batch": [
"server3.example.com",
"server4.example.com"
]
}
PLAY RECAP
server1.example.com : ok=2 changed=0 unreachable=0 failed=0
server2.example.com : ok=2 changed=0 unreachable=0 failed=0
server3.example.com : ok=2 changed=0 unreachable=0 failed=0
server4.example.com : ok=2 changed=0 unreachable=0 failed=0
Did you find this article helpful?
If so, consider buying me a coffee over at