From charlesreid1

(Created page with "{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/St...")
 
No edit summary
Line 56: Line 56:
* cache is loaded (if present)
* cache is loaded (if present)
* data is printed (list of nodes)
* data is printed (list of nodes)
=Flags=
{{AnsibleFlag}}

Revision as of 16:40, 11 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

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 profile
  • EC2_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.)

Ref: https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#inventory-script-example-aws-ec2

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:

The call order of the script, when run, is:

Flags