Dima Kreisserman
Dima Kreisserman

Reputation: 659

ansible - run playbooks in parallel

I have one ansible playbook which is called from the Jenkins after a nightly build. As you can see each line contains different product component is installed. Is there a way to run them in parallel? So all playbooks files to server1 will run in serial but playbooks for server1 and server2, should run in parallel.

- include: "componentA.yml target=server1"
- include: "componentB.yml target=server1"

- include: "componentC.yml target=server2"
- include: "componentD.yml target=server2"

Upvotes: 2

Views: 6453

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

You can refactor your playbook to target all servers at once and use strategy: free with dynamically included tasks. Like this:

---
- hosts: server*
  strategy: free
  tasks:
    - include: componentA_tasks.yml
      when: inventory_hostname == 'server1'
    - include: componentB_tasks.yml
      when: inventory_hostname == 'server1'
    - include: componentC_tasks.yml
      when: inventory_hostname == 'server2'
    - include: componentD_tasks.yml
      when: inventory_hostname == 'server2'

This way server2 will not wait for every task on server1 to complete and will fast-forward to componentC tasks.

Or you can split your Jenkins pipeline into parallel tasks and execute ansible in separate threads for server1 and server2.

Upvotes: 1

Related Questions