mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* Added integration test and fixed bug
* added changelog fragment
* Update changelogs/fragments/1972-ini_file-empty-str-value.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
* Update tests/integration/targets/ini_file/tasks/main.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 088743749b
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
1e5e0824d2
commit
1ae57fc5dd
3 changed files with 42 additions and 6 deletions
2
changelogs/fragments/1972-ini_file-empty-str-value.yml
Normal file
2
changelogs/fragments/1972-ini_file-empty-str-value.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- ini_file - allows an empty string as a value for an option (https://github.com/ansible-collections/community.general/pull/1972).
|
|
@ -114,9 +114,7 @@ from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
def match_opt(option, line):
|
def match_opt(option, line):
|
||||||
option = re.escape(option)
|
option = re.escape(option)
|
||||||
return re.match('( |\t)*%s( |\t)*(=|$)' % option, line) \
|
return re.match('[#;]?( |\t)*%s( |\t)*(=|$)' % option, line)
|
||||||
or re.match('#( |\t)*%s( |\t)*(=|$)' % option, line) \
|
|
||||||
or re.match(';( |\t)*%s( |\t)*(=|$)' % option, line)
|
|
||||||
|
|
||||||
|
|
||||||
def match_active_opt(option, line):
|
def match_active_opt(option, line):
|
||||||
|
@ -251,9 +249,9 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
||||||
if not within_section and state == 'present':
|
if not within_section and state == 'present':
|
||||||
ini_lines.append('[%s]\n' % section)
|
ini_lines.append('[%s]\n' % section)
|
||||||
msg = 'section and option added'
|
msg = 'section and option added'
|
||||||
if option and value:
|
if option and value is not None:
|
||||||
ini_lines.append(assignment_format % (option, value))
|
ini_lines.append(assignment_format % (option, value))
|
||||||
elif option and not value and allow_no_value:
|
elif option and value is None and allow_no_value:
|
||||||
ini_lines.append('%s\n' % option)
|
ini_lines.append('%s\n' % option)
|
||||||
else:
|
else:
|
||||||
msg = 'only section added'
|
msg = 'only section added'
|
||||||
|
@ -312,7 +310,7 @@ def main():
|
||||||
allow_no_value = module.params['allow_no_value']
|
allow_no_value = module.params['allow_no_value']
|
||||||
create = module.params['create']
|
create = module.params['create']
|
||||||
|
|
||||||
if state == 'present' and not allow_no_value and not value:
|
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 not be empty 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)
|
||||||
|
|
|
@ -444,3 +444,39 @@
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- content14 == expected14
|
- content14 == expected14
|
||||||
|
|
||||||
|
- name: Check add option with empty string value
|
||||||
|
block:
|
||||||
|
- name: Remove drinks
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: drinks
|
||||||
|
state: absent
|
||||||
|
- name: Remove tea
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section:
|
||||||
|
option: like
|
||||||
|
value: tea
|
||||||
|
state: absent
|
||||||
|
- name: Test with empty string
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: extensions
|
||||||
|
option: evolve
|
||||||
|
value: ""
|
||||||
|
|
||||||
|
- name: read content from output file
|
||||||
|
slurp:
|
||||||
|
src: "{{ output_file }}"
|
||||||
|
register: output_content
|
||||||
|
|
||||||
|
- name: set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
expected15: "\n[extensions]\nevolve = \n"
|
||||||
|
content15: "{{ output_content.content | b64decode }}"
|
||||||
|
- debug: var=content15
|
||||||
|
- name: Verify content of ini file is as expected
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- content15 == expected15
|
||||||
|
|
Loading…
Reference in a new issue