From 6c2dbdae59dddeecf177312452afe2800c0f0367 Mon Sep 17 00:00:00 2001 From: spike77453 Date: Sat, 16 May 2026 14:56:52 +0200 Subject: [PATCH] to_ini filter plugin: add no_extra_spaces parameter (#8576) (#12059) * to_ini filter plugin: add no_extra_spaces parameter (#8576) * Update changelogs/fragments/12059-to_ini_filter_add_no_extra_spaces_parameter.yml Co-authored-by: Felix Fontein * Update plugins/filter/to_ini.py Co-authored-by: Felix Fontein * Update plugins/filter/to_ini.py Co-authored-by: Felix Fontein * Update plugins/filter/to_ini.py Co-authored-by: Felix Fontein * Update plugins/filter/to_ini.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein --- ...9-to_ini_filter_add_no_extra_spaces_parameter.yml | 2 ++ plugins/filter/to_ini.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/12059-to_ini_filter_add_no_extra_spaces_parameter.yml diff --git a/changelogs/fragments/12059-to_ini_filter_add_no_extra_spaces_parameter.yml b/changelogs/fragments/12059-to_ini_filter_add_no_extra_spaces_parameter.yml new file mode 100644 index 0000000000..688c7c9d28 --- /dev/null +++ b/changelogs/fragments/12059-to_ini_filter_add_no_extra_spaces_parameter.yml @@ -0,0 +1,2 @@ +minor_changes: + - to_ini filter plugin - add ``no_extra_spaces`` parameter (https://github.com/ansible-collections/community.general/issues/8576, https://github.com/ansible-collections/community.general/pull/12059). diff --git a/plugins/filter/to_ini.py b/plugins/filter/to_ini.py index 5a664740ff..ec2b04121f 100644 --- a/plugins/filter/to_ini.py +++ b/plugins/filter/to_ini.py @@ -16,6 +16,12 @@ options: description: The dictionary that should be converted to the INI format. type: dictionary required: true + no_extra_spaces: + description: + - Do not insert spaces before and after V(=) symbol. + type: bool + default: false + version_added: 13.0.0 seealso: - plugin: ansible.builtin.ini plugin_type: lookup @@ -68,11 +74,13 @@ class IniParser(ConfigParser): self.optionxform = str -def to_ini(obj): +def to_ini(obj, *, no_extra_spaces=False): """Read the given dict and return an INI formatted string""" if not isinstance(obj, Mapping): raise AnsibleFilterError(f"to_ini requires a dict, got {type(obj)}") + if not isinstance(no_extra_spaces, bool): + raise AnsibleFilterError(f"no_extra_spaces must be a boolean, got {type(no_extra_spaces)}") ini_parser = IniParser() @@ -86,7 +94,7 @@ def to_ini(obj): raise AnsibleFilterError("to_ini received an empty dict. An empty dict cannot be converted.") config = StringIO() - ini_parser.write(config) + ini_parser.write(config, space_around_delimiters=not no_extra_spaces) # config.getvalue() returns two \n at the end # with the below insanity, we remove the very last character of