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

417 lines
15 KiB
Python
Raw Permalink Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016, Roman Belyakovsky <ihryamzik () gmail.com>
# 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 = '''
---
module: interfaces_file
short_description: Tweak settings in /etc/network/interfaces files
extends_documentation_fragment:
- ansible.builtin.files
- community.general.attributes
2020-03-09 10:11:07 +01:00
description:
- Manage (add, remove, change) individual interface options in an interfaces-style file without having
to manage the file as a whole with, say, M(ansible.builtin.template) or M(ansible.builtin.assemble). Interface has to be presented in a file.
- Read information about interfaces from interfaces-styled files
attributes:
check_mode:
support: full
diff_mode:
support: none
2020-03-09 10:11:07 +01:00
options:
dest:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: path
2020-03-09 10:11:07 +01:00
description:
- Path to the interfaces file
default: /etc/network/interfaces
iface:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- Name of the interface, required for value changes or option remove
address_family:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- Address family of the interface, useful if same interface name is used for both inet and inet6
option:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- Name of the option, required for value changes or option remove
value:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- If I(option) is not presented for the I(interface) and I(state) is C(present) option will be added.
If I(option) already exists and is not C(pre-up), C(up), C(post-up) or C(down), it's value will be updated.
C(pre-up), C(up), C(post-up) and C(down) options can't be updated, only adding new options, removing existing
ones or cleaning the whole option set are supported
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
default: false
2020-03-09 10:11:07 +01:00
state:
Enabling validation-modules for system modules (#1212) * fixed validation-modules for aix_devices.py * fixed validation-modules for aix_filesystem.py * fixed validation-modules for aix_inittab.py * fixed validation-modules for aix_lvg.py * fixed validation-modules for aix_lvol.py * fixed validation-modules for awall.py * fixed validation-modules for dconf.py * fixed validation-modules for gconftool2.py * fixed validation-modules for interfaces_file.py * fixed validation-modules for java_keystore.py * fixed validation-modules for kernel_blacklist.py * fixed validation-modules for plugins/modules/system/lbu.py * fixed validation-modules for plugins/modules/system/locale_gen.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/mksysb.py * fixed validation-modules for plugins/modules/system/modprobe.py * fixed validation-modules for plugins/modules/system/nosh.py * fixed validation-modules for plugins/modules/system/open_iscsi.py * fixed validation-modules for plugins/modules/system/openwrt_init.py * fixed validation-modules for plugins/modules/system/osx_defaults.py * fixed validation-modules for plugins/modules/system/pamd.py * fixed validation-modules for plugins/modules/system/pam_limits.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/puppet.py * fixed validation-modules for plugins/modules/system/python_requirements_info.py * fixed validation-modules for plugins/modules/system/runit.py the parameter "dist" is not used anywhere in the module * fixed validation-modules for plugins/modules/system/sefcontext.py * fixed validation-modules for plugins/modules/system/selogin.py * fixed validation-modules for plugins/modules/system/seport.py * fixed validation-modules for plugins/modules/system/solaris_zone.py * fixed validation-modules for plugins/modules/system/syspatch.py * fixed validation-modules for plugins/modules/system/vdo.py * fixed validation-modules for plugins/modules/system/xfconf.py * removed ignore almost all validate-modules lines in system * removed unnecessary validations, per shippable test * kernel_blacklist: keeping blacklist_file as str instead of path * mksysb: keeping storage_path as str instead of path * pam_limits: keeping dest as str instead of path * rollback on adding doc for puppet.py legacy param * rolledback param seuser required in selogin module * rolledback changes in runit * rolledback changes in osx_defaults * rolledback changes in aix_defaults
2020-11-04 09:02:50 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- If set to C(absent) the option or section will be removed if present instead of created.
default: "present"
choices: [ "present", "absent" ]
notes:
- If option is defined multiple times last one will be updated but all will be deleted in case of an absent state
requirements: []
author: "Roman Belyakovsky (@hryamzik)"
'''
RETURN = '''
dest:
description: destination file/path
returned: success
type: str
sample: "/etc/network/interfaces"
ifaces:
description: interfaces dictionary
returned: success
type: complex
contains:
ifaces:
description: interface dictionary
returned: success
type: dict
contains:
eth0:
description: Name of the interface
returned: success
type: dict
contains:
address_family:
description: interface address family
returned: success
type: str
sample: "inet"
method:
description: interface method
returned: success
type: str
sample: "manual"
mtu:
description: other options, all values returned as strings
returned: success
type: str
sample: "1500"
pre-up:
description: list of C(pre-up) scripts
returned: success
type: list
sample:
- "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
- "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
up:
description: list of C(up) scripts
returned: success
type: list
sample:
- "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
- "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
post-up:
description: list of C(post-up) scripts
returned: success
type: list
sample:
- "route add -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
- "route add -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
down:
description: list of C(down) scripts
returned: success
type: list
sample:
- "route del -net 10.10.10.0/24 gw 10.10.10.1 dev eth1"
- "route del -net 10.10.11.0/24 gw 10.10.11.1 dev eth2"
...
'''
EXAMPLES = '''
- name: Set eth1 mtu configuration value to 8000
community.general.interfaces_file:
2020-03-09 10:11:07 +01:00
dest: /etc/network/interfaces.d/eth1.cfg
iface: eth1
option: mtu
value: 8000
backup: true
2020-03-09 10:11:07 +01:00
state: present
register: eth1_cfg
'''
import os
import re
import tempfile
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_bytes
2020-03-09 10:11:07 +01:00
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
def lineDict(line):
2020-03-09 10:11:07 +01:00
return {'line': line, 'line_type': 'unknown'}
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
def optionDict(line, iface, option, value, address_family):
2020-03-09 10:11:07 +01:00
return {'line': line, 'iface': iface, 'option': option, 'value': value, 'line_type': 'option', 'address_family': address_family}
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
def getValueFromLine(s):
spaceRe = re.compile(r'\s+')
m = list(spaceRe.finditer(s))[-1]
valueEnd = m.start()
option = s.split()[0]
optionStart = s.find(option)
optionLen = len(option)
return s[optionLen + optionStart:].strip()
2020-03-09 10:11:07 +01:00
def read_interfaces_file(module, filename):
with open(filename, 'r') as f:
return read_interfaces_lines(module, f)
2020-03-09 10:11:07 +01:00
def read_interfaces_lines(module, line_strings):
lines = []
ifaces = {}
currently_processing = None
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
i = 0
for line in line_strings:
i += 1
2020-03-09 10:11:07 +01:00
words = line.split()
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
if len(words) < 1:
lines.append(lineDict(line))
continue
if words[0][0] == "#":
lines.append(lineDict(line))
2020-03-09 10:11:07 +01:00
continue
if words[0] == "mapping":
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
# currmap = calloc(1, sizeof *currmap);
lines.append(lineDict(line))
2020-03-09 10:11:07 +01:00
currently_processing = "MAPPING"
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
elif words[0] == "source":
lines.append(lineDict(line))
currently_processing = "NONE"
elif words[0] == "source-dir":
lines.append(lineDict(line))
currently_processing = "NONE"
elif words[0] == "source-directory":
lines.append(lineDict(line))
2020-03-09 10:11:07 +01:00
currently_processing = "NONE"
elif words[0] == "iface":
currif = {
"pre-up": [],
"up": [],
"down": [],
"post-up": []
}
iface_name = words[1]
try:
currif['address_family'] = words[2]
except IndexError:
currif['address_family'] = None
address_family = currif['address_family']
try:
currif['method'] = words[3]
except IndexError:
currif['method'] = None
ifaces[iface_name] = currif
lines.append({'line': line, 'iface': iface_name, 'line_type': 'iface', 'params': currif, 'address_family': address_family})
currently_processing = "IFACE"
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
elif words[0] == "auto":
lines.append(lineDict(line))
currently_processing = "NONE"
elif words[0].startswith("allow-"):
lines.append(lineDict(line))
currently_processing = "NONE"
elif words[0] == "no-auto-down":
lines.append(lineDict(line))
currently_processing = "NONE"
elif words[0] == "no-scripts":
lines.append(lineDict(line))
currently_processing = "NONE"
2020-03-09 10:11:07 +01:00
else:
if currently_processing == "IFACE":
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
option_name = words[0]
value = getValueFromLine(line)
lines.append(optionDict(line, iface_name, option_name, value, address_family))
2020-03-09 10:11:07 +01:00
if option_name in ["pre-up", "up", "down", "post-up"]:
currif[option_name].append(value)
else:
currif[option_name] = value
elif currently_processing == "MAPPING":
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
lines.append(lineDict(line))
2020-03-09 10:11:07 +01:00
elif currently_processing == "NONE":
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
lines.append(lineDict(line))
2020-03-09 10:11:07 +01:00
else:
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
module.fail_json(msg="misplaced option %s in line %d" % (line, i))
return None, None
2020-03-09 10:11:07 +01:00
return lines, ifaces
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
def get_interface_options(iface_lines):
return [i for i in iface_lines if i['line_type'] == 'option']
def get_target_options(iface_options, option):
return [i for i in iface_options if i['option'] == option]
def update_existing_option_line(target_option, value):
old_line = target_option['line']
old_value = target_option['value']
prefix_start = old_line.find(target_option["option"])
optionLen = len(target_option["option"])
old_value_position = re.search(r"\s+".join(map(re.escape, old_value.split())), old_line[prefix_start + optionLen:])
start = old_value_position.start() + prefix_start + optionLen
end = old_value_position.end() + prefix_start + optionLen
line = old_line[:start] + value + old_line[end:]
return line
def set_interface_option(module, lines, iface, option, raw_value, state, address_family=None):
2020-03-09 10:11:07 +01:00
value = str(raw_value)
changed = False
iface_lines = [item for item in lines if "iface" in item and item["iface"] == iface]
if address_family is not None:
iface_lines = [item for item in iface_lines
if "address_family" in item and item["address_family"] == address_family]
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
if len(iface_lines) < 1:
2020-03-09 10:11:07 +01:00
# interface not found
module.fail_json(msg="Error: interface %s not found" % iface)
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
return changed, None
2020-03-09 10:11:07 +01:00
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
iface_options = get_interface_options(iface_lines)
target_options = get_target_options(iface_options, option)
2020-03-09 10:11:07 +01:00
if state == "present":
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
if len(target_options) < 1:
changed = True
2020-03-09 10:11:07 +01:00
# add new option
last_line_dict = iface_lines[-1]
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
changed, lines = addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options, address_family)
else:
if option in ["pre-up", "up", "down", "post-up"]:
if len(list(filter(lambda i: i['value'] == value, target_options))) < 1:
changed, lines = addOptionAfterLine(option, value, iface, lines, target_options[-1], iface_options, address_family)
else:
# if more than one option found edit the last one
if target_options[-1]['value'] != value:
changed = True
target_option = target_options[-1]
line = update_existing_option_line(target_option, value)
address_family = target_option['address_family']
index = len(lines) - lines[::-1].index(target_option) - 1
lines[index] = optionDict(line, iface, option, value, address_family)
elif state == "absent":
if len(target_options) >= 1:
2020-03-09 10:11:07 +01:00
if option in ["pre-up", "up", "down", "post-up"] and value is not None and value != "None":
for target_option in [ito for ito in target_options if ito['value'] == value]:
2020-03-09 10:11:07 +01:00
changed = True
lines = [ln for ln in lines if ln != target_option]
2020-03-09 10:11:07 +01:00
else:
changed = True
for target_option in target_options:
lines = [ln for ln in lines if ln != target_option]
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
else:
module.fail_json(msg="Error: unsupported state %s, has to be either present or absent" % state)
2020-03-09 10:11:07 +01:00
return changed, lines
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
def addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options, address_family):
2020-03-09 10:11:07 +01:00
# Changing method of interface is not an addition
if option == 'method':
changed = False
for ln in lines:
if ln.get('line_type', '') == 'iface' and ln.get('iface', '') == iface and value != ln.get('params', {}).get('method', ''):
changed = True
ln['line'] = re.sub(ln.get('params', {}).get('method', '') + '$', value, ln.get('line'))
ln['params']['method'] = value
return changed, lines
last_line = last_line_dict['line']
prefix_start = last_line.find(last_line.split()[0])
suffix_start = last_line.rfind(last_line.split()[-1]) + len(last_line.split()[-1])
prefix = last_line[:prefix_start]
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
if len(iface_options) < 1:
2020-03-09 10:11:07 +01:00
# interface has no options, ident
prefix += " "
line = prefix + "%s %s" % (option, value) + last_line[suffix_start:]
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
option_dict = optionDict(line, iface, option, value, address_family)
2020-03-09 10:11:07 +01:00
index = len(lines) - lines[::-1].index(last_line_dict)
lines.insert(index, option_dict)
return True, lines
def write_changes(module, lines, dest):
[PR #6131/abcba9db backport][stable-6] Interfaces file spaces fix (#6169) Interfaces file spaces fix (#6131) * interfaces_file: added test case for #6120 * interfaces_file: reverted code to #fafabed * interfaces_file: added changelog fragment * interfaces_file: added missing licenses * interfaces_file: improved test coverage * interfaces_file: fixed retrieving option values * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/modules/interfaces_file/test_interfaces_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * interfaces_file: spacing fix --------- Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit abcba9dbbec79374dc0cdb9978f0622e74a60e48) Co-authored-by: Roman Belyakovsky <roman.beliakovskii@smartly.io>
2023-03-09 20:31:08 +01:00
2020-03-09 10:11:07 +01:00
tmpfd, tmpfile = tempfile.mkstemp()
with os.fdopen(tmpfd, 'wb') as f:
f.write(to_bytes(''.join(lines), errors='surrogate_or_strict'))
2020-03-09 10:11:07 +01:00
module.atomic_move(tmpfile, os.path.realpath(dest))
def main():
module = AnsibleModule(
argument_spec=dict(
dest=dict(type='path', default='/etc/network/interfaces'),
iface=dict(type='str'),
address_family=dict(type='str'),
option=dict(type='str'),
value=dict(type='str'),
backup=dict(type='bool', default=False),
state=dict(type='str', default='present', choices=['absent', 'present']),
),
add_file_common_args=True,
supports_check_mode=True,
required_by=dict(
option=('iface',),
),
)
dest = module.params['dest']
iface = module.params['iface']
address_family = module.params['address_family']
option = module.params['option']
value = module.params['value']
backup = module.params['backup']
state = module.params['state']
if option is not None and state == "present" and value is None:
module.fail_json(msg="Value must be set if option is defined and state is 'present'")
lines, ifaces = read_interfaces_file(module, dest)
changed = False
if option is not None:
changed, lines = set_interface_option(module, lines, iface, option, value, state, address_family)
2020-03-09 10:11:07 +01:00
if changed:
Tidy up all pylint:blacklisted-name ignore lines (#1819) * fixed validation-modules for plugins/callback/hipchat.py * fixed validation-modules for plugins/connection/lxc.py * fixed validation-modules for plugins/modules/cloud/lxc/lxc_container.py * fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py * fixed validation-modules for plugins/modules/system/alternatives.py * fixed validation-modules for plugins/modules/system/beadm.py * fixed validation-modules for plugins/modules/system/cronvar.py * fixed validation-modules for plugins/modules/system/dconf.py * fixed validation-modules for plugins/modules/system/interfaces_file.py * fixed validation-modules for plugins/modules/system/java_cert.py * fixed validation-modules for plugins/modules/system/lvg.py * fixed validation-modules for plugins/modules/system/lvol.py * fixed validation-modules for plugins/modules/system/parted.py * fixed validation-modules for plugins/modules/system/timezone.py * fixed validation-modules for plugins/modules/web_infrastructure/rundeck_acl_policy.py * Tidy up all pylint:blacklisted-name sanity checks ignore lines * Missed one in statusio_maintenace.py * fixed validation-modules for plugins/modules/system/filesystem.py * Missed one in gconftool2.py * Missed one in alternatives.py * Using dummies now * fixed indentation * Made all the changes about replacing _ with dummy * Rollback bug fixed * Rollback bug fixed, part II * added changelog fragment * Improved changelog frag message per PR
2021-02-16 08:03:51 +01:00
dummy, ifaces = read_interfaces_lines(module, [d['line'] for d in lines if 'line' in d])
2020-03-09 10:11:07 +01:00
if changed and not module.check_mode:
if backup:
module.backup_local(dest)
write_changes(module, [d['line'] for d in lines if 'line' in d], dest)
module.exit_json(dest=dest, changed=changed, ifaces=ifaces)
if __name__ == '__main__':
main()