mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-28 11:59:09 -07:00
ini_file: fixed case of empty section + parameter validation (#1533)
* Fixed case of empty section + parameter validation * Adjusted code for integration test, and added test case for empty section * Update changelogs/fragments/479-ini_file-empty-section.yaml Co-authored-by: Felix Fontein <felix@fontein.de> * Update changelogs/fragments/479-ini_file-empty-section.yaml Co-authored-by: Felix Fontein <felix@fontein.de> * Using "is changed" in integration tests, per PR review Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
39bf187a25
commit
2b824f2d7b
3 changed files with 65 additions and 24 deletions
|
@ -143,11 +143,8 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
|||
os.makedirs(destpath)
|
||||
ini_lines = []
|
||||
else:
|
||||
ini_file = open(filename, 'r')
|
||||
try:
|
||||
with open(filename, 'r') as ini_file:
|
||||
ini_lines = ini_file.readlines()
|
||||
finally:
|
||||
ini_file.close()
|
||||
|
||||
if module._diff:
|
||||
diff['before'] = ''.join(ini_lines)
|
||||
|
@ -172,7 +169,7 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
|||
# Insert it at the beginning
|
||||
ini_lines.insert(0, '[%s]' % fake_section_name)
|
||||
|
||||
# At botton:
|
||||
# At bottom:
|
||||
ini_lines.append('[')
|
||||
|
||||
# If no section is defined, fake section is used
|
||||
|
@ -198,12 +195,14 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
|||
for i in range(index, 0, -1):
|
||||
# search backwards for previous non-blank or non-comment line
|
||||
if not re.match(r'^[ \t]*([#;].*)?$', ini_lines[i - 1]):
|
||||
if not value and allow_no_value:
|
||||
ini_lines.insert(i, '%s\n' % option)
|
||||
else:
|
||||
if option and value:
|
||||
ini_lines.insert(i, assignment_format % (option, value))
|
||||
msg = 'option added'
|
||||
changed = True
|
||||
msg = 'option added'
|
||||
changed = True
|
||||
elif option and not value and allow_no_value:
|
||||
ini_lines.insert(i, '%s\n' % option)
|
||||
msg = 'option added'
|
||||
changed = True
|
||||
break
|
||||
elif state == 'absent' and not option:
|
||||
# remove the entire section
|
||||
|
@ -249,14 +248,16 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
|||
del ini_lines[0]
|
||||
del ini_lines[-1:]
|
||||
|
||||
if not within_section and option and state == 'present':
|
||||
if not within_section and state == 'present':
|
||||
ini_lines.append('[%s]\n' % section)
|
||||
if not value and allow_no_value:
|
||||
msg = 'section and option added'
|
||||
if option and value:
|
||||
ini_lines.append(assignment_format % (option, value))
|
||||
elif option and not value and allow_no_value:
|
||||
ini_lines.append('%s\n' % option)
|
||||
else:
|
||||
ini_lines.append(assignment_format % (option, value))
|
||||
msg = 'only section added'
|
||||
changed = True
|
||||
msg = 'section and option added'
|
||||
|
||||
if module._diff:
|
||||
diff['after'] = ''.join(ini_lines)
|
||||
|
@ -311,6 +312,9 @@ def main():
|
|||
allow_no_value = module.params['allow_no_value']
|
||||
create = module.params['create']
|
||||
|
||||
if state == 'present' and not allow_no_value and not value:
|
||||
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)
|
||||
|
||||
if not module.check_mode and os.path.exists(path):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue