Add ignore_spaces option to ini_file to ignore spacing changes (#7273)

* Add `ignore_spaces` option to `ini_file` to ignore spacing changes

Add a new `ignore_spaces` option to the `ini_file` module which, if
true, prevents the module from changing a line in a file if the only
thing that would change by doing so is whitespace before or after the
`=`.

Also add test cases for this new functionality. There were previously
no tests for `ini_file` at all, and this doesn't attempt to fix that,
but it does add tests to ensure that the new behavior implemented here
as well as the old behavior in the affected code are correct.

Fixes #7202.

* Add changelog fragment

* pep8 / pylint

* remove unused import

* fix typo in comment in integration test file

* Add symlink tests to main.yml

It appears that #6546 added symlink tests but neglected to add them to
main.yml so they weren't being executed.

* ini_file symlink tests; create output files in correct location

* Add integration tests for ini_file ignore_spaces

* PR feedback
This commit is contained in:
Jonathan Kamens 2023-09-17 07:22:22 -04:00 committed by GitHub
parent be9dcd2c85
commit 8a9b98273d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 220 additions and 19 deletions

View file

@ -38,3 +38,9 @@
- name: include tasks to test regressions
include_tasks: tests/03-encoding.yml
- name: include tasks to test symlink handling
include_tasks: tests/04-symlink.yml
- name: include tasks to test ignore_spaces
include_tasks: tests/05-ignore_spaces.yml

View file

@ -3,7 +3,7 @@
# 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
## basiscs
## basics
- name: test-basic 1 - specify both "value" and "values" and fail
ini_file:

View file

@ -9,26 +9,26 @@
content: |
[main]
foo=BAR
dest: my_original_file.ini
dest: "{{ remote_tmp_dir }}/my_original_file.ini"
- name: Clean up symlink.ini
ansible.builtin.file:
path: symlink.ini
path: "{{ remote_tmp_dir }}/symlink.ini"
state: absent
- name: Create a symbolic link
ansible.builtin.file:
src: my_original_file.ini
dest: symlink.ini
dest: "{{ remote_tmp_dir }}/symlink.ini"
state: link
- name: Set the proxy key on the symlink which will be converted as a file
community.general.ini_file:
path: symlink.ini
path: "{{ remote_tmp_dir }}/symlink.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
- name: Set the proxy key on the final file that is still unchanged
community.general.ini_file:
path: my_original_file.ini
path: "{{ remote_tmp_dir }}/my_original_file.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
@ -41,7 +41,7 @@
- block: *prepare
- name: Set the proxy key on the symlink which will be preserved
community.general.ini_file:
path: symlink.ini
path: "{{ remote_tmp_dir }}/symlink.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
@ -49,7 +49,7 @@
register: result
- name: Set the proxy key on the target directly that was changed in the previous step
community.general.ini_file:
path: my_original_file.ini
path: "{{ remote_tmp_dir }}/my_original_file.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'

View file

@ -0,0 +1,123 @@
---
# 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 ignore_spaces option
- name: test-ignore_spaces 1 (commented line updated) - create test file
copy:
dest: "{{ output_file }}"
content: "[foo]\n; bar=baz\n"
- name: test-ignore_spaces 1 - set new value
ini_file:
path: "{{ output_file }}"
section: foo
option: bar
value: frelt
ignore_spaces: true
register: result
- name: test-ignore_spaces 1 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-ignore_spaces 1 - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: "[foo]\nbar = frelt\n"
assert:
that:
- actual_content == expected_content
- result is changed
- result.msg == 'option changed'
- name: test-ignore_spaces 2 (uncommented line updated) - create test file
copy:
dest: "{{ output_file }}"
content: "[foo]\nbar=baz\n"
- name: test-ignore_spaces 2 - set new value
ini_file:
path: "{{ output_file }}"
section: foo
option: bar
value: frelt
ignore_spaces: true
register: result
- name: test-ignore_spaces 2 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-ignore_spaces 2 - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: "[foo]\nbar = frelt\n"
assert:
that:
- actual_content == expected_content
- result is changed
- result.msg == 'option changed'
- name: test-ignore_spaces 3 (spaces on top of no spaces) - create test file
copy:
dest: "{{ output_file }}"
content: "[foo]\nbar=baz\n"
- name: test-ignore_spaces 3 - try to set value
ini_file:
path: "{{ output_file }}"
section: foo
option: bar
value: baz
ignore_spaces: true
register: result
- name: test-ignore_spaces 3 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-ignore_spaces 3 - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: "[foo]\nbar=baz\n"
assert:
that:
- actual_content == expected_content
- result is not changed
- result.msg == "OK"
- name: test-ignore_spaces 4 (no spaces on top of spaces) - create test file
copy:
dest: "{{ output_file }}"
content: "[foo]\nbar = baz\n"
- name: test-ignore_spaces 4 - try to set value
ini_file:
path: "{{ output_file }}"
section: foo
option: bar
value: baz
ignore_spaces: true
no_extra_spaces: true
register: result
- name: test-ignore_spaces 4 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-ignore_spaces 4 - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: "[foo]\nbar = baz\n"
assert:
that:
- actual_content == expected_content
- result is not changed
- result.msg == "OK"