Skip to content

Instantly share code, notes, and snippets.

@askldjd
Created March 17, 2017 20:34
Show Gist options
  • Save askldjd/11f60381da41490a4a8f6efe1ee9cc02 to your computer and use it in GitHub Desktop.
Save askldjd/11f60381da41490a4a8f6efe1ee9cc02 to your computer and use it in GitHub Desktop.
Simple ansible inventory without ec2.py
#!/usr/bin/python
import boto3
import json
import os
client = boto3.client('ec2')
# filter.json
# [
# {
# "Name": "tag:type",
# "Values": ["utility"]
# },
# {
# "Name": "tag:app_project",
# "Values": ["appeals"]
# },
# {
# "Name": "tag:app_group",
# "Values": ["dsva"]
# },
# {
# "Name": "vpc-id",
# "Values": ["vpc-b9e418dc"]
# }
# ]
filter_file_name = os.getenv('EC2_FILTER_FILE', 'filter.json')
with open(filter_file_name) as filter_file:
filter_data= json.load(filter_file)
response = client.describe_instances(
Filters=filter_data
)
ec2_url = []
for resv in response['Reservations']:
for instance in resv['Instances']:
ec2_url.append(instance['PrivateDnsName'])
print json.dumps({
'all' : {
'hosts' : ec2_url
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment