Ansible/Hosts/Dynamic Inventory
From charlesreid1
Contents
What is a dynamic inventory script
In order to remotely connect to machines and run commands to configure the machines, Ansible needs information about how to connect to each remote machine.
The There are two ways to provide this information:
- a static inventory file, in which details are given by hand (this can be streamlined using groups and variables)
- a dynamic inventory file, in which a script provides Ansible with details about nodes using a dynamic resource (API, database, etc.)
How to use dynamic inventory script
The dynamic inventory script must accept to command line flags, for the two ways Ansible will call this dynamic inventory script:
--host=<hostname> show host details --list list groups
For example, Ansible will call the inventory script like so:
$ ./dynamic.py --host=vagrant2
Example: Vagrant Dynamic Inventory Script
See Ansible/Vagrant/Dynamic Inventory for a sample dynamic inventory script for use with Vagrant.
Example: EC2 Dynamic Inventory Script
There is a very thorough EC2 dynamic inventory script in the Ansible Github repository: https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
This script also has an .ini configuration file associated with it: https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
Let's run through an overview of how it works.
Environment variables
This dynamic inventory script uses lots of environment variables. The most important are:
AWS_ACCESS_KEY_ID
to set your AWS API access key (for boto)AWS_SECRET_ACCESS_KEY
to set your AWS API access secret (for boto)AWS_PROFILE
to specify a boto profileEC2_INSTANCE_FILTERS
to filter the AWS instances returned on various criteria. Extremely detailed API reference for filtering is here
Note that each of these can also be specified in the .ini file.
Important
By default, the ec2.ini file is configured for all Amazon cloud services. You have to turn off the ones you don't want (elasticcache, rds, etc.)
Python script
The script proper defines a Python object that manages all of the information received from the AWS API.
The script uses the boto library to interact with the AWS API. The object defines methods for parsing user command line arguments, and implements a number of other methods to do things like ask for a list of nodes, filter nodes by attribute, and store/retrieve information from a cache on disk to prevent everything from going extremely slowly due to slow AWS API responses.
Key methods defined for the object:
- read input file: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L304
- parse cli: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L523
- get instances: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L593
- get route names for instances from route 53: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L1468
- get a dictionary with instance information: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L1489
- process API call returns: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L1542
The call order of the script, when run, is:
- object is created https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L1709
- init method is called: https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L245
- credentials are set up
- cache is loaded (if present)
- data is printed (list of nodes)