From 94d60b53de58a9743707dd0ff4147780744772ea Mon Sep 17 00:00:00 2001 From: Lorrin Nelson Date: Thu, 5 Dec 2013 11:40:18 -0800 Subject: [PATCH] ec2 inventory now include Ansible 1.3 style _meta data each host. --- plugins/inventory/ec2.py | 42 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/plugins/inventory/ec2.py b/plugins/inventory/ec2.py index b64025647ef..d6fe9504865 100755 --- a/plugins/inventory/ec2.py +++ b/plugins/inventory/ec2.py @@ -136,7 +136,7 @@ class Ec2Inventory(object): # Inventory grouped by instance IDs, tags, security groups, regions, # and availability zones - self.inventory = {} + self.inventory = {"_meta" : {"hostvars" : {}}} # Index of hostname (address) to instance ID self.index = {} @@ -376,6 +376,8 @@ class Ec2Inventory(object): # Global Tag: tag all EC2 instances self.push(self.inventory, 'ec2', dest) + self.inventory["_meta"]["hostvars"][dest] = self.get_host_info_dict_from_instance(instance) + def add_rds_instance(self, instance, region): ''' Adds an RDS instance to the inventory and index, as long as it is @@ -479,23 +481,7 @@ class Ec2Inventory(object): return list(name_list) - def get_host_info(self): - ''' Get variables about a specific host ''' - - if len(self.index) == 0: - # Need to load index from cache - self.load_index_from_cache() - - if not self.args.host in self.index: - # try updating the cache - self.do_api_calls_update_cache() - if not self.args.host in self.index: - # host migh not exist anymore - return self.json_format_dict({}, True) - - (region, instance_id) = self.index[self.args.host] - - instance = self.get_instance(region, instance_id) + def get_host_info_dict_from_instance(self, instance): instance_vars = {} for key in vars(instance): value = getattr(instance, key) @@ -536,8 +522,26 @@ class Ec2Inventory(object): #print type(value) #print value - return self.json_format_dict(instance_vars, True) + return instance_vars + def get_host_info(self): + ''' Get variables about a specific host ''' + + if len(self.index) == 0: + # Need to load index from cache + self.load_index_from_cache() + + if not self.args.host in self.index: + # try updating the cache + self.do_api_calls_update_cache() + if not self.args.host in self.index: + # host migh not exist anymore + return self.json_format_dict({}, True) + + (region, instance_id) = self.index[self.args.host] + + instance = self.get_instance(region, instance_id) + return self.json_format_dict(self.get_host_info_dict_from_instance(instance), True) def push(self, my_dict, key, element): ''' Pushed an element onto an array that may not have been defined in