Parallel Playbook Execution In Ansible

İbrahim Gündüz
May 6, 2017 · 2 min read

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 memcacheserver
* 1 mongodbserver
* 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, free deployment strategy helps us to run all the tasks in parallel in the tasks 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 memcache and mongodb installation steps.

memcache.yml

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

mongodb.yml

- 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.

provision.yml:

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

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
İbrahim Gündüz

Written by

Just another software developer Github: https://github.com/ibrahimgunduz34 Linkedin: linkedin.com/in/ibrahimgunduz