2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
|
|
|
# Copyright (c) 2015, Ales Nosek <anosek.nosek () gmail.com>
|
|
|
|
# Copyright (c) 2017, 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
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: ini_file
|
|
|
|
short_description: Tweak settings in INI files
|
2023-02-20 17:28:47 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- files
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Manage (add, remove, change) individual settings in an INI-style file without having
|
|
|
|
to manage the file as a whole with, say, M(ansible.builtin.template) or M(ansible.builtin.assemble).
|
|
|
|
- Adds missing sections if they don't exist.
|
|
|
|
- Before Ansible 2.0, comments are discarded when the source file is read, and therefore will not show up in the destination file.
|
|
|
|
- Since Ansible 2.3, this module adds missing ending newlines to files to keep in line with the POSIX standard, even when
|
|
|
|
no other modifications need to be applied.
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- Path to the INI-style file; this file is created if required.
|
|
|
|
- Before Ansible 2.3 this option was only usable as I(dest).
|
|
|
|
type: path
|
|
|
|
required: true
|
|
|
|
aliases: [ dest ]
|
|
|
|
section:
|
|
|
|
description:
|
2022-09-06 07:32:35 +02:00
|
|
|
- Section name in INI file. This is added if I(state=present) automatically when
|
2020-03-09 10:11:07 +01:00
|
|
|
a single value is being set.
|
2023-04-23 16:51:18 +02:00
|
|
|
- If left empty, being omitted, or being set to C(null), the I(option) will be placed before the first I(section).
|
2020-03-09 10:11:07 +01:00
|
|
|
- Using C(null) is also required if the config format does not support sections.
|
|
|
|
type: str
|
|
|
|
option:
|
|
|
|
description:
|
|
|
|
- If set (required for changing a I(value)), this is the name of the option.
|
|
|
|
- May be omitted if adding/removing a whole I(section).
|
|
|
|
type: str
|
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- The string value to be associated with an I(option).
|
|
|
|
- May be omitted when removing an I(option).
|
2021-08-15 12:59:50 +02:00
|
|
|
- Mutually exclusive with I(values).
|
|
|
|
- I(value=v) is equivalent to I(values=[v]).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
2021-08-15 12:59:50 +02:00
|
|
|
values:
|
|
|
|
description:
|
|
|
|
- The string value to be associated with an I(option).
|
|
|
|
- May be omitted when removing an I(option).
|
|
|
|
- Mutually exclusive with I(value).
|
|
|
|
- I(value=v) is equivalent to I(values=[v]).
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
version_added: 3.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
backup:
|
|
|
|
description:
|
|
|
|
- Create a backup file including the timestamp information so you can get
|
|
|
|
the original file back if you somehow clobbered it incorrectly.
|
|
|
|
type: bool
|
2022-08-24 20:00:26 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2022-08-24 20:00:26 +02:00
|
|
|
- If set to C(absent) and I(exclusive) set to C(true) all matching I(option) lines are removed.
|
2022-09-06 07:32:35 +02:00
|
|
|
- If set to C(absent) and I(exclusive) set to C(false) the specified I(option=value) lines are removed,
|
2021-08-15 12:59:50 +02:00
|
|
|
but the other I(option)s with the same name are not touched.
|
2022-09-06 07:32:35 +02:00
|
|
|
- If set to C(present) and I(exclusive) set to C(false) the specified I(option=values) lines are added,
|
2021-08-15 12:59:50 +02:00
|
|
|
but the other I(option)s with the same name are not touched.
|
2022-09-06 07:32:35 +02:00
|
|
|
- If set to C(present) and I(exclusive) set to C(true) all given I(option=values) lines will be
|
2021-08-15 12:59:50 +02:00
|
|
|
added and the other I(option)s with the same name are removed.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
2021-08-15 12:59:50 +02:00
|
|
|
exclusive:
|
|
|
|
description:
|
2022-08-24 20:00:26 +02:00
|
|
|
- If set to C(true) (default), all matching I(option) lines are removed when I(state=absent),
|
2021-08-15 12:59:50 +02:00
|
|
|
or replaced when I(state=present).
|
2022-08-24 20:00:26 +02:00
|
|
|
- If set to C(false), only the specified I(value(s)) are added when I(state=present),
|
2021-08-15 12:59:50 +02:00
|
|
|
or removed when I(state=absent), and existing ones are not modified.
|
|
|
|
type: bool
|
2022-08-24 20:00:26 +02:00
|
|
|
default: true
|
2021-08-15 12:59:50 +02:00
|
|
|
version_added: 3.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
no_extra_spaces:
|
|
|
|
description:
|
|
|
|
- Do not insert spaces before and after '=' symbol.
|
|
|
|
type: bool
|
2022-08-24 20:00:26 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
create:
|
|
|
|
description:
|
2022-08-24 20:00:26 +02:00
|
|
|
- If set to C(false), the module will fail if the file does not already exist.
|
2020-03-09 10:11:07 +01:00
|
|
|
- By default it will create the file if it is missing.
|
|
|
|
type: bool
|
2022-08-24 20:00:26 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
allow_no_value:
|
|
|
|
description:
|
|
|
|
- Allow option without value and without '=' symbol.
|
|
|
|
type: bool
|
2022-08-24 20:00:26 +02:00
|
|
|
default: false
|
2023-05-29 18:56:06 +02:00
|
|
|
follow:
|
|
|
|
description:
|
|
|
|
- This flag indicates that filesystem links, if they exist, should be followed.
|
|
|
|
- I(follow=true) can modify I(src) when combined with parameters such as I(mode).
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 7.1.0
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
|
|
|
- While it is possible to add an I(option) without specifying a I(value), this makes no sense.
|
|
|
|
- As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but I(dest) still works as well.
|
2021-05-26 07:06:23 +02:00
|
|
|
- As of community.general 3.2.0, UTF-8 BOM markers are discarded when reading files.
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- Jan-Piet Mens (@jpmens)
|
|
|
|
- Ales Nosek (@noseka1)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
# Before Ansible 2.3, option 'dest' was used instead of 'path'
|
|
|
|
- name: Ensure "fav=lemonade is in section "[drinks]" in specified file
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.ini_file:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /etc/conf
|
|
|
|
section: drinks
|
|
|
|
option: fav
|
|
|
|
value: lemonade
|
|
|
|
mode: '0600'
|
2022-08-24 20:00:26 +02:00
|
|
|
backup: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Ensure "temperature=cold is in section "[drinks]" in specified file
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.ini_file:
|
2020-03-09 10:11:07 +01:00
|
|
|
path: /etc/anotherconf
|
|
|
|
section: drinks
|
|
|
|
option: temperature
|
|
|
|
value: cold
|
2022-08-24 20:00:26 +02:00
|
|
|
backup: true
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
- name: Add "beverage=lemon juice" is in section "[drinks]" in specified file
|
|
|
|
community.general.ini_file:
|
|
|
|
path: /etc/conf
|
|
|
|
section: drinks
|
|
|
|
option: beverage
|
|
|
|
value: lemon juice
|
|
|
|
mode: '0600'
|
|
|
|
state: present
|
2022-08-24 20:00:26 +02:00
|
|
|
exclusive: false
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
- name: Ensure multiple values "beverage=coke" and "beverage=pepsi" are in section "[drinks]" in specified file
|
|
|
|
community.general.ini_file:
|
|
|
|
path: /etc/conf
|
|
|
|
section: drinks
|
|
|
|
option: beverage
|
|
|
|
values:
|
|
|
|
- coke
|
|
|
|
- pepsi
|
|
|
|
mode: '0600'
|
|
|
|
state: present
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
2021-05-22 22:20:37 +02:00
|
|
|
import io
|
2020-03-09 10:11:07 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-27 10:00:01 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def match_opt(option, line):
|
|
|
|
option = re.escape(option)
|
2021-08-15 12:59:50 +02:00
|
|
|
return re.match('[#;]?( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def match_active_opt(option, line):
|
|
|
|
option = re.escape(option)
|
2021-08-15 12:59:50 +02:00
|
|
|
return re.match('( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-15 12:59:50 +02:00
|
|
|
def update_section_line(changed, section_lines, index, changed_lines, newline, msg):
|
|
|
|
option_changed = section_lines[index] != newline
|
|
|
|
changed = changed or option_changed
|
|
|
|
if option_changed:
|
|
|
|
msg = 'option changed'
|
|
|
|
section_lines[index] = newline
|
|
|
|
changed_lines[index] = 1
|
|
|
|
return (changed, msg)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
def do_ini(module, filename, section=None, option=None, values=None,
|
|
|
|
state='present', exclusive=True, backup=False, no_extra_spaces=False,
|
2023-05-29 18:56:06 +02:00
|
|
|
create=True, allow_no_value=False, follow=False):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-06-27 10:00:01 +02:00
|
|
|
if section is not None:
|
|
|
|
section = to_text(section)
|
|
|
|
if option is not None:
|
|
|
|
option = to_text(option)
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
# deduplicate entries in values
|
|
|
|
values_unique = []
|
|
|
|
[values_unique.append(to_text(value)) for value in values if value not in values_unique and value is not None]
|
|
|
|
values = values_unique
|
2021-06-27 10:00:01 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
diff = dict(
|
|
|
|
before='',
|
|
|
|
after='',
|
|
|
|
before_header='%s (content)' % filename,
|
|
|
|
after_header='%s (content)' % filename,
|
|
|
|
)
|
|
|
|
|
2023-05-29 18:56:06 +02:00
|
|
|
if follow and os.path.islink(filename):
|
|
|
|
target_filename = os.path.realpath(filename)
|
|
|
|
else:
|
|
|
|
target_filename = filename
|
|
|
|
|
|
|
|
if not os.path.exists(target_filename):
|
2020-03-09 10:11:07 +01:00
|
|
|
if not create:
|
2023-05-29 18:56:06 +02:00
|
|
|
module.fail_json(rc=257, msg='Destination %s does not exist!' % target_filename)
|
|
|
|
destpath = os.path.dirname(target_filename)
|
2020-03-09 10:11:07 +01:00
|
|
|
if not os.path.exists(destpath) and not module.check_mode:
|
|
|
|
os.makedirs(destpath)
|
|
|
|
ini_lines = []
|
|
|
|
else:
|
2023-05-29 18:56:06 +02:00
|
|
|
with io.open(target_filename, 'r', encoding="utf-8-sig") as ini_file:
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines = [to_text(line) for line in ini_file.readlines()]
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if module._diff:
|
2021-06-27 10:00:01 +02:00
|
|
|
diff['before'] = u''.join(ini_lines)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
# ini file could be empty
|
|
|
|
if not ini_lines:
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines.append(u'\n')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# last line of file may not contain a trailing newline
|
2021-06-27 10:00:01 +02:00
|
|
|
if ini_lines[-1] == u"" or ini_lines[-1][-1] != u'\n':
|
|
|
|
ini_lines[-1] += u'\n'
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
# append fake section lines to simplify the logic
|
|
|
|
# At top:
|
|
|
|
# Fake random section to do not match any other in the file
|
|
|
|
# Using commit hash as fake section name
|
2021-06-27 10:00:01 +02:00
|
|
|
fake_section_name = u"ad01e11446efb704fcdbdb21f2c43757423d91c5"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Insert it at the beginning
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines.insert(0, u'[%s]' % fake_section_name)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-12-28 10:47:01 +01:00
|
|
|
# At bottom:
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines.append(u'[')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# If no section is defined, fake section is used
|
|
|
|
if not section:
|
|
|
|
section = fake_section_name
|
|
|
|
|
|
|
|
within_section = not section
|
2021-08-15 12:59:50 +02:00
|
|
|
section_start = section_end = 0
|
2020-03-09 10:11:07 +01:00
|
|
|
msg = 'OK'
|
|
|
|
if no_extra_spaces:
|
2021-06-27 10:00:01 +02:00
|
|
|
assignment_format = u'%s=%s\n'
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-06-27 10:00:01 +02:00
|
|
|
assignment_format = u'%s = %s\n'
|
|
|
|
|
2021-08-15 12:59:50 +02:00
|
|
|
option_no_value_present = False
|
|
|
|
|
2021-06-27 10:00:01 +02:00
|
|
|
non_blank_non_comment_pattern = re.compile(to_text(r'^[ \t]*([#;].*)?$'))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-15 12:59:50 +02:00
|
|
|
before = after = []
|
|
|
|
section_lines = []
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
for index, line in enumerate(ini_lines):
|
2021-08-15 12:59:50 +02:00
|
|
|
# find start and end of section
|
2021-06-27 10:00:01 +02:00
|
|
|
if line.startswith(u'[%s]' % section):
|
2020-03-09 10:11:07 +01:00
|
|
|
within_section = True
|
|
|
|
section_start = index
|
2021-06-27 10:00:01 +02:00
|
|
|
elif line.startswith(u'['):
|
2020-03-09 10:11:07 +01:00
|
|
|
if within_section:
|
2021-08-15 12:59:50 +02:00
|
|
|
section_end = index
|
|
|
|
break
|
|
|
|
|
|
|
|
before = ini_lines[0:section_start]
|
|
|
|
section_lines = ini_lines[section_start:section_end]
|
|
|
|
after = ini_lines[section_end:len(ini_lines)]
|
|
|
|
|
|
|
|
# Keep track of changed section_lines
|
|
|
|
changed_lines = [0] * len(section_lines)
|
|
|
|
|
|
|
|
# handling multiple instances of option=value when state is 'present' with/without exclusive is a bit complex
|
|
|
|
#
|
|
|
|
# 1. edit all lines where we have a option=value pair with a matching value in values[]
|
2022-07-08 23:11:57 +02:00
|
|
|
# 2. edit all the remaining lines where we have a matching option
|
2021-08-15 12:59:50 +02:00
|
|
|
# 3. delete remaining lines where we have a matching option
|
|
|
|
# 4. insert missing option line(s) at the end of the section
|
|
|
|
|
|
|
|
if state == 'present' and option:
|
|
|
|
for index, line in enumerate(section_lines):
|
|
|
|
if match_opt(option, line):
|
|
|
|
match = match_opt(option, line)
|
|
|
|
if values and match.group(6) in values:
|
|
|
|
matched_value = match.group(6)
|
|
|
|
if not matched_value and allow_no_value:
|
|
|
|
# replace existing option with no value line(s)
|
|
|
|
newline = u'%s\n' % option
|
|
|
|
option_no_value_present = True
|
|
|
|
else:
|
|
|
|
# replace existing option=value line(s)
|
|
|
|
newline = assignment_format % (option, matched_value)
|
|
|
|
(changed, msg) = update_section_line(changed, section_lines, index, changed_lines, newline, msg)
|
|
|
|
values.remove(matched_value)
|
|
|
|
elif not values and allow_no_value:
|
|
|
|
# replace existing option with no value line(s)
|
|
|
|
newline = u'%s\n' % option
|
|
|
|
(changed, msg) = update_section_line(changed, section_lines, index, changed_lines, newline, msg)
|
|
|
|
option_no_value_present = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if state == 'present' and exclusive and not allow_no_value:
|
|
|
|
# override option with no value to option with value if not allow_no_value
|
|
|
|
if len(values) > 0:
|
|
|
|
for index, line in enumerate(section_lines):
|
2022-09-25 16:28:06 +02:00
|
|
|
if not changed_lines[index] and match_active_opt(option, line):
|
2021-08-15 12:59:50 +02:00
|
|
|
newline = assignment_format % (option, values.pop(0))
|
|
|
|
(changed, msg) = update_section_line(changed, section_lines, index, changed_lines, newline, msg)
|
|
|
|
if len(values) == 0:
|
|
|
|
break
|
|
|
|
# remove all remaining option occurrences from the rest of the section
|
|
|
|
for index in range(len(section_lines) - 1, 0, -1):
|
|
|
|
if not changed_lines[index] and match_active_opt(option, section_lines[index]):
|
|
|
|
del section_lines[index]
|
|
|
|
del changed_lines[index]
|
|
|
|
changed = True
|
|
|
|
msg = 'option changed'
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
# insert missing option line(s) at the end of the section
|
|
|
|
for index in range(len(section_lines), 0, -1):
|
|
|
|
# search backwards for previous non-blank or non-comment line
|
|
|
|
if not non_blank_non_comment_pattern.match(section_lines[index - 1]):
|
|
|
|
if option and values:
|
|
|
|
# insert option line(s)
|
|
|
|
for element in values[::-1]:
|
|
|
|
# items are added backwards, so traverse the list backwards to not confuse the user
|
|
|
|
# otherwise some of their options might appear in reverse order for whatever fancy reason ¯\_(ツ)_/¯
|
|
|
|
if element is not None:
|
|
|
|
# insert option=value line
|
|
|
|
section_lines.insert(index, assignment_format % (option, element))
|
|
|
|
msg = 'option added'
|
|
|
|
changed = True
|
|
|
|
elif element is None and allow_no_value:
|
|
|
|
# insert option with no value line
|
|
|
|
section_lines.insert(index, u'%s\n' % option)
|
|
|
|
msg = 'option added'
|
|
|
|
changed = True
|
|
|
|
elif option and not values and allow_no_value and not option_no_value_present:
|
|
|
|
# insert option with no value line(s)
|
|
|
|
section_lines.insert(index, u'%s\n' % option)
|
|
|
|
msg = 'option added'
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
break
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if option:
|
|
|
|
if exclusive:
|
|
|
|
# delete all option line(s) with given option and ignore value
|
|
|
|
new_section_lines = [line for line in section_lines if not (match_active_opt(option, line))]
|
|
|
|
if section_lines != new_section_lines:
|
|
|
|
changed = True
|
|
|
|
msg = 'option changed'
|
|
|
|
section_lines = new_section_lines
|
|
|
|
elif not exclusive and len(values) > 0:
|
|
|
|
# delete specified option=value line(s)
|
|
|
|
new_section_lines = [i for i in section_lines if not (match_active_opt(option, i) and match_active_opt(option, i).group(6) in values)]
|
|
|
|
if section_lines != new_section_lines:
|
|
|
|
changed = True
|
|
|
|
msg = 'option changed'
|
|
|
|
section_lines = new_section_lines
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-08-15 12:59:50 +02:00
|
|
|
# drop the entire section
|
2022-02-17 13:35:46 +01:00
|
|
|
if section_lines:
|
|
|
|
section_lines = []
|
|
|
|
msg = 'section removed'
|
|
|
|
changed = True
|
2021-08-15 12:59:50 +02:00
|
|
|
|
|
|
|
# reassemble the ini_lines after manipulation
|
|
|
|
ini_lines = before + section_lines + after
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# remove the fake section line
|
|
|
|
del ini_lines[0]
|
|
|
|
del ini_lines[-1:]
|
|
|
|
|
2020-12-28 10:47:01 +01:00
|
|
|
if not within_section and state == 'present':
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines.append(u'[%s]\n' % section)
|
2020-12-28 10:47:01 +01:00
|
|
|
msg = 'section and option added'
|
2021-08-15 12:59:50 +02:00
|
|
|
if option and values:
|
|
|
|
for value in values:
|
|
|
|
ini_lines.append(assignment_format % (option, value))
|
|
|
|
elif option and not values and allow_no_value:
|
2021-06-27 10:00:01 +02:00
|
|
|
ini_lines.append(u'%s\n' % option)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2020-12-28 10:47:01 +01:00
|
|
|
msg = 'only section added'
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
if module._diff:
|
2021-06-27 10:00:01 +02:00
|
|
|
diff['after'] = u''.join(ini_lines)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
backup_file = None
|
|
|
|
if changed and not module.check_mode:
|
|
|
|
if backup:
|
2023-05-29 18:56:06 +02:00
|
|
|
backup_file = module.backup_local(target_filename)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-06-27 10:00:01 +02:00
|
|
|
encoded_ini_lines = [to_bytes(line) for line in ini_lines]
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
|
2021-06-27 10:00:01 +02:00
|
|
|
f = os.fdopen(tmpfd, 'wb')
|
|
|
|
f.writelines(encoded_ini_lines)
|
2020-03-09 10:11:07 +01:00
|
|
|
f.close()
|
|
|
|
except IOError:
|
|
|
|
module.fail_json(msg="Unable to create temporary file %s", traceback=traceback.format_exc())
|
|
|
|
|
|
|
|
try:
|
2023-05-29 18:56:06 +02:00
|
|
|
module.atomic_move(tmpfile, target_filename)
|
2020-03-09 10:11:07 +01:00
|
|
|
except IOError:
|
|
|
|
module.ansible.fail_json(msg='Unable to move temporary \
|
2023-05-29 18:56:06 +02:00
|
|
|
file %s to %s, IOError' % (tmpfile, target_filename), traceback=traceback.format_exc())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
return (changed, backup_file, diff, msg)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
path=dict(type='path', required=True, aliases=['dest']),
|
2023-04-23 16:51:18 +02:00
|
|
|
section=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
option=dict(type='str'),
|
|
|
|
value=dict(type='str'),
|
2021-08-15 12:59:50 +02:00
|
|
|
values=dict(type='list', elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
backup=dict(type='bool', default=False),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
2021-08-15 12:59:50 +02:00
|
|
|
exclusive=dict(type='bool', default=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
no_extra_spaces=dict(type='bool', default=False),
|
|
|
|
allow_no_value=dict(type='bool', default=False),
|
2023-05-29 18:56:06 +02:00
|
|
|
create=dict(type='bool', default=True),
|
|
|
|
follow=dict(type='bool', default=False)
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2021-08-15 12:59:50 +02:00
|
|
|
mutually_exclusive=[
|
|
|
|
['value', 'values']
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
add_file_common_args=True,
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
path = module.params['path']
|
|
|
|
section = module.params['section']
|
|
|
|
option = module.params['option']
|
|
|
|
value = module.params['value']
|
2021-08-15 12:59:50 +02:00
|
|
|
values = module.params['values']
|
2020-03-09 10:11:07 +01:00
|
|
|
state = module.params['state']
|
2021-08-15 12:59:50 +02:00
|
|
|
exclusive = module.params['exclusive']
|
2020-03-09 10:11:07 +01:00
|
|
|
backup = module.params['backup']
|
|
|
|
no_extra_spaces = module.params['no_extra_spaces']
|
|
|
|
allow_no_value = module.params['allow_no_value']
|
|
|
|
create = module.params['create']
|
2023-05-29 18:56:06 +02:00
|
|
|
follow = module.params['follow']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-15 12:59:50 +02:00
|
|
|
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.")
|
|
|
|
|
|
|
|
if value is not None:
|
|
|
|
values = [value]
|
|
|
|
elif values is None:
|
|
|
|
values = []
|
2020-12-28 10:47:01 +01:00
|
|
|
|
2023-05-29 18:56:06 +02:00
|
|
|
(changed, backup_file, diff, msg) = do_ini(module, path, section, option, values, state, exclusive, backup, no_extra_spaces, create, allow_no_value, follow)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not module.check_mode and os.path.exists(path):
|
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
|
|
|
|
|
|
|
results = dict(
|
|
|
|
changed=changed,
|
|
|
|
diff=diff,
|
|
|
|
msg=msg,
|
|
|
|
path=path,
|
|
|
|
)
|
|
|
|
if backup_file is not None:
|
|
|
|
results['backup_file'] = backup_file
|
|
|
|
|
|
|
|
# Mission complete
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|