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 articles:
I'd like to get more examples about organizing playbooks, variables, and 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:
production/
├── group_vars
│ └── server.yml
└── inventory
staging/
├── group_vars
│ └── server.yml
└── inventory
deploy.yml
And usage is extremely simple:
ansible-playbook -i staging deploy.yml
Where deploy.yml
is the name of your playbook.
Ansible-playbook when provided a directory as the inventory, will search by default a file named
inventory
so no need to specify-i production/inventory
, only-i production
will work just fine.
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 encrypted