conn=boto.ec2.connect_to_region('us-east-1')
reservations = conn.get_all_instances()
for res in reservations:
for inst in res.instances:
if 'Name' in inst.tags:
print ("define host{")
print ("use\tgeneric-host") # \t for tab
print "%s %s " % ("host_name", inst.tags['Name'])
#print "%s "% (inst.tags['Name'])
print "%s \t %s " % ("alias", inst.tags['Name'])
print "%s %s " % ("address", inst.public_dns_name)
print(inst.instance_type)
print ("}\n")
#print "%s (%s) [%s] [%s]" % (inst.tags['Name'], inst.id, inst.state, inst.public_dns_name)
else:
print "%s [%s]" % (inst.id, inst.state)
######NOTE#########
I believe in the following script output you can see the output name " example: instance_type " so you can use the same for getting that information:
example: for my above script:
inst.public_dns_name will give public_dns_name
inst.region will give the region details and so..on
NOTE: To get the details of the instance that is part of a custom tags then:
reservations = conn.get_all_instances(filters={'tag-key': 'groups'})
############ external script ##############
from pprint import pprint from boto import ec2 AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXX' AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' ec2conn = ec2.connection.EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) reservations = ec2conn.get_all_instances() instances = [i for r in reservations for i in r.instances] for i in instances: pprint(i.__dict__) break # remove this to list all instances
{'_in_monitoring_element': False,
'ami_launch_index': u'0',
'architecture': u'x86_64',
'block_device_mapping': {},
'connection': EC2Connection:ec2.amazonaws.com,
'dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
'id': u'i-xxxxxxxx',
'image_id': u'ami-xxxxxxxx',
'instanceState': u'\n ',
'instance_class': None,
'instance_type': u'm1.large',
'ip_address': u'xxx.xxx.xxx.xxx',
'item': u'\n ',
'kernel': None,
'key_name': u'FARM-xxxx',
'launch_time': u'2009-10-27T17:10:22.000Z',
'monitored': False,
'monitoring': u'\n ',
'persistent': False,
'placement': u'us-east-1d',
'previous_state': None,
'private_dns_name': u'ip-10-xxx-xxx-xxx.ec2.internal',
'private_ip_address': u'10.xxx.xxx.xxx',
'product_codes': [],
'public_dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
'ramdisk': None,
'reason': '',
'region': RegionInfo:us-east-1,
'requester_id': None,
'rootDeviceType': u'instance-store',
'root_device_name': None,
'shutdown_state': None,
'spot_instance_request_id': None,
'state': u'running',
'state_code': 16,
'subnet_id': None,
'vpc_id': None}
External Links:
http://www.saltycrane.com/blog/2010/03/how-list-attributes-ec2-instance-python-and-boto/
No comments:
Post a Comment