From ebf1feb5bbdeb11f8d348e41abb86b120ec098b3 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 14 Jun 2016 17:13:21 -0400 Subject: [PATCH] Adding instance_states option to gce inventory --- contrib/inventory/gce.ini | 5 +++++ contrib/inventory/gce.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/contrib/inventory/gce.ini b/contrib/inventory/gce.ini index 31f49704e6..97a7ee403f 100644 --- a/contrib/inventory/gce.ini +++ b/contrib/inventory/gce.ini @@ -45,6 +45,11 @@ gce_service_account_email_address = gce_service_account_pem_file_path = gce_project_id = +# Filter inventory based on on state. Leave undefined to return instances regardless of state. +# example: Uncomment to only return inventory in the running or provisioning state +#instance_states = RUNNING,PROVISIONING + + [inventory] # The 'inventory_ip_type' parameter specifies whether 'ansible_ssh_host' should # contain the instance internal or external address. Values may be either diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index 2b402de740..307d6efa2c 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -159,6 +159,19 @@ class GceInventory(object): config.add_section('inventory') config.read(gce_ini_path) + + ######### + # Section added for processing ini settings + ######### + + # Set the instance_states filter based on config file options + self.instance_states = [] + if config.has_option('gce', 'instance_states'): + states = config.get('gce', 'instance_states') + # Ignore if instance_states is an empty string. + if states: + self.instance_states = states.split(',') + return config def get_inventory_options(self): @@ -283,6 +296,17 @@ class GceInventory(object): meta["hostvars"] = {} for node in self.driver.list_nodes(): + + # This check filters on the desired instance states defined in the + # config file with the instance_states config option. + # + # If the instance_states list is _empty_ then _ALL_ states are returned. + # + # If the instance_states list is _populated_ then check the current + # state against the instance_states list + if self.instance_states and not node.extra['status'] in self.instance_states: + continue + name = node.name meta["hostvars"][name] = self.node_to_dict(node)