1.. _playbooks_strategies: 2 3Controlling playbook execution: strategies and more 4=================================================== 5 6By default, Ansible runs each task on all hosts affected by a play before starting the next task on any host, using 5 forks. If you want to change this default behavior, you can use a different strategy plugin, change the number of forks, or apply one of several keywords like ``serial``. 7 8.. contents:: 9 :local: 10 11Selecting a strategy 12-------------------- 13The default behavior described above is the :ref:`linear strategy<linear_strategy>`. Ansible offers other strategies, including the :ref:`debug strategy<debug_strategy>` (see also :ref:`playbook_debugger`) and the :ref:`free strategy<free_strategy>`, which allows each host to run until the end of the play as fast as it can:: 14 15 - hosts: all 16 strategy: free 17 tasks: 18 ... 19 20You can select a different strategy for each play as shown above, or set your preferred strategy globally in ``ansible.cfg``, under the ``defaults`` stanza:: 21 22 [defaults] 23 strategy = free 24 25All strategies are implemented as :ref:`strategy plugins<strategy_plugins>`. Please review the documentation for each strategy plugin for details on how it works. 26 27Setting the number of forks 28--------------------------- 29If you have the processing power available and want to use more forks, you can set the number in ``ansible.cfg``:: 30 31 [defaults] 32 forks = 30 33 34or pass it on the command line: `ansible-playbook -f 30 my_playbook.yml`. 35 36Using keywords to control execution 37----------------------------------- 38 39In addition to strategies, several :ref:`keywords<playbook_keywords>` also affect play execution. You can set a number, a percentage, or a list of numbers of hosts you want to manage at a time with ``serial``. Ansible completes the play on the specified number or percentage of hosts before starting the next batch of hosts. You can restrict the number of workers allotted to a block or task with ``throttle``. You can control how Ansible selects the next host in a group to execute against with ``order``. You can run a task on a single host with ``run_once``. These keywords are not strategies. They are directives or options applied to a play, block, or task. 40 41.. _rolling_update_batch_size: 42 43Setting the batch size with ``serial`` 44^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 45 46By default, Ansible runs in parallel against all the hosts in the :ref:`pattern <intro_patterns>` you set in the ``hosts:`` field of each play. If you want to manage only a few machines at a time, for example during a rolling update, you can define how many hosts Ansible should manage at a single time using the ``serial`` keyword:: 47 48 --- 49 - name: test play 50 hosts: webservers 51 serial: 3 52 gather_facts: False 53 54 tasks: 55 - name: first task 56 command: hostname 57 - name: second task 58 command: hostname 59 60In the above example, if we had 6 hosts in the group 'webservers', Ansible would execute the play completely (both tasks) on 3 of the hosts before moving on to the next 3 hosts:: 61 62 63 PLAY [webservers] **************************************** 64 65 TASK [first task] **************************************** 66 changed: [web3] 67 changed: [web2] 68 changed: [web1] 69 70 TASK [second task] *************************************** 71 changed: [web1] 72 changed: [web2] 73 changed: [web3] 74 75 PLAY [webservers] **************************************** 76 77 TASK [first task] **************************************** 78 changed: [web4] 79 changed: [web5] 80 changed: [web6] 81 82 TASK [second task] *************************************** 83 changed: [web4] 84 changed: [web5] 85 changed: [web2] 86 87 PLAY RECAP *********************************************** 88 web1 : ok=2 changed=2 unreachable=0 failed=0 89 web2 : ok=2 changed=2 unreachable=0 failed=0 90 web3 : ok=2 changed=2 unreachable=0 failed=0 91 web4 : ok=2 changed=2 unreachable=0 failed=0 92 web5 : ok=2 changed=2 unreachable=0 failed=0 93 web6 : ok=2 changed=2 unreachable=0 failed=0 94 95 96You can also specify a percentage with the ``serial`` keyword. Ansible applies the percentage to the total number of hosts in a play to determine the number of hosts per pass:: 97 98 --- 99 - name: test play 100 hosts: webservers 101 serial: "30%" 102 103If the number of hosts does not divide equally into the number of passes, the final pass contains the remainder. In this example, if you had 20 hosts in the webservers group, the first batch would contain 6 hosts, the second batch would contain 6 hosts, the third batch would contain 6 hosts, and the last batch would contain 2 hosts. 104 105You can also specify batch sizes as a list. For example:: 106 107 --- 108 - name: test play 109 hosts: webservers 110 serial: 111 - 1 112 - 5 113 - 10 114 115In the above example, the first batch would contain a single host, the next would contain 5 hosts, and (if there are any hosts left), every following batch would contain either 10 hosts or all the remaining hosts, if fewer than 10 hosts remained. 116 117You can list multiple batch sizes as percentages:: 118 119 --- 120 - name: test play 121 hosts: webservers 122 serial: 123 - "10%" 124 - "20%" 125 - "100%" 126 127You can also mix and match the values:: 128 129 --- 130 - name: test play 131 hosts: webservers 132 serial: 133 - 1 134 - 5 135 - "20%" 136 137.. note:: 138 No matter how small the percentage, the number of hosts per pass will always be 1 or greater. 139 140Restricting execution with ``throttle`` 141^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 142 143The ``throttle`` keyword limits the number of workers for a particular task. It can be set at the block and task level. Use ``throttle`` to restrict tasks that may be CPU-intensive or interact with a rate-limiting API:: 144 145 tasks: 146 - command: /path/to/cpu_intensive_command 147 throttle: 1 148 149If you have already restricted the number of forks or the number of machines to execute against in parallel, you can reduce the number of workers with ``throttle``, but you cannot increase it. In other words, to have an effect, your ``throttle`` setting must be lower than your ``forks`` or ``serial`` setting if you are using them together. 150 151Ordering execution based on inventory 152^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 153 154The ``order`` keyword controls the order in which hosts are run. Possible values for order are: 155 156inventory: 157 (default) The order provided in the inventory 158reverse_inventory: 159 The reverse of the order provided by the inventory 160sorted: 161 Sorted alphabetically sorted by name 162reverse_sorted: 163 Sorted by name in reverse alphabetical order 164shuffle: 165 Randomly ordered on each run 166 167Other keywords that affect play execution include ``ignore_errors``, ``ignore_unreachable``, and ``any_errors_fatal``. These options are documented in :ref:`playbooks_error_handling`. 168 169.. _run_once: 170 171Running on a single machine with ``run_once`` 172^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 173 174If you want a task to run only on the first host in your batch of hosts, set ``run_once`` to true on that task:: 175 176 --- 177 # ... 178 179 tasks: 180 181 # ... 182 183 - command: /opt/application/upgrade_db.py 184 run_once: true 185 186 # ... 187 188Ansible executes this task on the first host in the current batch and applies all results and facts to all the hosts in the same batch. This approach is similar to applying a conditional to a task such as:: 189 190 - command: /opt/application/upgrade_db.py 191 when: inventory_hostname == webservers[0] 192 193However, with ``run_once``, the results are applied to all the hosts. To run the task on a specific host, instead of the first host in the batch, delegate the task:: 194 195 - command: /opt/application/upgrade_db.py 196 run_once: true 197 delegate_to: web01.example.org 198 199As always with :ref:`delegation <playbooks_delegation>`, the action will be executed on the delegated host, but the information is still that of the original host in the task. 200 201.. note:: 202 When used together with ``serial``, tasks marked as ``run_once`` will be run on one host in *each* serial batch. If the task must run only once regardless of ``serial`` mode, use 203 :code:`when: inventory_hostname == ansible_play_hosts_all[0]` construct. 204 205.. note:: 206 Any conditional (in other words, `when:`) will use the variables of the 'first host' to decide if the task runs or not, no other hosts will be tested. 207 208.. note:: 209 If you want to avoid the default behavior of setting the fact for all hosts, set ``delegate_facts: True`` for the specific task or block. 210 211.. seealso:: 212 213 :ref:`about_playbooks` 214 An introduction to playbooks 215 :ref:`playbooks_delegation` 216 Running tasks on or assigning facts to specific machines 217 :ref:`playbooks_reuse_roles` 218 Playbook organization by roles 219 `User Mailing List <https://groups.google.com/group/ansible-devel>`_ 220 Have a question? Stop by the google group! 221 `irc.libera.chat <https://libera.chat/>`_ 222 #ansible IRC chat channel 223