Search code examples
ansibleansible-inventory

Take hosts from hosts-inventory in ascending order - Ansible


I have 100 hosts in hosts-inventory. The hosts-inventory looks like this:

[workstations-new]
de001pc[100:201] ansible_connection=ssh ansible_ssh_user=root

When I run an ansible-playbook command:

ansible-playbook playbooks/linux/uptime.yml -i hosts-inventory --extra-vars "hosts=workstations-new" -k

is running it on all hosts, but it doesn't take the hosts in ascending order. It takes the hosts in an non order (103, 14, 102, 100, ...)

Example:

TASK: [uptime machine] ******************************************************** 
changed: [de001pc103]
changed: [de001pc104]
changed: [de001pc102]
changed: [de001pc100]
changed: [de001pc101]
changed: [de001pc107]
changed: [de001pc106]
changed: [de001pc108]
changed: [de001pc109]
changed: [de001pc110]
changed: [de001pc116]
changed: [de001pc112]
................................................

How can I run the command to take the hosts in ascending order?


Solution

  • By default, Ansible will try to manage many machines referenced in a play in parallel. Some machines might be replying slightly faster than others. That's why you see 103 before 101.

    If you want your managed machines to be processed strictly in sequential order you should use serial: 1 parameter. However, keep in mind that you will significantly slow down your playbook execution speed. If I'm doing my math correctly, on 100 machines that's 20 times slower.