Adding a new filter: to_ini, which allows conversion of a dictionary to an INI formatted string (#7744)

* Adding a new filter: to_ini, which allows conversion of a dictionary to an INI formatted string

* Adding to_ini maintainers into BOTMETA

* Correcting filter suffix

* Moving filter to correct path

* Adding error handling; Removing quotes from examples; Fixing RETURN documentation

* Removing the last newline char; Adding error handling for an empty dict

* Adding integration tests for to_ini

* Fixing F-String usage

* Fixing formatting

* Fixing whitespace

* Moving import statements below documentation; Adding a more generic Exception handling; Removing unused imports

* Removing not needed set_fact and replacing it with using vars:

* Replacing MutableMapping with Mapping
This commit is contained in:
Steffen Scheib 2023-12-28 08:32:57 +01:00 committed by GitHub
commit ec12422fae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 173 additions and 0 deletions

View file

@ -0,0 +1,58 @@
---
# Copyright (c) 2023, Steffen Scheib <steffen@scheib.me>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: >-
Write INI file that reflects using to_ini to {{ ini_test_file_filter }}
ansible.builtin.copy:
dest: '{{ ini_test_file_filter }}'
content: '{{ ini_test_dict | community.general.to_ini }}'
vars:
ini_test_dict:
section_name:
key_name: 'key value'
another_section:
connection: 'ssh'
- name: 'Write INI file manually to {{ ini_test_file }}'
ansible.builtin.copy:
dest: '{{ ini_test_file }}'
content: |
[section_name]
key_name = key value
[another_section]
connection = ssh
- name: 'Slurp the manually created test file: {{ ini_test_file }}'
ansible.builtin.slurp:
src: '{{ ini_test_file }}'
register: 'ini_file_content'
- name: 'Slurp the test file created with to_ini: {{ ini_test_file_filter }}'
ansible.builtin.slurp:
src: '{{ ini_test_file_filter }}'
register: 'ini_file_filter_content'
- name: >-
Ensure the manually created test file and the test file created with
to_ini are identical
ansible.builtin.assert:
that:
- 'ini_file_content.content | b64decode ==
ini_file_filter_content.content | b64decode'
- name: 'Try to convert an empty dictionary with to_ini'
ansible.builtin.debug:
msg: '{{ {} | community.general.to_ini }}'
register: 'ini_empty_dict'
ignore_errors: true
- name: 'Ensure the correct exception was raised'
ansible.builtin.assert:
that:
- "'to_ini received an empty dict. An empty dict cannot be converted.' in
ini_empty_dict.msg"
...

View file

@ -0,0 +1,8 @@
---
# Copyright (c) 2023, Steffen Scheib <steffen@scheib.me>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
ini_test_file: '/tmp/test.ini'
ini_test_file_filter: '/tmp/test_filter.ini'
...