ansible-config_template/tests/test-json.yml
Dmitriy Rabotyagov 13b52a62cd 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
2024-07-16 13:26:14 +02:00

69 lines
2.7 KiB
YAML

---
# Copyright 2018, Rackspace US
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Test basic function of config_template
# 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 }}"
register: test_content_no_overrides_json
notify: test_content_no_overrides_json check diff
- name: Read test_content_no_overrides.json
slurp:
src: /tmp/test_content_no_overrides.json
register: content_no_overrides_file
- 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)"
# 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)"