What approach would you advise to organize multistage deployment with ansible in case you have different variables for stages?
The main idea is defining group variables for different stages.
There are two artcles:
- http://rosstuck.com/multistage-environments-with-ansible/)
- (http://toja.io/using-host-and-group-vars-files-in-ansible/)
I'd like to get more examples about organizing playbooks, variables, hosts and understand advantages and disadvantages of your approach.
Recently, I used the approach I had already mentioned in the question, and it occurred to be one of the most convenient to my mind.
It is taken from Organizing Group Vars Files in Ansible article, but altered a little, because, unfortunately, title of the article does not reflect it's real value and purpose and names of playbooks are confusing as well. In fact, it took a considerable time to realize that it is about Multistage deployment with Ansible.
Your layout of directories should be like that:
And usage is extremely simple:
Where
deploy.yml
is the name of your playbook.And the benefits are:
You don't have to mantain some unnecessary groups like
[production:children]
You don't need to keep confusing groups and files like like
group_vars/production.yml
All vars and hosts are in separate directories, therefore it is easy to keep them different and history of changes is clear. You may even split it in separate repositories if you want
You may also keep secrets for production in repository using
ansible-vault
, in other words, store all your vital variables encryptedIn case of complex inventory structures, when maintaining groups of groups is not the best option ( http://docs.ansible.com/ansible/intro_inventory.html#groups-of-groups-and-group-variables ) the following trick can be used:
Production Inventory File:
Development Inventory File:
Now in playbook itself, include the following task before loading roles:
To prevent accidental execution of playbook on production environment,
prod.config.yml
cab be encrypted withansible-vault
Currently I'm using the following structure:
The inventories could look like this:
And for production:
This allows for some nice combinations. By using
ansible -i hosts
I can target all known hosts. I'm using this, for example, to add all servers in the inventory to a monitoring configuration file.By using
ansible -i hosts/development
I can limit the command to the development (or production) servers. I do this when I want to test a new configuration on the development system before I apply it to production.I'm currently using this structure for about 25 servers on 3 different stages and it works pretty well for me. It has some weaknesses, though:
development
orproduction
groups, because it's redundant and easy to forget.That being said, it works pretty well for me, so maybe it works well for you, too.