From 1469ec448771a299fce9aefd877e3190a2bed91a Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Nov 2018 11:45:45 -0500 Subject: [PATCH] better handling of bad type in config (#48821) * better handling of bad type in config fixes #22468, fixes #22476 (cherry picked from commit 87e44a7ed137d0c34b180dfb4e8ba9ed600c28e6) --- changelogs/fragments/deal_with_bad_config_types.yml | 2 ++ lib/ansible/config/manager.py | 10 +++++++++- test/integration/targets/config/aliases | 1 + test/integration/targets/config/runme.sh | 10 ++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/deal_with_bad_config_types.yml create mode 100644 test/integration/targets/config/aliases create mode 100755 test/integration/targets/config/runme.sh diff --git a/changelogs/fragments/deal_with_bad_config_types.yml b/changelogs/fragments/deal_with_bad_config_types.yml new file mode 100644 index 00000000000..c7b2f99426c --- /dev/null +++ b/changelogs/fragments/deal_with_bad_config_types.yml @@ -0,0 +1,2 @@ +bugfixes: + - better error message when bad type in config, deal with EVNAR= more gracefully https://github.com/ansible/ansible/issues/22470 diff --git a/lib/ansible/config/manager.py b/lib/ansible/config/manager.py index 6a7b388f37e..edabf771668 100644 --- a/lib/ansible/config/manager.py +++ b/lib/ansible/config/manager.py @@ -430,7 +430,15 @@ class ConfigManager(object): return value, origin # ensure correct type, can raise exceptoins on mismatched types - value = ensure_type(value, defs[config].get('type'), origin=origin) + try: + value = ensure_type(value, defs[config].get('type'), origin=origin) + except ValueError as e: + if origin.startswith('env:') and value == '': + # this is empty env var for non string so we can set to default + origin = 'default' + value = ensure_type(defs[config].get('default'), defs[config].get('type'), origin=origin) + else: + raise AnsibleOptionsError('Invalid type for configuration option %s: %s' % (to_native(config), to_native(e))) # deal with deprecation of the setting if 'deprecated' in defs[config] and origin != 'default': diff --git a/test/integration/targets/config/aliases b/test/integration/targets/config/aliases new file mode 100644 index 00000000000..a6dafcf8cd8 --- /dev/null +++ b/test/integration/targets/config/aliases @@ -0,0 +1 @@ +shippable/posix/group1 diff --git a/test/integration/targets/config/runme.sh b/test/integration/targets/config/runme.sh new file mode 100755 index 00000000000..f0181afc3ab --- /dev/null +++ b/test/integration/targets/config/runme.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -eux + +# ignore empty env var and use default +# shellcheck disable=SC1007 +ANSIBLE_TIMEOUT= ansible -m ping localhost "$@" + +# env var is wrong type, this should be a fatal error pointing at the setting +ANSIBLE_TIMEOUT='lola' ansible -m ping localhost "$@" 2>&1|grep 'AnsibleOptionsError: Invalid type for configuration option DEFAULT_TIMEOUT'