Ansible – Inventory Managment


In this series of blog posts, I will be talking about Ansible. Ansible is a powerful automation tool that you can learn quickly.

When using Ansible, I have been constantly uneasy about how inventory is managed. If you feel the same, in this article I will try to explain how I structure my inventories and where I think it could be improved.

The Ansible documentation recommends to put servers in groups by type, like so:

mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

In your playbook you can then target specific parts to one group. For example your webservers will have different packages compared to your db servers. It also allows you to limit deployment to one group. For example with the –limit flag you can limit your deployment to just the dbservers.

An alternative approach I have been taking is to create seperate inventories for each environment. Like Production, UAT and Testing. For each environment I create an inventory like so:

[webservers]
web-1-prd.example.com
web-2-prd.example.com

[dbservers]
db-1-prd.example.com

UAT:

[webservers]
web-1-uat.example.com
web-2-uat.example.com

[dbservers]
db-1-uat.example.com

This means that when I run my Ansible command, I have a separate command for each environment. This means there is less of a chance of deploying to the wrong servers and I can deploy an entire environment all at once easily. Those commands would look like this:

ansible-playbook rails_stack.yml -i uat
ansible-playbook rails_stack.yml -i prod

The Ansible best practices document recommends this as well and naming your groups based on their location (data center).