mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Don't convert nulls to strings.
This change is similar to https://github.com/ansible/ansible/pull/10465 It extends the logic there to also support none types. Right now if you have a '!!null' in yaml, and that var gets passed around, it will get converted to a string. eg. defaults/main.yml ``` ENABLE_AWESOME_FEATURE: !!null # Yaml Null OTHER_CONFIG: secret1: "so_secret" secret2: "even_more_secret" CONFIG: hostname: "some_hostname" features: awesame_feature: "{{ ENABLE_AWESOME_FEATURE}}" secrets: "{{ OTHER_CONFIG }}" ``` If you output `CONFIG` to json or yaml, the feature flag would get represented in the output as a string instead of as a null, but secrets would get represented as a dictionary. This is a mis-match in behaviour where some "types" are retained and others are not. This change should fix the issue. I also updated the template test to test for this and made the changes to v2. Added a changelog entry specifically for the change from empty string to null as the default. Made the null representation configurable. It still defaults to the python NoneType but can be overriden to be an emptystring by updating the DEFAULT_NULL_REPRESENTATION config.
This commit is contained in:
@@ -40,7 +40,7 @@ def mk_boolean(value):
|
||||
else:
|
||||
return False
|
||||
|
||||
def get_config(p, section, key, env_var, default, boolean=False, integer=False, floating=False, islist=False):
|
||||
def get_config(p, section, key, env_var, default, boolean=False, integer=False, floating=False, islist=False, isnone=False):
|
||||
''' return a configuration variable with casting '''
|
||||
value = _get_config(p, section, key, env_var, default)
|
||||
if boolean:
|
||||
@@ -53,6 +53,9 @@ def get_config(p, section, key, env_var, default, boolean=False, integer=False,
|
||||
elif islist:
|
||||
if isinstance(value, string_types):
|
||||
value = [x.strip() for x in value.split(',')]
|
||||
elif isnone:
|
||||
if value == "None":
|
||||
value = None
|
||||
elif isinstance(value, string_types):
|
||||
value = unquote(value)
|
||||
return value
|
||||
@@ -205,6 +208,7 @@ DEFAULT_LOAD_CALLBACK_PLUGINS = get_config(p, DEFAULTS, 'bin_ansible_callbacks'
|
||||
DEFAULT_CALLBACK_WHITELIST = get_config(p, DEFAULTS, 'callback_whitelist', 'ANSIBLE_CALLBACK_WHITELIST', [], islist=True)
|
||||
RETRY_FILES_ENABLED = get_config(p, DEFAULTS, 'retry_files_enabled', 'ANSIBLE_RETRY_FILES_ENABLED', True, boolean=True)
|
||||
RETRY_FILES_SAVE_PATH = get_config(p, DEFAULTS, 'retry_files_save_path', 'ANSIBLE_RETRY_FILES_SAVE_PATH', '~/')
|
||||
DEFAULT_NULL_REPRESENTATION = get_config(p, DEFAULTS, 'null_representation', 'ANSIBLE_NULL_REPRESENTATION', None, isnone=True)
|
||||
|
||||
# CONNECTION RELATED
|
||||
ANSIBLE_SSH_ARGS = get_config(p, 'ssh_connection', 'ssh_args', 'ANSIBLE_SSH_ARGS', None)
|
||||
|
||||
@@ -37,6 +37,7 @@ from ansible.template.vars import AnsibleJ2Vars
|
||||
from ansible.utils.debug import debug
|
||||
|
||||
from numbers import Number
|
||||
from types import NoneType
|
||||
|
||||
__all__ = ['Templar']
|
||||
|
||||
@@ -187,6 +188,8 @@ class Templar:
|
||||
resolved_val = self._available_variables[var_name]
|
||||
if isinstance(resolved_val, NON_TEMPLATED_TYPES):
|
||||
return resolved_val
|
||||
elif isinstance(resolved_val, NoneType):
|
||||
return C.DEFAULT_NULL_REPRESENTATION
|
||||
|
||||
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user