diff options
author | Achraf Cherti <Asher256@users.noreply.github.com> | 2017-08-18 00:27:03 -0400 |
---|---|---|
committer | ansibot <ansibot@users.noreply.github.com> | 2017-08-18 00:27:03 -0400 |
commit | e45d5b7e8ebc05fadb0f66f115722c5ae29246af (patch) | |
tree | d6aedc16e99d302ff5bd9ff82e1650ebb626aa45 /contrib | |
parent | 9ab9945cf316be65bc9819074deb29a45a95dcbc (diff) | |
download | ansible-e45d5b7e8ebc05fadb0f66f115722c5ae29246af.tar.gz |
Compatibility of gce.py (Google Cloud Ansible inventory) with Python 3 (#26032)
* Compatibility of gce.py (inventory) with Python 3
* Revert './secrets.py' file check (will import 'secrets' from PYTHONPATH)
Instead of checking if secrets.py exists in the current directory, this
commit will make gce import 'secrets' from one of PYTHONPATH's paths.
There are 2 possibilities:
1. secrets.py will be used if secrets.GCE_PARAMS and
secrets.GCE_KEYWORD_PARAMS are declared.
2. secrets.py will be ignored if secrets.GCE_PARAMS and
secrets.GCE_KEYWORD_PARAMS aren't declared. This could happen in Python
>=3.6 where a module named 'secrets' could be imported if a custom
secrets.py doesn't exist in PYTHONPATH.
Check out https://www.python.org/dev/peps/pep-0506/ and
https://docs.python.org/3/library/secrets.html for more information.
Diffstat (limited to 'contrib')
-rwxr-xr-x | contrib/inventory/gce.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index a73c8b7d02..a1e72dac7f 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -92,7 +92,10 @@ import argparse from time import time -import ConfigParser +if sys.version_info >= (3, 0): + import configparser +else: + import ConfigParser as configparser import logging logging.getLogger('libcloud.common.google').addHandler(logging.NullHandler()) @@ -213,7 +216,7 @@ class GceInventory(object): # This provides empty defaults to each key, so that environment # variable configuration (as opposed to INI configuration) is able # to work. - config = ConfigParser.SafeConfigParser(defaults={ + config = configparser.SafeConfigParser(defaults={ 'gce_service_account_email_address': '', 'gce_service_account_pem_file_path': '', 'gce_project_id': '', @@ -271,10 +274,11 @@ class GceInventory(object): # exists. secrets_path = self.config.get('gce', 'libcloud_secrets') secrets_found = False + try: import secrets - args = list(getattr(secrets, 'GCE_PARAMS', [])) - kwargs = getattr(secrets, 'GCE_KEYWORD_PARAMS', {}) + args = list(secrets.GCE_PARAMS) + kwargs = secrets.GCE_KEYWORD_PARAMS secrets_found = True except: pass |