Skip to content

Example 5

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- hosts: all
  gather_facts: false
  serial: 2
  tasks:
    - name: 1st sleep
      command: sleep 1
    - name: 2nd sleep
      command: sleep 1
    - name: 3rd sleep
      command: sleep 1
    - name: 4th sleep
      command: sleep 1
    - name: 5th sleep
      command: sleep 1

Tip

Ansible's default execution strategy is linear, where each task needs to complete on all remotes before proceeding to the next one. You can batch tasks using the serial directive in order to run them on a subset of your remote hosts.

Execute the following command

ansible-playbook playbook.yml

Bonus round

Substitute the serial: 2 directive from the playbook with strategy: free and measure the time performance. The free strategy allows all hosts to run towards the end of the playbook as fast as they can.