Ansible Inventory Files

What is Ansible?

The elevator pitch for Ansible is that it’s an automation engine, not unlike Puppet and Chef. However, Ansible doesn’t require the use of any agents on the target machines. Which is always appealing to IT folks, because not only are we automating and making things consistent, we’re also taking away administrative headaches by not installing more agents. You can read more about Ansible here, but I’m really going to concentrate on Inventory Files in this blog.

Why We Need Inventory Files

Ansible can be used in multiple ways to automate configurations on many objects that might live in our data centers like servers, networking devices, storage devices, security, etc. It would be ridiculous to have to type in which objects we want Ansible to configure every time we want to use it, though. So we create Inventory Files which are basically text files that contain the devices in our data centers and groups them to make it even easier to run tasks against.

Let’s see an example:

#Web Servers
web1.malhoit.com
web2.malhoit.com

#DB Servers
db1.malhoit.com
db2.malhoit.com

This is a really easy example, but we can build that out by adding some simple host names at the beginning.

#Web Servers
web1 web1.malhoit.com
web2 web2.malhoit.com

#DB Servers
db1 db1.malhoit.com
db2 db2.malhoit.com

We can also create groups now.

#Web Servers
web1 web1.malhoit.com
web2 web2.malhoit.com

#DB Servers
db1 db1.malhoit.com
db2 db2.malhoit.com

#Here are the groups
[web_servers]
web1
web2

[db_servers]
db1
db2

#And even groups of groups by using the :children syntax

[all_servers:children]
web_servers
db_servers

Groups

As shown above creating groups is pretty easy. It’s also good to know that devices/servers can belong to multiple groups. It makes sense because you might have a data base server that belongs to [db_servers] but it also belongs to [windows_servers]. That way we can easily update database applications using the db group, but also patch the OS using the Windows group, just as a simple use case example.

Inventory Parameters

Not only do these inventory files contain the devices, but their host names, how to connect to them (for example via SSH) and usernames and passwords. Adding this information is done through the use of Inventory Paramaters. These are absolutely necessary because it tells the Ansible Controller how to communicate with the various devices. Some examples of inventory paramaters are: ansible_port, ansible_password, and ansible_connection. I’ll show some common ones in examples.

When we first specify our devices as I did above with

web1 web1.malhoit.com

this is where we can include more information using inventory parameters. For example, if this is a Linux server, I’ll want to connect to it using SSH.

web1 ansible_host=web1.malhoit.com ansible_connection=ssh ansible_port=22

If this were a Windows server, the inventory parameter would be:

web1 ansible_host=web1.malhoit.com ansible_connection=winrm

We can also add usernames and passwords and all sorts of other parameters as listed here.

web1 ansible_host=web1.malhoit.com ansible_connection=ssh ansible_port=22 ansible_user=lauren ansible_ssh_pass=password

Every parameter is simply separated with a space.

I’ll continue to go deeper on these topics over the next several weeks. As always, no egos here. Please let me know if you have any constructive criticism @malhoit.