There are 4 strategies that can be used.
- debug (same as linear except debug info will be returned)
- free (each managed node will go through the tasks independently, with the goal of completing the playbook as quickly as possible)
- host_pinned (set a limit on the number of managed hosts that will execute the tasks in a playbook simultaneously)
- linear (complete each task on each managed node before moving onto next task)
The ansible-doc command can be used to display documentation on the each strategy.
ansible-doc -t strategy --snippet host_pinned
Let's consider the scenario where Ansible is being used to perform two (or more) tasks on managed nodes (e.g. target systems). For example, let's say the file module is being used to create two directories, /tmp/foo and /tmp/bar.
---
- hosts: all
tasks:
- name: mkdir /tmp/foo
file:
path: /tmp/foo
state: directory
- name: mkdir /tmp/bar
file:
path: /tmp/bar
state: directory
...
By default, Ansible uses the linear strategy, which means that each task is performed against all of the managed nodes before moving onto the next task. Let's say you have 5 managed nodes in your inventory, server1 through server5. In this example, this would mean that the /tmp/foo directory would be created on server1 through server5 before moving onto the task to create the /tmp/bar directory on server1 through server5.
The host_pinned strategy is also linear, except that once a certain number of managed nodes are processing the tasks in the playbook, additional managed nodes will need to wait (like their in queue) for one of the managed nodes to complete all of the tasks in the playbook to free up a slot for the next managed host to start processing the tasks in the playbook.
To use the host_pined strategy, you could include strategy: host_pinned in your playbook.
---
- hosts: all
strategy: host_pinned
tasks:
- name: mkdir /tmp/foo
file:
path: /tmp/foo
state: directory
- name: mkdir /tmp/bar
file:
path: /tmp/bar
state: directory
...
Or you could set the the strategy to host_pinned in ansible.cfg, which would make host_pinned the default strategy for all playbooks.
strategy = host_pinned
Or you could set the ANSIBLE_STRATEGY environmental variable to host_pinned for your current shell.
export ANSIBLE_STRATEGY=host_pinned
Let's say you invoke your playbook with 3 forks using the --forks command line option. In this scenario, 3 managed nodes will start to go through the tasks in the playbook, and the remaining managed nodes will not start processing the tasks in the playbook until one of the prior managed nodes completes all of the tasks in the playbook.
ansible-playbook example.yml --forks 3
Did you find this article helpful?
If so, consider buying me a coffee over at