Sobes.tech
Middle

How can you change the execution of a playbook from sequential to parallel on machines?

sobes.tech AI

Answer from AI

To change task execution from sequential to parallel in Ansible, you can use the following approaches:

  1. serial parameter: Allows running a playbook on a group of hosts in subsets of a specified size. This is not exactly parallel execution of all hosts at once, but it divides the group into "batches".

    # playbook.yml
    ---
    - name: Apply configuration
      hosts: webservers
      serial: 5 # Run on 5 hosts simultaneously
      tasks:
        - name: Update packages
          apt:
            name: '*'
            state: latest
    
  2. throttle parameter: Limits the number of processes executing a specific task or block simultaneously. It is applied within a task or block.

    # playbook.yml
    ---
    - name: Apply configuration
      hosts: webservers
      tasks:
        - name: Deploy application
          command: /opt/deploy_app.sh
          throttle: 3 # Run this command on no more than 3 hosts at a time
    
  3. free_strategy parameter: Changes the playbook execution strategy from default (sequential task execution on hosts) to free (tasks are executed on hosts as they become ready, without waiting for previous tasks to complete on the same host). This affects task execution within a single host.

    # playbook.yml
    ---
    - name: Configure servers with free strategy
      hosts: all
      strategy: free
      tasks:
        - name: Install nginx
          ansible.builtin.apt:
            name: nginx
            state: present
        - name: Install redis
          ansible.builtin.apt:
            name: redis
            state: present
    
  4. Configuring forks parameter in ansible.cfg: This parameter controls the number of parallel processes that ansible launches to interact with hosts. Increasing forks allows Ansible to work with more hosts simultaneously.

    # ansible.cfg
    [defaults]
    forks = 30 # Increase the number of parallel processes to 30
    
  5. Asynchronous tasks with polling (async and poll): For tasks that may take a long time, you can use asynchronous mode. Ansible starts the task on the remote machine and immediately moves on to the next hosts or tasks. It then periodically checks the status of the asynchronous task.

    # playbook.yml
    ---
    - name: Run long-running task asynchronously
      hosts: databases
      tasks:
        - name: Backup database
          command: /opt/backup_db.sh
          async: 1800 # Maximum execution time in seconds (30 minutes)
          poll: 15    # How often to check task status (every 15 seconds)
    

The most direct way to influence parallelism at the host level within a playbook execution (if serial is not used) is to use the forks parameter in the configuration. The strategy: free, serial, throttle, and asynchronous tasks provide more granular control over the order or concurrency of execution.

Method Application level Impact
forks Ansible configuration Maximum number of parallel hosts
serial Playbook Execution in groups (batches)
throttle Task/Block Limit on parallel task executions
strategy: free Playbook Parallel task execution within a host
async / poll Task Asynchronous task launch