summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2016-06-14 17:13:21 -0400
committerKenny Woodson <kwoodson@redhat.com>2016-06-17 10:11:23 -0400
commitebf1feb5bbdeb11f8d348e41abb86b120ec098b3 (patch)
tree801e0130ecad315149afa3731bf28000bbd087b2
parent8c8e064828b3e3946770af04d5282335ffd3add0 (diff)
downloadansible-ebf1feb5bbdeb11f8d348e41abb86b120ec098b3.tar.gz
Adding instance_states option to gce inventory
-rw-r--r--contrib/inventory/gce.ini5
-rwxr-xr-xcontrib/inventory/gce.py24
2 files changed, 29 insertions, 0 deletions
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)