From e997098a39dfaa6a033b3af189172cc7834082bd Mon Sep 17 00:00:00 2001 From: Donien <88634789+Donien@users.noreply.github.com> Date: Tue, 9 Dec 2025 19:45:49 +0100 Subject: [PATCH] Fix escaping of special characters in INI writer Values are now enclosed by double quotes while using the `to_json` filter for the escaping. --- .../fragments/426-quotes-and-escaping-ini-writer.yml | 2 ++ .../tests/integration/test_ini_config.py | 2 +- roles/icingaweb2/templates/ini_template.j2 | 8 ++------ 3 files changed, 5 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/426-quotes-and-escaping-ini-writer.yml diff --git a/changelogs/fragments/426-quotes-and-escaping-ini-writer.yml b/changelogs/fragments/426-quotes-and-escaping-ini-writer.yml new file mode 100644 index 00000000..5548511a --- /dev/null +++ b/changelogs/fragments/426-quotes-and-escaping-ini-writer.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix escaping of special characters within the generic INI writer (:code:`icingaweb2` role). Values are now enclosed by double quotes while using the :code:`to_json` filter for the escaping (#426). diff --git a/molecule/ini-configuration-tests/tests/integration/test_ini_config.py b/molecule/ini-configuration-tests/tests/integration/test_ini_config.py index 962d3949..20f9aa77 100644 --- a/molecule/ini-configuration-tests/tests/integration/test_ini_config.py +++ b/molecule/ini-configuration-tests/tests/integration/test_ini_config.py @@ -2,7 +2,7 @@ def test_string(host): i2_file = host.file("/tmp/string") print(i2_file.content_string) assert i2_file.is_file - assert i2_file.content_string == "\n[section]\ntest = string\n" + assert i2_file.content_string == '\n[section]\ntest = "string"\n' def test_number(host): i2_file = host.file("/tmp/number") diff --git a/roles/icingaweb2/templates/ini_template.j2 b/roles/icingaweb2/templates/ini_template.j2 index 0443b5c0..386aeb72 100644 --- a/roles/icingaweb2/templates/ini_template.j2 +++ b/roles/icingaweb2/templates/ini_template.j2 @@ -3,14 +3,10 @@ [{{ section }}] {% for option, value in options.items() %} -{% if value is number %} -{{ option }} = "{{ value | quote }}" -{% elif value is iterable and (value is not string and value is not mapping) %} +{% if value is iterable and (value is not string and value is not mapping) %} {{ option }} = "{{ value | join(', ') }}" -{% elif ( value is string and ( "=" in value or "!" in value or " " in value ) )%} -{{ option }} = "{{ value }}" {% else %} -{{ option }} = {{ value }} +{{ option }} = "{{ value | to_json | trim('"') }}" {% endif %} {% endfor %} {% endfor %}