From 3b40c6f3b795ac226c5cf0067ff5e181669e2097 Mon Sep 17 00:00:00 2001 From: Rick Elrod Date: Wed, 14 Oct 2020 11:53:08 -0500 Subject: [PATCH] [config] coerce more to string when 'type: str' (#72172) Change: - When a plugin defines `type: str` on a parameter, treat more kinds of input as a string instead of whatever it is parsed as. Test Plan: - New unit tests - CI Signed-off-by: Rick Elrod --- changelogs/fragments/more-types-to-string-config.yml | 2 ++ lib/ansible/config/manager.py | 2 +- test/units/config/__init__.py | 0 test/units/config/test_manager.py | 12 ++++++++++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/more-types-to-string-config.yml create mode 100644 test/units/config/__init__.py diff --git a/changelogs/fragments/more-types-to-string-config.yml b/changelogs/fragments/more-types-to-string-config.yml new file mode 100644 index 00000000000..bcbdf61d023 --- /dev/null +++ b/changelogs/fragments/more-types-to-string-config.yml @@ -0,0 +1,2 @@ +minor_changes: + - "config - more types are now automatically coerced to string when ``type: str`` is used and the value is parsed as a different type" diff --git a/lib/ansible/config/manager.py b/lib/ansible/config/manager.py index e57bbc5ace4..dfc85387ddb 100644 --- a/lib/ansible/config/manager.py +++ b/lib/ansible/config/manager.py @@ -149,7 +149,7 @@ def ensure_type(value, value_type, origin=None): errmsg = 'dictionary' elif value_type in ('str', 'string'): - if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode)): + if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode, bool, int, float, complex)): value = unquote(to_text(value, errors='surrogate_or_strict')) else: errmsg = 'string' diff --git a/test/units/config/__init__.py b/test/units/config/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/config/test_manager.py b/test/units/config/test_manager.py index d103e5e6618..15c9c1fb500 100644 --- a/test/units/config/test_manager.py +++ b/test/units/config/test_manager.py @@ -60,6 +60,18 @@ ensure_test_data = [ ('a', 'string', string_types), ('Café', 'string', string_types), ('', 'string', string_types), + ('29', 'str', string_types), + ('13.37', 'str', string_types), + ('123j', 'string', string_types), + ('0x123', 'string', string_types), + ('true', 'string', string_types), + ('True', 'string', string_types), + (0, 'str', string_types), + (29, 'str', string_types), + (13.37, 'str', string_types), + (123j, 'string', string_types), + (0x123, 'string', string_types), + (True, 'string', string_types), ('None', 'none', type(None)) ]