diff --git a/action/_v2_config_template.py b/action/_v2_config_template.py index ff8c5c1..dde4b54 100644 --- a/action/_v2_config_template.py +++ b/action/_v2_config_template.py @@ -434,7 +434,19 @@ class ActionModule(ActionBase): file_path = self._loader.get_basedir() 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_content: return False, dict( diff --git a/tests/files/test_content_no_overrides.json.expected b/tests/files/test_content_no_overrides.json.expected new file mode 100644 index 0000000..a5b7f4f --- /dev/null +++ b/tests/files/test_content_no_overrides.json.expected @@ -0,0 +1 @@ +{"charlie": "echo", "alfa": "bravo", "foxtrot": {"golf": "hotel"}} diff --git a/tests/templates/test.json b/tests/templates/test.json new file mode 100644 index 0000000..06ac6cb --- /dev/null +++ b/tests/templates/test.json @@ -0,0 +1,7 @@ +{ + "alfa": "bravo", + "charlie" : "echo", + "foxtrot": { + "golf": "hotel" + } +} diff --git a/tests/test-config_template.yml b/tests/test-config_template.yml index c8d695a..4961243 100644 --- a/tests/test-config_template.yml +++ b/tests/test-config_template.yml @@ -108,6 +108,34 @@ assert: that: - "(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: test_config_ini_overrides: DEFAULT: