1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add support for environment variables in GCE module.

This commit is contained in:
Luke Sneeringer 2014-08-01 11:44:58 -05:00
parent 27a73f2c11
commit a4fce4818e

View file

@ -38,19 +38,37 @@ def gce_connect(module):
pem_file = module.params.get('pem_file', None) pem_file = module.params.get('pem_file', None)
project_id = module.params.get('project_id', None) project_id = module.params.get('project_id', None)
# If any of the values are not given as parameters, check the appropriate
# environment variables.
if not service_account_email:
service_account_email = os.environ.get('GCE_EMAIL', None)
if not project_id:
project_id = os.environ.get('GCE_PROJECT', None)
if not pem_file:
pem_file = os.environ.get('GCE_PEM_FILE_PATH', None)
# If we still don't have one or more of our credentials, attempt to
# get the remaining values from the libcloud secrets file.
if service_account_email is None or pem_file is None: if service_account_email is None or pem_file is None:
# Load in the libcloud secrets file
try: try:
import secrets import secrets
except ImportError: except ImportError:
secrets = None secrets = None
service_account_email, pem_file = getattr(secrets, 'GCE_PARAMS', (None, None)) if hasattr(secrets, 'GCE_PARAMS'):
if not service_account_email:
service_account_email = secrets.GCE_PARAMS[0]
if not pem_file:
pem_file = secrets.GCE_PARAMS[1]
keyword_params = getattr(secrets, 'GCE_KEYWORD_PARAMS', {}) keyword_params = getattr(secrets, 'GCE_KEYWORD_PARAMS', {})
if not project_id:
project_id = keyword_params.get('project', None) project_id = keyword_params.get('project', None)
# If we *still* don't have the credentials we need, then it's time to
# just fail out.
if service_account_email is None or pem_file is None or project_id is None: if service_account_email is None or pem_file is None or project_id is None:
module.fail_json(msg='Missing GCE connection parameters in libcloud secrets file.') module.fail_json(msg='Missing GCE connection parameters in libcloud '
'secrets file.')
return None return None
try: try: