mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
[PR #7744/ec12422f backport][stable-8] Adding a new filter: to_ini, which allows conversion of a dictionary to an INI formatted string (#7772)
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
(cherry picked from commit ec12422fae)
Co-authored-by: Steffen Scheib <37306894+sscheib@users.noreply.github.com>
This commit is contained in:
58
tests/integration/targets/filter_to_ini/tasks/main.yml
Normal file
58
tests/integration/targets/filter_to_ini/tasks/main.yml
Normal 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"
|
||||
...
|
||||
8
tests/integration/targets/filter_to_ini/vars/main.yml
Normal file
8
tests/integration/targets/filter_to_ini/vars/main.yml
Normal 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'
|
||||
...
|
||||
Reference in New Issue
Block a user