Ansible/EC2/Dynamic Inventory: Difference between revisions
From charlesreid1
No edit summary |
|||
| Line 9: | Line 9: | ||
[[Ansible/EC2/Dynamic Inventory]] - dynamic inventory uses the AWS API to get information about machines that AWS is managing. | [[Ansible/EC2/Dynamic Inventory]] - dynamic inventory uses the AWS API to get information about machines that AWS is managing. | ||
=Managing a dynamic inventory using EC2 nodes= | =Managing a dynamic inventory using EC2 nodes: example script= | ||
Here we walk through the dynamic EC2 inventory script provided by Ansible in their 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 | This script also has an .ini configuration file associated with it: https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini | ||
Revision as of 16:24, 15 November 2018
{Main|Ansible/EC2}}
This page covers how to use a dynamic inventory script to manage Ansible nodes when using Amazon AWS EC2.
Static vs dynamic inventory
Ansible/EC2/Static Inventory - static inventory requires the hosts file (containing the list of machines that Ansible is managing) be kept up to date by hand. This can be a burden if details are changing often or if resources are allocated automatically.
Ansible/EC2/Dynamic Inventory - dynamic inventory uses the AWS API to get information about machines that AWS is managing.
Managing a dynamic inventory using EC2 nodes: example script
Here we walk through the dynamic EC2 inventory script provided by Ansible in their 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_IDto set your AWS API access key (for boto)AWS_SECRET_ACCESS_KEYto set your AWS API access secret (for boto)AWS_PROFILEto specify a boto profileEC2_INSTANCE_FILTERSto 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)
Flags