[Ansible] – How to run tasks in parallel?

By default, Ansible runs the same task in 5 hosts in parallel. This configuration can be changed by using “forks” parameter definition in the ansible.cfg file and/or by informing it in the command line(E.g ansible-playbook my.yml –forks=10). See more about it here.

The async attribute can define how long should waiting for the task be completed and the poll defines the period that should wait for try to get the result. The default poll value is 10 seconds. Following an example.

- name: Example of async and poll
  hosts: all
    - name: Run scripts
      command: "/tmp/{{ item }}.sh"
      async: 100
      poll: 25
        - fill_database
        - create_users
      register: result

See more about it in the here. To know more about how to check the result see this part of the Ansible docs.

The following example shows that usage of wait_for and retries. 

    - name: Checking that the machine is started
      wait_for: # The task will waiting for the host be started
        host: servera.example.com
        state: started
        delay: 10
        timeout: 60
        port: 22
      delegate_to: serverb.example.com
      retries: 5 # number of times that this check will be performed

To know how to make the tasks more performative see this doc.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s