From ee96928a2d1c21c5d2319418f4cf6ca774a3e010 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 5 Feb 2015 16:02:36 -0500 Subject: Attempting to only refresh cache when doing --list on ossh. --- bin/ansibleutil.py | 10 +++++++--- bin/ossh | 18 +++++++++++------- inventory/aws/ec2.ini | 4 ++-- inventory/multi_ec2.py | 15 ++++++++++++++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py index ed35ef8f9..6df3e7126 100644 --- a/bin/ansibleutil.py +++ b/bin/ansibleutil.py @@ -11,8 +11,12 @@ class AnsibleUtil(object): self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) self.multi_ec2_path = os.path.realpath(os.path.join(self.file_path, '..','inventory','multi_ec2.py')) - def get_inventory(self): + def get_inventory(self,args=[]): cmd = [self.multi_ec2_path] + + if args: + cmd.extend(args) + env = {} p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env) @@ -50,8 +54,8 @@ class AnsibleUtil(object): return groups - def build_host_dict(self): - inv = self.get_inventory() + def build_host_dict(self, args=[]): + inv = self.get_inventory(args) inst_by_env = {} for dns, host in inv['_meta']['hostvars'].items(): diff --git a/bin/ossh b/bin/ossh index 7f7acc6c2..1fa987d21 100755 --- a/bin/ossh +++ b/bin/ossh @@ -39,15 +39,15 @@ class Ossh(object): self.ansible = ansibleutil.AnsibleUtil() - self.host_inventory = self.get_hosts() + # get a dict of host inventory + if self.args.list: + self.get_hosts() + else: + self.get_hosts(True) if self.args.debug: print self.args - # get a dict of host inventory - self.get_hosts() - - # perform the SSH if self.args.list: self.list_hosts() @@ -106,7 +106,7 @@ class Ossh(object): if self.args.login_name: self.user = self.args.login_name - def get_hosts(self): + def get_hosts(self, cache=False): '''Query our host inventory and return a dict where the format equals: @@ -114,7 +114,11 @@ class Ossh(object): ''' # TODO: perform a numerical sort on these hosts # and display them - self.host_inventory = self.ansible.build_host_dict() + + if not cache: + self.host_inventory = self.ansible.build_host_dict() + else: + self.host_inventory = self.ansible.build_host_dict(['--cache-only']) def select_host(self, regex=False): '''select host attempts to match the host specified diff --git a/inventory/aws/ec2.ini b/inventory/aws/ec2.ini index c6693bb1c..cdc22d2d4 100644 --- a/inventory/aws/ec2.ini +++ b/inventory/aws/ec2.ini @@ -11,8 +11,8 @@ # AWS regions to make calls to. Set this to 'all' to make request to all regions # in AWS and merge the results together. Alternatively, set this to a comma # separated list of regions. E.g. 'us-east-1,us-west-1,us-west-2' -#regions = all -regions = us-east-1 +regions = all +#regions = us-east-1 regions_exclude = us-gov-west-1,cn-north-1 # When generating inventory, Ansible needs to know how to address a server. diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index c60bb50d2..499264267 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -43,9 +43,15 @@ class MultiEc2(object): else: raise RuntimeError("Could not find valid ec2 credentials in the environment.") + if self.args.cache_only: + # get data from disk + result = self.get_inventory_from_cache() + if not result: + self.get_inventory() + self.write_to_cache() # if its a host query, fetch and do not cache - if self.args.host: + elif self.args.host: self.get_inventory() elif not self.is_cache_valid(): # go fetch the inventories and cache them if cache is expired @@ -186,6 +192,8 @@ class MultiEc2(object): ''' Command line argument processing ''' parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on a provider') + parser.add_argument('--cache-only', action='store_true', default=False, + help='Fetch cached only instances (default: False)') parser.add_argument('--list', action='store_true', default=True, help='List instances (default: True)') parser.add_argument('--host', action='store', default=False, @@ -203,9 +211,14 @@ class MultiEc2(object): ''' Reads the inventory from the cache file and returns it as a JSON object ''' + if not os.path.isfile(self.cache_path): + return None + with open(self.cache_path, 'r') as cache: self.result = json.loads(cache.read()) + return True + def json_format_dict(self, data, pretty=False): ''' Converts a dict to a JSON object and dumps it as a formatted string ''' -- cgit v1.2.3