1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ini_file: fix empty-value vs. no-value inconsistency (#3074)

* fix empty-value vs. no-value inconsistency

* rename changelog fragment

* tests: omit value where there should be no value

* add integration tests
This commit is contained in:
quidame 2021-08-08 18:34:34 +02:00 committed by GitHub
parent 85bcef3f5a
commit 2831bc45f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 10 deletions

View file

@ -0,0 +1,4 @@
---
bugfixes:
- ini_file - fix inconsistency between empty value and no value
(https://github.com/ansible-collections/community.general/issues/3031).

View file

@ -205,11 +205,11 @@ def do_ini(module, filename, section=None, option=None, value=None,
for i in range(index, 0, -1): for i in range(index, 0, -1):
# search backwards for previous non-blank or non-comment line # search backwards for previous non-blank or non-comment line
if not non_blank_non_comment_pattern.match(ini_lines[i - 1]): if not non_blank_non_comment_pattern.match(ini_lines[i - 1]):
if option and value: if option and value is not None:
ini_lines.insert(i, assignment_format % (option, value)) ini_lines.insert(i, assignment_format % (option, value))
msg = 'option added' msg = 'option added'
changed = True changed = True
elif option and not value and allow_no_value: elif option and value is None and allow_no_value:
ini_lines.insert(i, '%s\n' % option) ini_lines.insert(i, '%s\n' % option)
msg = 'option added' msg = 'option added'
changed = True changed = True
@ -225,7 +225,7 @@ def do_ini(module, filename, section=None, option=None, value=None,
if state == 'present': if state == 'present':
# change the existing option line # change the existing option line
if match_opt(option, line): if match_opt(option, line):
if not value and allow_no_value: if value is None and allow_no_value:
newline = u'%s\n' % option newline = u'%s\n' % option
else: else:
newline = assignment_format % (option, value) newline = assignment_format % (option, value)
@ -324,7 +324,7 @@ def main():
create = module.params['create'] create = module.params['create']
if state == 'present' and not allow_no_value and value is None: if state == 'present' and not allow_no_value and value is None:
module.fail_json("Parameter 'value' must not be empty if state=present and allow_no_value=False") module.fail_json("Parameter 'value' must be defined if state=present and allow_no_value=False")
(changed, backup_file, diff, msg) = do_ini(module, path, section, option, value, state, backup, no_extra_spaces, create, allow_no_value) (changed, backup_file, diff, msg) = do_ini(module, path, section, option, value, state, backup, no_extra_spaces, create, allow_no_value)

View file

@ -215,10 +215,10 @@
path: "{{ output_file }}" path: "{{ output_file }}"
section: mysqld section: mysqld
option: "{{ item.o }}" option: "{{ item.o }}"
value: "{{ item.v }}" value: "{{ item.v | d(omit) }}"
allow_no_value: yes allow_no_value: yes
with_items: with_items:
- { o: "skip-name-resolve", v: null } - { o: "skip-name-resolve" }
- { o: "max_connections", v: "500" } - { o: "max_connections", v: "500" }
- name: read content from output file - name: read content from output file
@ -459,12 +459,23 @@
option: like option: like
value: tea value: tea
state: absent state: absent
- name: Test with empty string
# See https://github.com/ansible-collections/community.general/issues/3031
- name: Tests with empty strings
ini_file: ini_file:
path: "{{ output_file }}" path: "{{ output_file }}"
section: extensions section: "{{ item.section | d('extensions') }}"
option: evolve option: "{{ item.option }}"
value: "" value: ""
allow_no_value: "{{ item.no_value | d(omit) }}"
loop:
- option: evolve
- option: regress
- section: foobar
option: foo
no_value: true
- option: improve
no_value: true
- name: read content from output file - name: read content from output file
slurp: slurp:
@ -473,7 +484,7 @@
- name: set expected content and get current ini file content - name: set expected content and get current ini file content
set_fact: set_fact:
expected15: "\n[extensions]\nevolve = \n" expected15: "\n[extensions]\nevolve = \nregress = \nimprove = \n[foobar]\nfoo = \n"
content15: "{{ output_content.content | b64decode }}" content15: "{{ output_content.content | b64decode }}"
- debug: var=content15 - debug: var=content15
- name: Verify content of ini file is as expected - name: Verify content of ini file is as expected