From 13b52a62cd35751b26be520a72214c2d10e8c471 Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Tue, 16 Jul 2024 13:26:14 +0200 Subject: [PATCH] Respect yml_multilines for JSON configs At the moment there is no good way to have new lines ("\n") in JSON files and not to convert the result to a list. In the meanwhile this can be totally expected/required thing to do, for example to define cloud-init vendor data [1] [1] https://cloudinit.readthedocs.io/en/latest/reference/datasources/openstack.html#vendor-data Related-Bug: #2073171 Change-Id: I00d74a26357a8458e38d9eb816a3f3eceeae1e0d --- plugins/action/config_template.py | 3 ++- tests/files/test_multiline_strs.json.expected | 8 +++++++ tests/templates/test_multiline_strs.json | 3 +++ tests/test-json.yml | 23 +++++++++++++++++++ tests/test.yml | 8 +++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/files/test_multiline_strs.json.expected create mode 100644 tests/templates/test_multiline_strs.json diff --git a/plugins/action/config_template.py b/plugins/action/config_template.py index 7a82b45..0490e18 100644 --- a/plugins/action/config_template.py +++ b/plugins/action/config_template.py @@ -556,7 +556,8 @@ class ActionModule(ActionBase): merged_resultant = self._merge_dict( base_items=original_resultant, new_items=config_overrides, - list_extend=list_extend + list_extend=list_extend, + yml_multilines=yml_multilines ) return json.dumps( merged_resultant, diff --git a/tests/files/test_multiline_strs.json.expected b/tests/files/test_multiline_strs.json.expected new file mode 100644 index 0000000..81a50b4 --- /dev/null +++ b/tests/files/test_multiline_strs.json.expected @@ -0,0 +1,8 @@ +{ + "cloud-init": "#cloud-config\npackage_upgrade: True\npackages:\n - htop", + "list": [ + "Curious cat investigates", + "a ball of yarn in sight" + ], + "string": "Freshly baked cookies\n, waiting to be enjoyed." +} \ No newline at end of file diff --git a/tests/templates/test_multiline_strs.json b/tests/templates/test_multiline_strs.json new file mode 100644 index 0000000..f020e12 --- /dev/null +++ b/tests/templates/test_multiline_strs.json @@ -0,0 +1,3 @@ +{ + "string": "Freshly baked cookies\n, waiting to be enjoyed." +} \ No newline at end of file diff --git a/tests/test-json.yml b/tests/test-json.yml index 7e5ed75..f5c9b57 100644 --- a/tests/test-json.yml +++ b/tests/test-json.yml @@ -43,3 +43,26 @@ assert: that: - "(content_no_overrides_file.content | b64decode | from_json) == (content_no_overrides_file_expected.content | b64decode | from_json)" + + +# Values containing newlines should not be chopped into a list +# when yml_multilines is set to True +- name: Test multiline strings in json + config_template: + src: "{{ playbook_dir }}/templates/test_multiline_strs.json" + dest: "/tmp/multiline_strs.json" + config_overrides: "{{ test_multiline_strs_json_overrides }}" + config_type: json + yml_multilines: True +- name: Read multiline_strs.json + slurp: + src: /tmp/multiline_strs.json + register: multiline_strs_file +- debug: + msg: "Multiline JSON Strings - {{ multiline_strs_file.content | b64decode }}" +- debug: + msg: "Multiline JSON Strings Expected - {{ multiline_strs_json_file_expected.content | b64decode }}" +- name: Compare files + assert: + that: + - "(multiline_strs_json_file_expected.content | b64decode) == (multiline_strs_file.content | b64decode)" diff --git a/tests/test.yml b/tests/test.yml index ed64b93..98f730a 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -63,6 +63,11 @@ src: "{{ playbook_dir }}/files/test_multiline_strs.yml.expected" register: multiline_strs_file_expected + - name: Read expected test_multiline_strs.yml + slurp: + src: "{{ playbook_dir }}/files/test_multiline_strs.json.expected" + register: multiline_strs_json_file_expected + - name: Read expected test_list_only.yml slurp: src: "{{ playbook_dir }}/files/test_list_only.yml.expected" @@ -166,6 +171,9 @@ new_multiline_str: | This should not be a list + test_multiline_strs_json_overrides: + "list": "Curious cat investigates\n, a ball of yarn in sight" + "cloud-init": "#cloud-config\npackage_upgrade: True\npackages:\n - htop" test_list_only_overrides: - things: stuff test_default_section_overrides: