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
This commit is contained in:
Dmitriy Rabotyagov 2024-07-16 13:26:14 +02:00
parent ca25191181
commit 13b52a62cd
5 changed files with 44 additions and 1 deletions

View File

@ -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,

View File

@ -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."
}

View File

@ -0,0 +1,3 @@
{
"string": "Freshly baked cookies\n, waiting to be enjoyed."
}

View File

@ -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)"

View File

@ -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: