Adding instance_states option to gce inventory

This commit is contained in:
Kenny Woodson 2016-06-14 17:13:21 -04:00
parent 8c8e064828
commit ebf1feb5bb
2 changed files with 29 additions and 0 deletions

View file

@ -45,6 +45,11 @@ gce_service_account_email_address =
gce_service_account_pem_file_path = gce_service_account_pem_file_path =
gce_project_id = 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] [inventory]
# The 'inventory_ip_type' parameter specifies whether 'ansible_ssh_host' should # The 'inventory_ip_type' parameter specifies whether 'ansible_ssh_host' should
# contain the instance internal or external address. Values may be either # contain the instance internal or external address. Values may be either

View file

@ -159,6 +159,19 @@ class GceInventory(object):
config.add_section('inventory') config.add_section('inventory')
config.read(gce_ini_path) 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 return config
def get_inventory_options(self): def get_inventory_options(self):
@ -283,6 +296,17 @@ class GceInventory(object):
meta["hostvars"] = {} meta["hostvars"] = {}
for node in self.driver.list_nodes(): 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 name = node.name
meta["hostvars"][name] = self.node_to_dict(node) meta["hostvars"][name] = self.node_to_dict(node)