Handle 'dict' type as input of 'content'
When users specificy the 'content' attribute when using the config_template module, it's possible that this input can be a dict. This commit handles dictionary input, and dumps it accordingly based on 'config_type' Change-Id: I1f12810ad7ce5746f8938ec9d608c532ba530d58
This commit is contained in:
parent
d66fb8f310
commit
371cbc1fde
@ -434,7 +434,19 @@ class ActionModule(ActionBase):
|
|||||||
file_path = self._loader.get_basedir()
|
file_path = self._loader.get_basedir()
|
||||||
|
|
||||||
user_source = self._task.args.get('src')
|
user_source = self._task.args.get('src')
|
||||||
user_content = str(self._task.args.get('content'))
|
# (alextricity25) It's possible that the user could pass in a datatype
|
||||||
|
# and not always a string. In this case we don't want the datatype
|
||||||
|
# python representation to be printed out to the file, but rather we
|
||||||
|
# want the serialized version.
|
||||||
|
_user_content = self._task.args.get('content')
|
||||||
|
|
||||||
|
# If the data type of the content input is a dictionary, it's
|
||||||
|
# converted dumped as json if config_type is 'json'.
|
||||||
|
if isinstance(_user_content, dict):
|
||||||
|
if self._task.args.get('config_type') == 'json':
|
||||||
|
_user_content = json.dumps(_user_content)
|
||||||
|
|
||||||
|
user_content = str(_user_content)
|
||||||
if not user_source:
|
if not user_source:
|
||||||
if not user_content:
|
if not user_content:
|
||||||
return False, dict(
|
return False, dict(
|
||||||
|
1
tests/files/test_content_no_overrides.json.expected
Normal file
1
tests/files/test_content_no_overrides.json.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"charlie": "echo", "alfa": "bravo", "foxtrot": {"golf": "hotel"}}
|
7
tests/templates/test.json
Normal file
7
tests/templates/test.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"alfa": "bravo",
|
||||||
|
"charlie" : "echo",
|
||||||
|
"foxtrot": {
|
||||||
|
"golf": "hotel"
|
||||||
|
}
|
||||||
|
}
|
@ -108,6 +108,34 @@
|
|||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "(no_extend_file.content | b64decode) == (no_extend_file_expected.content | b64decode)"
|
- "(no_extend_file.content | b64decode) == (no_extend_file_expected.content | b64decode)"
|
||||||
|
|
||||||
|
# Test content attribute with a dictionary input and config_type equal to 'json'
|
||||||
|
- name: Template test JSON template with content attribute
|
||||||
|
config_template:
|
||||||
|
dest: "/tmp/test_content_no_overrides.json"
|
||||||
|
config_overrides: {}
|
||||||
|
config_type: "json"
|
||||||
|
content: "{{ lookup('file', playbook_dir ~ '/templates/test.json') | from_json }}"
|
||||||
|
- name: Read test_content_no_overrides.json
|
||||||
|
slurp:
|
||||||
|
src: /tmp/test_content_no_overrides.json
|
||||||
|
register: content_no_overrides_file
|
||||||
|
- name: Read expected test_content_no_overrides.json
|
||||||
|
slurp:
|
||||||
|
src: "{{ playbook_dir }}/files/test_content_no_overrides.json.expected"
|
||||||
|
register: content_no_overrides_file_expected
|
||||||
|
- debug:
|
||||||
|
msg: "content_no_overrides.json - {{ content_no_overrides_file.content | b64decode | from_json }}"
|
||||||
|
- debug:
|
||||||
|
msg: "content_no_overrides.json.expected - {{ content_no_overrides_file_expected.content | b64decode | from_json }}"
|
||||||
|
# NOTE (alextricity25): The config_template module doesn't use ordered dicts when reading and writing json
|
||||||
|
# data, so we can't guarantee that the string literal of both file's content will be the same. Instead, we compare
|
||||||
|
# the content after transforming it into a dictionary.
|
||||||
|
- name: Compare file content
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "(content_no_overrides_file.content | b64decode | from_json) == (content_no_overrides_file_expected.content | b64decode | from_json)"
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
test_config_ini_overrides:
|
test_config_ini_overrides:
|
||||||
DEFAULT:
|
DEFAULT:
|
||||||
|
Loading…
Reference in New Issue
Block a user