ini_file: add allow_no_value param (#24442)
This commit is contained in:
parent
7d0e1f92f4
commit
daeec920b0
2 changed files with 159 additions and 10 deletions
|
@ -75,6 +75,13 @@ options:
|
||||||
type: bool
|
type: bool
|
||||||
default: 'yes'
|
default: 'yes'
|
||||||
version_added: "2.2"
|
version_added: "2.2"
|
||||||
|
allow_no_value:
|
||||||
|
description:
|
||||||
|
- allow option without value and without '=' symbol
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
version_added: "2.6"
|
||||||
notes:
|
notes:
|
||||||
- While it is possible to add an I(option) without specifying a I(value), this makes
|
- While it is possible to add an I(option) without specifying a I(value), this makes
|
||||||
no sense.
|
no sense.
|
||||||
|
@ -114,18 +121,19 @@ 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) \
|
||||||
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):
|
||||||
option = re.escape(option)
|
option = re.escape(option)
|
||||||
return re.match('( |\t)*%s( |\t)*=' % option, line)
|
return re.match('( |\t)*%s( |\t)*(=|$)' % option, line)
|
||||||
|
|
||||||
|
|
||||||
def do_ini(module, filename, section=None, option=None, value=None,
|
def do_ini(module, filename, section=None, option=None, value=None,
|
||||||
state='present', backup=False, no_extra_spaces=False, create=True):
|
state='present', backup=False, no_extra_spaces=False, create=True,
|
||||||
|
allow_no_value=False):
|
||||||
|
|
||||||
diff = dict(
|
diff = dict(
|
||||||
before='',
|
before='',
|
||||||
|
@ -184,7 +192,10 @@ 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 re.match(r'^[ \t]*([#;].*)?$', ini_lines[i - 1]):
|
if not re.match(r'^[ \t]*([#;].*)?$', ini_lines[i - 1]):
|
||||||
ini_lines.insert(i, assignment_format % (option, value))
|
if not value and allow_no_value:
|
||||||
|
ini_lines.insert(i, '%s\n' % option)
|
||||||
|
else:
|
||||||
|
ini_lines.insert(i, assignment_format % (option, value))
|
||||||
msg = 'option added'
|
msg = 'option added'
|
||||||
changed = True
|
changed = True
|
||||||
break
|
break
|
||||||
|
@ -199,7 +210,10 @@ 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):
|
||||||
newline = assignment_format % (option, value)
|
if not value and allow_no_value:
|
||||||
|
newline = '%s\n' % option
|
||||||
|
else:
|
||||||
|
newline = assignment_format % (option, value)
|
||||||
option_changed = ini_lines[index] != newline
|
option_changed = ini_lines[index] != newline
|
||||||
changed = changed or option_changed
|
changed = changed or option_changed
|
||||||
if option_changed:
|
if option_changed:
|
||||||
|
@ -230,7 +244,10 @@ def do_ini(module, filename, section=None, option=None, value=None,
|
||||||
|
|
||||||
if not within_section and option and state == 'present':
|
if not within_section and option and state == 'present':
|
||||||
ini_lines.append('[%s]\n' % section)
|
ini_lines.append('[%s]\n' % section)
|
||||||
ini_lines.append(assignment_format % (option, value))
|
if not value and allow_no_value:
|
||||||
|
ini_lines.append('%s\n' % option)
|
||||||
|
else:
|
||||||
|
ini_lines.append(assignment_format % (option, value))
|
||||||
changed = True
|
changed = True
|
||||||
msg = 'section and option added'
|
msg = 'section and option added'
|
||||||
|
|
||||||
|
@ -270,6 +287,7 @@ def main():
|
||||||
backup=dict(type='bool', default=False),
|
backup=dict(type='bool', default=False),
|
||||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||||
no_extra_spaces=dict(type='bool', default=False),
|
no_extra_spaces=dict(type='bool', default=False),
|
||||||
|
allow_no_value=dict(type='bool', default=False, required=False),
|
||||||
create=dict(type='bool', default=True)
|
create=dict(type='bool', default=True)
|
||||||
),
|
),
|
||||||
add_file_common_args=True,
|
add_file_common_args=True,
|
||||||
|
@ -283,9 +301,10 @@ def main():
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
backup = module.params['backup']
|
backup = module.params['backup']
|
||||||
no_extra_spaces = module.params['no_extra_spaces']
|
no_extra_spaces = module.params['no_extra_spaces']
|
||||||
|
allow_no_value = module.params['allow_no_value']
|
||||||
create = module.params['create']
|
create = module.params['create']
|
||||||
|
|
||||||
(changed, backup_file, diff, msg) = do_ini(module, path, section, option, value, state, backup, no_extra_spaces, create)
|
(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):
|
if not module.check_mode and os.path.exists(path):
|
||||||
file_args = module.load_file_common_arguments(module.params)
|
file_args = module.load_file_common_arguments(module.params)
|
||||||
|
|
|
@ -115,9 +115,139 @@
|
||||||
set_fact:
|
set_fact:
|
||||||
content5: "{{ lookup('file', output_file) }}"
|
content5: "{{ lookup('file', output_file) }}"
|
||||||
|
|
||||||
- name: assert changed
|
- name: assert changed and content is empty
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result5.changed == True
|
- result5.changed == True
|
||||||
- result5.msg == 'section removed'
|
- result5.msg == 'section removed'
|
||||||
- content5 == ""
|
- content5 == ""
|
||||||
|
|
||||||
|
# allow_no_value
|
||||||
|
|
||||||
|
- name: test allow_no_value
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: skip-name
|
||||||
|
allow_no_value: yes
|
||||||
|
register: result6
|
||||||
|
|
||||||
|
- name: assert section and option added
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result6.changed == True
|
||||||
|
- result6.msg == 'section and option added'
|
||||||
|
|
||||||
|
- name: test allow_no_value idempotency
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: skip-name
|
||||||
|
allow_no_value: yes
|
||||||
|
register: result6
|
||||||
|
|
||||||
|
- name: assert 'changed' false
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result6.changed == False
|
||||||
|
- result6.msg == 'OK'
|
||||||
|
|
||||||
|
- name: test allow_no_value with loop
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: "{{ item.o }}"
|
||||||
|
value: "{{ item.v }}"
|
||||||
|
allow_no_value: yes
|
||||||
|
with_items:
|
||||||
|
- { o: "skip-name-resolve", v: null }
|
||||||
|
- { o: "max_connections", v: "500" }
|
||||||
|
|
||||||
|
- name: set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
content7: "{{ lookup('file', output_file) }}"
|
||||||
|
expected7: |-
|
||||||
|
|
||||||
|
[mysqld]
|
||||||
|
skip-name
|
||||||
|
skip-name-resolve
|
||||||
|
max_connections = 500
|
||||||
|
|
||||||
|
- name: Verify content of ini file is as expected
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- content7 == expected7
|
||||||
|
|
||||||
|
- name: change option with no value to option with value
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: skip-name
|
||||||
|
value: myvalue
|
||||||
|
register: result8
|
||||||
|
|
||||||
|
- name: set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
content8: "{{ lookup('file', output_file) }}"
|
||||||
|
expected8: |-
|
||||||
|
|
||||||
|
[mysqld]
|
||||||
|
skip-name = myvalue
|
||||||
|
skip-name-resolve
|
||||||
|
max_connections = 500
|
||||||
|
|
||||||
|
- name: assert 'changed' and msg 'option changed' and content is as expected
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result8.changed == True
|
||||||
|
- result8.msg == 'option changed'
|
||||||
|
- content8 == expected8
|
||||||
|
|
||||||
|
- name: change option with value to option with no value
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: skip-name
|
||||||
|
allow_no_value: yes
|
||||||
|
register: result9
|
||||||
|
|
||||||
|
- name: set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
content9: "{{ lookup('file', output_file) }}"
|
||||||
|
expected9: |-
|
||||||
|
|
||||||
|
[mysqld]
|
||||||
|
skip-name
|
||||||
|
skip-name-resolve
|
||||||
|
max_connections = 500
|
||||||
|
|
||||||
|
- name: assert 'changed' and msg 'option changed' and content is as expected
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result9.changed == True
|
||||||
|
- result9.msg == 'option changed'
|
||||||
|
- content9 == expected9
|
||||||
|
|
||||||
|
- name: Remove option with no value
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: mysqld
|
||||||
|
option: skip-name-resolve
|
||||||
|
state: absent
|
||||||
|
register: result10
|
||||||
|
|
||||||
|
- name: set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
content10: "{{ lookup('file', output_file) }}"
|
||||||
|
expected10: |-
|
||||||
|
|
||||||
|
[mysqld]
|
||||||
|
skip-name
|
||||||
|
max_connections = 500
|
||||||
|
|
||||||
|
- name: assert 'changed' and msg 'option changed' and content is as expected
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result10.changed == True
|
||||||
|
- result10.msg == 'option changed'
|
||||||
|
- content10 == expected10
|
||||||
|
|
Loading…
Reference in a new issue