-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_config_fetch.py
56 lines (48 loc) · 1.41 KB
/
ec2_config_fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/python
#
# get ec2 instance config from aws and save in ec2_config.yml
# which use ansible playbook for provisioning similar instances
#
import boto3
import yaml
import getopt, sys
def usage():
print '\nUsage: '+sys.argv[0]+' -i <file1> [option]'
def get_config(name):
my_session = boto3.session.Session()
my_region = my_session.region_name
ec2 = boto3.client('ec2')
filters = [{
'Name': 'tag:Name',
'Values': [name]
}]
response = ec2.describe_instances(Filters=filters)
#response = ec2.describe_instances()
for r in response['Reservations']:
for i in r['Instances']:
config = {'instance_type':i['InstanceType'], \
'security_group': i['SecurityGroups'][0]['GroupId'], \
'image':i['ImageId'], 'region':my_region, 'keypair':i['KeyName']}
with open('ec2_config.yaml', 'w') as f:
print "Creating config file for instance "+name
yaml.dump(config, f, default_flow_style=False)
def main(argv):
try:
opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'name='])
if not opts:
print 'No options supplied'
usage()
except getopt.GetoptError,e:
print e
usage()
sys.exit(2)
name=None
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ("-n", "--name"):
name = arg
get_config(name)
if __name__ =='__main__':
main(sys.argv[1:])