
Let's say you have 4 managed nodes (e.g. target systems) defined in your default hosts file or your own inventory file, server1.example.com through server4.example.com.
all:
hosts:
server1.example.com:
server2.example.com:
server3.example.com:
server4.example.com:
You might assume that the tasks in your playbooks would be run against the managed nodes in the order they are listed in your default hosts file or your own inventory file. However, this does not always seem to be the case. In fact, according to https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_strategies.html:
The ‘inventory’ order does NOT equate to the order in which hosts/groups are defined in the inventory source file, but the ‘order in which a selection is returned from the compiled inventory’.
This is a backwards compatible option and while reproducible it is not normally predictable.
Due to the nature of inventory, host patterns, limits, inventory plugins and the ability to allow multiple sources it is almost impossible to return such an order.
For simple cases this might happen to match the file definition order, but that is not guaranteed.
Of course, you can experiment with this, and see the order. In this example, the tasks are being run against server1.example, then server2.example, then server3.example.com and finally server4.example.com.
PLAY [all]
TASK [create /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=1 changed=1 unreachable=0 failed=0
server2.example.com : ok=1 changed=1 unreachable=0 failed=0
server3.example.com : ok=1 changed=1 unreachable=0 failed=0
server4.example.com : ok=1 changed=1 unreachable=0 failed=0
If you need to be absolutely certain that tasks on run on nodes in a certain order, you may want to do something like this.
---
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
when: inventory_hostname is search 'server1'
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
when: inventory_hostname is search 'server2'
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
when: inventory_hostname is search 'server3'
- hosts: all
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
when: inventory_hostname is search 'server4'
...
Or, you can try to use order.
order: inventory is the default order, where each task in your playbooks will be run against the managed nodes in the order they are listed in your default hosts file or your own inventory file.
---
- hosts: all
order: inventory
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
order: reverse_inventory - each task in your playbooks will be run against the managed nodes in the reverse order they are listed in your default hosts file or your own inventory file.
---
- hosts: all
order: reverse_inventory
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
order: sorted - each task in your playbooks will be run against the managed nodes in alphabetical order.
---
- hosts: all
order: sorted
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
order: reverse_sorted - each task in your playbooks will be run against the managed nodes in reverse alphabetical order.
---
- hosts: all
order: reverse_sorted
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
order: shuffle - each task in your playbooks will be run against the managed nodes in random order.
---
- hosts: all
order: shuffle
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
Did you find this article helpful?
If so, consider buying me a coffee over at