From daeec920b05b502d9afca148cc4fc54ecf0570c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 5 Mar 2018 17:05:41 +0100 Subject: [PATCH] ini_file: add allow_no_value param (#24442) --- lib/ansible/modules/files/ini_file.py | 37 +++-- .../targets/ini_file/tasks/main.yml | 132 +++++++++++++++++- 2 files changed, 159 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/files/ini_file.py b/lib/ansible/modules/files/ini_file.py index 35a9dfbcbe3..5e41dd3db41 100644 --- a/lib/ansible/modules/files/ini_file.py +++ b/lib/ansible/modules/files/ini_file.py @@ -75,6 +75,13 @@ options: type: bool default: 'yes' 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: - While it is possible to add an I(option) without specifying a I(value), this makes no sense. @@ -114,18 +121,19 @@ from ansible.module_utils.basic import AnsibleModule def match_opt(option, line): option = re.escape(option) - return re.match('( |\t)*%s( |\t)*=' % option, line) \ - or re.match('#( |\t)*%s( |\t)*=' % option, line) \ - or 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): 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, - 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( before='', @@ -184,7 +192,10 @@ 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]): - 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' changed = True break @@ -199,7 +210,10 @@ def do_ini(module, filename, section=None, option=None, value=None, if state == 'present': # change the existing 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 changed = changed or 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': 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 msg = 'section and option added' @@ -270,6 +287,7 @@ def main(): backup=dict(type='bool', default=False), state=dict(type='str', default='present', choices=['absent', 'present']), no_extra_spaces=dict(type='bool', default=False), + allow_no_value=dict(type='bool', default=False, required=False), create=dict(type='bool', default=True) ), add_file_common_args=True, @@ -283,9 +301,10 @@ def main(): state = module.params['state'] backup = module.params['backup'] no_extra_spaces = module.params['no_extra_spaces'] + allow_no_value = module.params['allow_no_value'] 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): file_args = module.load_file_common_arguments(module.params) diff --git a/test/integration/targets/ini_file/tasks/main.yml b/test/integration/targets/ini_file/tasks/main.yml index 85e203cf83e..2f3eb16919b 100644 --- a/test/integration/targets/ini_file/tasks/main.yml +++ b/test/integration/targets/ini_file/tasks/main.yml @@ -115,9 +115,139 @@ set_fact: content5: "{{ lookup('file', output_file) }}" -- name: assert changed +- name: assert changed and content is empty assert: that: - result5.changed == True - result5.msg == 'section removed' - 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