1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-06-04 23:37:12 +00:00

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 <felix@fontein.de>

* Update plugins/filter/to_ini.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/filter/to_ini.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/filter/to_ini.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/filter/to_ini.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
spike77453 2026-05-16 14:56:52 +02:00 committed by GitHub
parent a15d9a3510
commit 6c2dbdae59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

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

View file

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