Added support for specifying zone for gce dynamic inventory (#20938)

This commit is contained in:
Markus Liljedahl 2017-03-16 13:28:30 +01:00 committed by Ryan Brown
parent 7a3b6ca37c
commit 67dc8c146e
2 changed files with 9 additions and 5 deletions

View file

@ -37,13 +37,14 @@
# statement in the inventory script. However, you can specify an absolute # statement in the inventory script. However, you can specify an absolute
# path to the secrets.py file with 'libcloud_secrets' parameter. # path to the secrets.py file with 'libcloud_secrets' parameter.
# This option will be deprecated in a future release. # This option will be deprecated in a future release.
libcloud_secrets = libcloud_secrets =
# If you are not going to use a 'secrets.py' file, you can set the necessary # If you are not going to use a 'secrets.py' file, you can set the necessary
# authorization parameters here. # authorization parameters here.
gce_service_account_email_address = gce_service_account_email_address =
gce_service_account_pem_file_path = gce_service_account_pem_file_path =
gce_project_id = gce_project_id =
gce_zone =
# Filter inventory based on on state. Leave undefined to return instances regardless of state. # 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 # example: Uncomment to only return inventory in the running or provisioning state

View file

@ -217,6 +217,7 @@ class GceInventory(object):
'gce_service_account_email_address': '', 'gce_service_account_email_address': '',
'gce_service_account_pem_file_path': '', 'gce_service_account_pem_file_path': '',
'gce_project_id': '', 'gce_project_id': '',
'gce_zone': '',
'libcloud_secrets': '', 'libcloud_secrets': '',
'inventory_ip_type': '', 'inventory_ip_type': '',
'cache_path': '~/.ansible/tmp', 'cache_path': '~/.ansible/tmp',
@ -296,13 +297,15 @@ class GceInventory(object):
self.config.get('gce','gce_service_account_email_address'), self.config.get('gce','gce_service_account_email_address'),
self.config.get('gce','gce_service_account_pem_file_path') self.config.get('gce','gce_service_account_pem_file_path')
] ]
kwargs = {'project': self.config.get('gce', 'gce_project_id')} kwargs = {'project': self.config.get('gce', 'gce_project_id'),
'datacenter': self.config.get('gce', 'gce_zone')}
# If the appropriate environment variables are set, they override # If the appropriate environment variables are set, they override
# other configuration; process those into our args and kwargs. # other configuration; process those into our args and kwargs.
args[0] = os.environ.get('GCE_EMAIL', args[0]) args[0] = os.environ.get('GCE_EMAIL', args[0])
args[1] = os.environ.get('GCE_PEM_FILE_PATH', args[1]) args[1] = os.environ.get('GCE_PEM_FILE_PATH', args[1])
kwargs['project'] = os.environ.get('GCE_PROJECT', kwargs['project']) kwargs['project'] = os.environ.get('GCE_PROJECT', kwargs['project'])
kwargs['datacenter'] = os.environ.get('GCE_ZONE', kwargs['datacenter'])
# Retrieve and return the GCE driver. # Retrieve and return the GCE driver.
gce = get_driver(Provider.GCE)(*args, **kwargs) gce = get_driver(Provider.GCE)(*args, **kwargs)