Strategies are a way to control play execution on the number of hosts given in a playbook. By default, plays run with a linear strategy, in which all hosts will run each task before any host starts the next task.
Let’s understand the strategy with a basic example of a playbook defined with multiple hosts. Consider below playbook:
- name: 'strategy demo' hosts: webserver1,sqlserver1 strategy: linear tasks: - name: 'first task' apt: name='apache2' state='present' - name: 'second task' command: touch /tmp/strategy_task2.txt
In the above example, playbook has defined two tasks and each task will run on each host in parallel. First task will start execution on webserver1 and sqlserver1 in parallel. Let’s consider if apache2 is already installed on webserver1 then webserver1 has to wait until apache2 gets installed on sqlserver1. Webserver1 has dependency on sqlserver1 and without waiting for installation to complete on sqlserver1, the webserver1 cannot proceed with ‘second task’ this behaviour of application installation on different hosts is known as LINEAR Strategy.
If there is a requirement to install or configure an application independently on different hosts without any dependency on host, that is also achievable in Ansible and is known as FREE Strategy. In free strategy, the host does not depend on each other to complete tasks for e.g. webserver will not wait to complete the task on sqlserver.
- name: 'strategy demo' hosts: webserver1,sqlserver1 strategy: free tasks: - name: 'first task' apt: name='apache2' state='present' - name: 'second task' command: touch /tmp/strategy_task2.txt
Let’s consider a case, where the admin has to configure/install an application on a huge list of hosts (might be 100 or 1000). In such a scenario, will the playbook start the first task execution on all 100 hosts in parallel? NO. The execution of tasks on all hosts will not happen in parallel. Ansible controllers take the control of the host and do the execution in parts.
There is a configuration file for ansible defined on path /etc/ansible/ansible.cfg, it has a fork value which is 5 by default. So, to handle 1000 host ansible controllers, first-run playbooks on the first 5 hosts, and so on. Fork value is editable but make sure if you give a large value then ansible host should have good CPU and memory to make execution fast and unbreakable.
Fork value can also be configured in the playbook as shown in the below example
- name: 'strategy demo' hosts: webserver1,sqlserver1 serial: 1 tasks: - name: 'first task' apt: name='apache2' state='present' - name: 'second task' command: touch /tmp/strategy_task2.txt - name: '3rd task' command: sleep 30
In the above example, as serial value is defined to 1, so the first playbook will get executed on Webserver1 and then on sqlserver1.
This way we can plan and strategize the playbook execution to achieve complex tasks in large organizations.