Welcome to part 2 of my Ansible series. If this is your first time here, you might consider starting with part 1 I’ve broken this part into two post to make it easier to consume. Part 2 will cover inventory, variables, modules and plays. These components are critical to understanding how Ansible works and the components of playbooks. This part covers mostly oncepts with supporting examples. When we get to Ad-hoc Ansible examples, we’ll get to see Ansible in action. So without further ado, lets get started.
Ansible needs to know what hosts to execute modules against. This list of hosts is called an inventory file. It’s a flat text file format, but does support some grouping and organization options. Here’s a simple sample.
Our simple sample has 13 hosts without any organiation. This is simpe enough to start running ad-hoc ansible commands against, but what it can’t do is target a specific server or specific servers. What we need is a bit more organization to our inventory.
Well that looks a lot better. We have a bunch of meaningful groups, but you’ll notice in the above example that a number of hosts that exist in multiple groups. Ansible support is perfectly happy with this, but can result in some unexpected behavior related to variables (we’ll cover these later in this post). While the above is a functional inventory file, odds are we don’t really want to repeat hostnames across multiple groups as this leads to added maintenance costs and possibility of maintenance errors. Luckily Ansible supports groups within groups.
While this looks a bit more complex, it is actually far easier to maintain and use. If you have a new server, or retire a server, you simple add or remove the hostname from the appropriate group and the associated groups are automatically updated when used. It does require some up front planning and organization when setting up groups. Ansible can also handle multiple inventory files, which allows you to create separate inventory files which aids when maintaining larger inventories. Ansible allows you to specify multiple inventory files via passing -i followed by a comma separated list of inventory files.
Note: Groups can have multiple parents and multiple children, but cannot have circular relationships.
In the real work, especially in a large enterprise, it’s unlikely that you are going to want to maintain large inventories by hand. It’s not easily maintained, it doesn’t scale and has a high risk of being inaccurate. In addition, complex grouping can become challenging to maintain properly. Fortunately, ansible has the concept of dynamic inventory.
Ansible provides a method for gathering inventory from various cloud providers, LDAP, Cobbler, or enterprise CMDB (Configuration Management database) software. To use dynamic inventory, you specify a path to a script file that when executed returns inventory information in a JSON format instead of passing a path to an inventory file. Ansible ships with a number of provider scripts for systems like EC2/Eucalyptus, Rackspace Cloud, and OpenStack. Each of these require some initial setup and can be used along with parameter’s to generate inventory files with just the hosts you want. Here’s a couple of example calls for cobbler, Amazon EC2 and Openstack.
Setting up these dynamic inventory providers is a bit out of the scope of this article and outside my experience. More information about dynamic inventory and examples can be found in Ansible’s documentation as well as documentation on how to create your own custom dynamic inventory source
Another option is to create inventory files thought an ansible module. This gives a great deal of flexibility to create inventory files with just the hosts and variables needed. It also allows you to check the content and helps troubleshooting. This is the method my company has gone with as we interfacing with a custom CMDB (and different instances of the CMBD) and operate across multiple datacenters. We’ll cover building modules in part 12
Lastly there are 2 built in groups:
ungrouped. All contains all hosts while ungrouped contains only hosts that are not defined in a group.
The second post of part 2 will cover Host and Group Variables, Modules and Plays. Click here to continue