I need some help with deploying a system I have been working for a year and a half now. In order you can understand my concern, I will explain a little about our infrastructure.

We have a server (let's call it TESTING_SERVER) where we have different testing environments for our system. Each of these environments is running entirely with docker. Each instance of a testing environment consists in: 1. Docker container with nginx acting as a proxy 2. Docker container with a Django web 3. Docker container with mysql

Every time we need to build a new environment for testing purposes (i.e: QA want to test a new feature), we use an ansible playbook which run these tasks on TESTING_SERVER:

  1. Create a docker network
  2. Create database container
  3. Clone or update django git repo somewhere in TESTING_SERVER
  4. Create django container
  5. Run django collectstatic command inside django container
  6. Run django migrate command inside django container
  7. Create nginx container

In our production environment we have a plain ubuntu server (PRODUCTION_SERVER) running mysql, django and nginx. Every time we have to deploy to production, we run an ansible playbook that (almost) repeat the steps listed above:

  1. check mysql connection (db is in another server)
  2. Clone or update django git repo somewhere in PRODUCTION_SERVER
  3. Check and restart gunicorn (is the equivalent to create django container)
  4. run django collectstatic
  5. run django migrate
  6. check nginx configuration

These two playbooks are different, although they have a lot in common. I was thinking to convert each step to an ansible task and use a conditional to know which tasks (dockerized or direct) should be run. But I will still have different tasks for each step (same playbook but seems a little tricky).

My question is: is there a way to "merge" these playbooks to have just one without repeating ourselves?


I would create per "task" a role and use it in the playbook, similar as described in the best practice document.


for example


and in the playbook call the roles


- hosts: testserver
    - create_a_docker_network
    - create_database_container
    - ...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.