Developer Space
Published in

Developer Space

Parallel Playbook Execution In Ansible

In some cases, an application environment might be consist of multiple independent services that you need to deploy. if you consider that all the services might have pre/post-deployment jobs, every release might become a nightmare. By the way, I didn’t mention that you need to multiply the duration with the instance quantity of each service yet…

Fortunately, we have ansible and it allows to deploy an application in various strategies. You can deploy an application that consists of multiple independent services to multiple server instances, either in parallel or serial based on services.

Let’s take an example. Let’s assume our application consists of the following services:
* 2 server
* 1 server
* 1 application server

There are three service types we need to deploy. However, we can start to installation for each of them at the same time because there is no relation between each other in the example case. So we can reduce the deployment time.

Ansible, deployment strategy helps us to run all the tasks in parallel in the section of a playbook. If the installation steps consist of multiple tasks, you can split the installation steps into separate files like the following example.

The following YAML files contain and installation steps.


- name: Install memcache
apt: name=memcached update_cache=yes
- name: Ensure memcache is started.
service: name=memcached state=started


- name: Install mongodb
apt: name=mongodb update_cache=yes
- name: Ensure mongodb is started.
service: name=mongodb state=started

Finally, the following file is the entry point of the installation steps which is going to execute all the installation steps in parallel.


- hosts: mongodb:memcache
gather_facts: no
strategy: free
- include: memcache.yml
when: "'memcache' in group_names"
- include: mongodb.yml
when: "'mongodb' in group_names"
- hosts: app

That’s all! You can run the following command to start the deployment.

$ ansible-playbook -i hosts.ini provision.yml

As you can see from the following output, ansible started to deploy all the services in parallel.

You can also check out the following benchmark results regarding synchronous and asynchronous execution of the same deployment.

Synchronous playbook:

real 0m19.681s
user 0m3.236s
sys 0m0.484s

Asynchronous playbook:

real 0m9.725s
user 0m2.428s
sys 0m0.408s



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store