-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PR #8075/4363f876 backport][stable-8] ini_file - support optional sp…
…aces around section names (#8135) ini_file - support optional spaces around section names (#8075) * ini_file - support optional spaces between section names and their surrounding brackets Some ini files have spaces between some of their section names and the brackets that enclose them. This is documented in the 'openssl.cnf(5)' man page. In order to manage files such as /etc/ssl/openssl.cnf with ini_file before now, one would have to include spaces in the section name like this: section: ' crypto_policy ' option: Options value: UnsafeLegacyRenegotiation This change implements matching section headers with such optional spaces. Existing tasks using the workaround above will continue to work, even in cases where spaces in section headers are subsequently removed. * readability improvement in the test content expressions --------- Co-authored-by: Todd Lewis <[email protected]> (cherry picked from commit 4363f87) Co-authored-by: Todd Lewis <[email protected]>
- Loading branch information
1 parent
c494fe5
commit 7b66573
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/8075-optional-space-around-section-names.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- "ini_file - support optional spaces between section names and their surrounding brackets (https://github.com/ansible-collections/community.general/pull/8075)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/integration/targets/ini_file/tasks/tests/07-section_name_spaces.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
# Copyright (c) Ansible Project | ||
# 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 | ||
|
||
## testing support for optional spaces between brackets and section names | ||
|
||
- name: Test-section_name_spaces 1 (does legacy workaround still work) - create test file | ||
ansible.builtin.copy: # noqa risky-file-permissions | ||
dest: "{{ output_file }}" | ||
content: | | ||
[ foo ] | ||
; bar=baz | ||
- name: Test-section_name_spaces 1 - update with optional spaces specified | ||
community.general.ini_file: # noqa risky-file-permissions | ||
path: "{{ output_file }}" | ||
section: ' foo ' | ||
option: bar | ||
value: frelt | ||
register: result | ||
|
||
- name: Test-section_name_spaces 1 - read content from output file | ||
ansible.builtin.slurp: | ||
src: "{{ output_file }}" | ||
register: output_content | ||
|
||
- name: Test-section_name_spaces 1 - verify results | ||
vars: | ||
actual_content: "{{ output_content.content | b64decode }}" | ||
expected_content: | | ||
[ foo ] | ||
bar = frelt | ||
ansible.builtin.assert: | ||
that: | ||
- actual_content == expected_content | ||
- result is changed | ||
- result.msg == 'option changed' | ||
|
||
|
||
- name: Test-section_name_spaces 2 (optional spaces omitted) - create test file | ||
ansible.builtin.copy: # noqa risky-file-permissions | ||
dest: "{{ output_file }}" | ||
content: | | ||
[ foo ] | ||
bar=baz" | ||
- name: Test-section_name_spaces 2 - update without optional spaces | ||
community.general.ini_file: # noqa risky-file-permissions | ||
path: "{{ output_file }}" | ||
section: foo | ||
option: bar | ||
value: frelt | ||
ignore_spaces: true | ||
register: result | ||
|
||
- name: Test-section_name_spaces 2 - read content from output file | ||
ansible.builtin.slurp: | ||
src: "{{ output_file }}" | ||
register: output_content | ||
|
||
- name: Test-section_name_spaces 2 - verify results | ||
vars: | ||
actual_content: "{{ output_content.content | b64decode }}" | ||
expected_content: "[ foo ]\nbar = frelt\n" | ||
ansible.builtin.assert: | ||
that: | ||
- actual_content == expected_content | ||
- result is changed | ||
- result.msg == 'option changed' | ||
|
||
|
||
- name: Test-section_name_spaces 3 (legacy workaround when not required) - create test file | ||
ansible.builtin.copy: # noqa risky-file-permissions | ||
dest: "{{ output_file }}" | ||
content: | | ||
[foo] | ||
; bar=baz | ||
- name: Test-section_name_spaces 3 - update with optional spaces specified | ||
community.general.ini_file: # noqa risky-file-permissions | ||
path: "{{ output_file }}" | ||
section: ' foo ' | ||
option: bar | ||
value: frelt | ||
register: result | ||
|
||
- name: Test-section_name_spaces 3 - read content from output file | ||
ansible.builtin.slurp: | ||
src: "{{ output_file }}" | ||
register: output_content | ||
|
||
- name: Test-section_name_spaces 3 - verify results | ||
vars: | ||
actual_content: "{{ output_content.content | b64decode }}" | ||
expected_content: | | ||
[foo] | ||
bar = frelt | ||
ansible.builtin.assert: | ||
that: | ||
- actual_content == expected_content | ||
- result is changed | ||
- result.msg == 'option changed' |