Created
March 17, 2017 20:34
-
-
Save askldjd/11f60381da41490a4a8f6efe1ee9cc02 to your computer and use it in GitHub Desktop.
Simple ansible inventory without ec2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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