Ansible AWX - Importing AWS EC2 instances
In this post I shall demonstrate how to configure AWX to auto discover AWS EC2 instances.
Overview
- Create/ use an existing user with access to the AWS service APIs
- Configure AWX to setup auto discovery of EC2 instances in a VPC
In this demonstration I have Ansible AWX setup locally.
Prerequisite
- VPC setup with Public DNS enabled
- Existing running EC2 instances present in VPC
Setup AWS API user
- Login to the AWS console
- Create a new user via IAM
- Access type = Programmatic access
- Policy = EC2 Full access (this user will require permissions to manage all EC2 instances)
- Save the credentials - access key id and secret access key
Configure AWX
Create AWS cloud credential
This credential will authorize AWX to make the necessary API calls.
Enter an appropriate name, and enter the access and secret keys you saved from the API user created in IAM earlier.
Create inventory
You will need to create a new inventory so any EC2 instances detected are then associated as hosts.
Add sources
Within the inventory select Sources, then click on the plus icon to add a new one.
- source = Amazon EC2
- credential = the AWS cloud credential created earlier
- regions = select the AWS region (Only Hosts associated with the selected regions will be updated)
- Update options = Overwrite (this cleans up the Hosts, any hosts and groups that were previously present on the external source but are now removed will be removed from the Tower inventory)
- instance filters
- Limit to hosts having a tag:
- Limit to hosts using either key pair:
- Limit to hosts where the Name tag begins with ‘test:)’
Add schedule
A schedule allows a polling mechanism to be put in place so that AWX will query AWS for an updated picture of the EC2 environment.
Select the appropriate start date, local time zone, repeat frequency, start time..
Note. You can manage all schedules in AWX via the Schedules menu.
Start Sync
Once the above steps are complete you can start a manual sync.
- Go back to the inventory
- Click on sources
- Click on the sync icon
A job console will show the progress of the import any running EC2 instances will be imported into AWX.
Navigate to Hosts within the same inventory to display a list of imported instances.