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

Adding parameter to prefix option with spaces for ini_file

This commit is contained in:
Trevor Schellhorn 2024-07-14 20:50:57 -06:00
parent a78f7b1e6a
commit 3cab65f622
3 changed files with 135 additions and 3 deletions

View file

@ -153,6 +153,12 @@ options:
type: bool type: bool
default: false default: false
version_added: 7.1.0 version_added: 7.1.0
option_prefix_spaces:
description:
- This flag indicates that the option should start with spaces.
type: int
default: 0
version_added: 8.0.0
notes: notes:
- While it is possible to add an O(option) without specifying a O(value), this makes no sense. - While it is possible to add an O(option) without specifying a O(value), this makes no sense.
- As of community.general 3.2.0, UTF-8 BOM markers are discarded when reading files. - As of community.general 3.2.0, UTF-8 BOM markers are discarded when reading files.
@ -257,6 +263,14 @@ EXAMPLES = r'''
value: xxxxxxxxxxxxxxxxxxxx value: xxxxxxxxxxxxxxxxxxxx
mode: '0600' mode: '0600'
state: present state: present
- name: Update the option and indent with spaces
community.general.ini_file:
path: /etc/influxdb/config.toml
section: default
option: url
value: http://localhost:8086
option_prefix_spaces: 2
state: present
''' '''
import io import io
@ -311,7 +325,8 @@ def check_section_has_values(section_has_values, section_lines):
def do_ini(module, filename, section=None, section_has_values=None, option=None, values=None, def do_ini(module, filename, section=None, section_has_values=None, option=None, values=None,
state='present', exclusive=True, backup=False, no_extra_spaces=False, state='present', exclusive=True, backup=False, no_extra_spaces=False,
ignore_spaces=False, create=True, allow_no_value=False, modify_inactive_option=True, follow=False): ignore_spaces=False, create=True, allow_no_value=False, modify_inactive_option=True,
follow=False, option_prefix_spaces=0):
if section is not None: if section is not None:
section = to_text(section) section = to_text(section)
@ -384,6 +399,9 @@ def do_ini(module, filename, section=None, section_has_values=None, option=None,
else: else:
assignment_format = u'%s = %s\n' assignment_format = u'%s = %s\n'
if option_prefix_spaces > 0:
assignment_format = (' ' * option_prefix_spaces) + assignment_format
option_no_value_present = False option_no_value_present = False
non_blank_non_comment_pattern = re.compile(to_text(r'^[ \t]*([#;].*)?$')) non_blank_non_comment_pattern = re.compile(to_text(r'^[ \t]*([#;].*)?$'))
@ -599,7 +617,8 @@ def main():
allow_no_value=dict(type='bool', default=False), allow_no_value=dict(type='bool', default=False),
modify_inactive_option=dict(type='bool', default=True), modify_inactive_option=dict(type='bool', default=True),
create=dict(type='bool', default=True), create=dict(type='bool', default=True),
follow=dict(type='bool', default=False) follow=dict(type='bool', default=False),
option_prefix_spaces=dict(type='int', default=0)
), ),
mutually_exclusive=[ mutually_exclusive=[
['value', 'values'] ['value', 'values']
@ -623,6 +642,7 @@ def main():
modify_inactive_option = module.params['modify_inactive_option'] modify_inactive_option = module.params['modify_inactive_option']
create = module.params['create'] create = module.params['create']
follow = module.params['follow'] follow = module.params['follow']
option_prefix_spaces = module.params['option_prefix_spaces']
if state == 'present' and not allow_no_value and value is None and not values: if state == 'present' and not allow_no_value and value is None and not values:
module.fail_json(msg="Parameter 'value(s)' must be defined if state=present and allow_no_value=False.") module.fail_json(msg="Parameter 'value(s)' must be defined if state=present and allow_no_value=False.")
@ -642,7 +662,8 @@ def main():
(changed, backup_file, diff, msg) = do_ini( (changed, backup_file, diff, msg) = do_ini(
module, path, section, section_has_values, option, values, state, exclusive, backup, module, path, section, section_has_values, option, values, state, exclusive, backup,
no_extra_spaces, ignore_spaces, create, allow_no_value, modify_inactive_option, follow) no_extra_spaces, ignore_spaces, create, allow_no_value, modify_inactive_option,
follow, option_prefix_spaces)
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)

View file

@ -52,3 +52,6 @@
- name: include tasks to test section_has_values - name: include tasks to test section_has_values
include_tasks: tests/08-section.yml include_tasks: tests/08-section.yml
- name: include tasks to test optional spaces at beginning of option lines
include_tasks: tests/09-option_prefix_spaces.yml

View file

@ -0,0 +1,108 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
## testing support for optional spaces at beginning of option lines
- name: Test-options_start_spaces 1 - update option with prefix spaces - create test file
ansible.builtin.copy: # noqa risky-file-permissions
dest: "{{ output_file }}"
content: |
[foo]
; bar=baz
- name: Test-options_start_spaces 1 - update option with prefix spaces - set new value
community.general.ini_file: # noqa risky-file-permissions
path: "{{ output_file }}"
section: 'foo'
option: bar
value: frelt
option_prefix_spaces: 2
register: result
- name: Test-options_start_spaces 1 - update option with prefix spaces - read content from output file
ansible.builtin.slurp:
src: "{{ output_file }}"
register: output_content
- name: Test-options_start_spaces 1 - update option with prefix spaces - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: |
[foo]
bar = frelt
ansible.builtin.assert:
that:
- actual_content == expected_content
- result is changed
- result.msg == 'option changed'
- name: Test-options_start_spaces 2 - set same value with prefix spaces - create test file
ansible.builtin.copy: # noqa risky-file-permissions
dest: "{{ output_file }}"
content: |
[foo]
bar = baz
- name: Test-options_start_spaces 2 - set same value with prefix spaces - set same value
community.general.ini_file: # noqa risky-file-permissions
path: "{{ output_file }}"
section: 'foo'
option: bar
value: baz
option_prefix_spaces: 2
register: result
- name: Test-options_start_spaces 2 - set same value with prefix spaces - read content from output file
ansible.builtin.slurp:
src: "{{ output_file }}"
register: output_content
- name: Test-options_start_spaces 2 - set same value with prefix spaces - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: |
[foo]
bar = baz
ansible.builtin.assert:
that:
- actual_content == expected_content
- result is not changed
- result.msg == 'OK'
- name: Test-options_start_spaces 3 - set new value in new sectio with prefix spaces - create test file
ansible.builtin.copy: # noqa risky-file-permissions
dest: "{{ output_file }}"
content: |
[foo]
bar = baz
- name: Test-options_start_spaces 3 - set new value in new sectio with prefix spaces - set same value
community.general.ini_file: # noqa risky-file-permissions
path: "{{ output_file }}"
section: 'boo'
option: url
value: daz
option_prefix_spaces: 2
register: result
- name: Test-options_start_spaces 3 - set new value in new sectio with prefix spaces - read content from output file
ansible.builtin.slurp:
src: "{{ output_file }}"
register: output_content
- name: Test-options_start_spaces 3 - set new value in new sectio with prefix spaces - verify results
vars:
actual_content: "{{ output_content.content | b64decode }}"
expected_content: |
[foo]
bar = baz
[boo]
url = daz
ansible.builtin.assert:
that:
- actual_content == expected_content
- result is changed
- result.msg == 'section and option added'