From 2c08a3c4c4358e314eda147bf7e153c8f339823d Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Fri, 22 Mar 2024 21:55:19 +0100 Subject: [PATCH 1/8] initial module --- plugins/modules/services_dhcpv4.py | 78 ++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 plugins/modules/services_dhcpv4.py diff --git a/plugins/modules/services_dhcpv4.py b/plugins/modules/services_dhcpv4.py new file mode 100644 index 00000000..08c12040 --- /dev/null +++ b/plugins/modules/services_dhcpv4.py @@ -0,0 +1,78 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2024, Lukas Grimm , Puzzle ITC +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +"""services_dhcpv4 module: Module to configure dhcpv4""" + +__metaclass__ = type + + + +# https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html +# fmt: off +DOCUMENTATION = r''' +--- +author: + - Lukas Grimm (@ombre8) +module: services_dhcpv4 +short_description: Configure DHCP server for specific Interface. +description: + - Module to configure general system settings +options: + interface: + description: "The Interface the DHCP server should be configured on" + type: str + required: true + enable: + description: Wheter the Server is enabled or not + type: bool + default: true + required: false + range_from: + description: Start of the IP Pool + type: str + required: false + range_to: + description: End of the IP Pool + type: str + required: false +''' + +EXAMPLES = r''' +--- +- name: Enable DHCP Server on LAN interface + puzzle.opnsense.services_dhcpv4: + interface: LAN + +- name: Enable DHCP Server on guestwifi interface + puzzle.opnsense.services_dhcpv4: + interface: guestwifi + enable: true + range_from: 192.168.10.100 + range_to: 192.168.10.254 +''' + +RETURN = ''' # TODO +opnsense_configure_output: + description: A List of the executed OPNsense configure function along with their respective stdout, stderr and rc + returned: always + type: list + sample: + - function: system_syslog_start + params: + - 'true' + rc: 0 + stderr: '' + stderr_lines: [] + stdout: 'Configuring system logging...done.' + stdout_lines: + - 'Configuring system logging...done.' +''' +# fmt: on + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.puzzle.opnsense.plugins.module_utils.config_utils import ( + OPNsenseModuleConfig, +) From 76415ec54a50341be223f873da79538393a8bd1a Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Sat, 23 Mar 2024 14:56:26 +0100 Subject: [PATCH 2/8] added inital module --- molecule/services_dhcpv4/molecule.yml | 67 +++++++++++++++++++++++++ molecule/services_dhcpv4/verify.yml | 6 +++ plugins/module_utils/module_index.py | 60 ++++++++++++++++++++++ plugins/modules/services_dhcpv4.py | 72 +++++++++++++++++++++++++-- 4 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 molecule/services_dhcpv4/molecule.yml create mode 100644 molecule/services_dhcpv4/verify.yml diff --git a/molecule/services_dhcpv4/molecule.yml b/molecule/services_dhcpv4/molecule.yml new file mode 100644 index 00000000..8c715fc8 --- /dev/null +++ b/molecule/services_dhcpv4/molecule.yml @@ -0,0 +1,67 @@ +--- +scenario: + name: service_dhcpv4 + test_sequence: + # - dependency not relevant unless we have requirements + - destroy + - syntax + - create + - converge + - idempotence + - verify + - destroy + +driver: + name: vagrant + parallel: true + +platforms: + - name: "22.7" + hostname: false + box: puzzle/opnsense + box_version: "22.7" + memory: 1024 + cpus: 2 + instance_raw_config_args: + - 'vm.guest = :freebsd' + - 'ssh.sudo_command = "%c"' + - 'ssh.shell = "/bin/sh"' + - name: "23.1" + box: puzzle/opnsense + hostname: false + box_version: "23.1" + memory: 1024 + cpus: 2 + instance_raw_config_args: + - 'vm.guest = :freebsd' + - 'ssh.sudo_command = "%c"' + - 'ssh.shell = "/bin/sh"' + - name: "23.7" + box: puzzle/opnsense + hostname: false + box_version: "23.7" + memory: 1024 + cpus: 2 + instance_raw_config_args: + - 'vm.guest = :freebsd' + - 'ssh.sudo_command = "%c"' + - 'ssh.shell = "/bin/sh"' + - name: "24.1" + box: puzzle/opnsense + hostname: false + box_version: "24.1" + memory: 1024 + cpus: 2 + instance_raw_config_args: + - 'vm.guest = :freebsd' + - 'ssh.sudo_command = "%c"' + - 'ssh.shell = "/bin/sh"' + +provisioner: + name: ansible + env: + ANSIBLE_VERBOSITY: 3 +verifier: + name: ansible + options: + become: true diff --git a/molecule/services_dhcpv4/verify.yml b/molecule/services_dhcpv4/verify.yml new file mode 100644 index 00000000..b447dcf4 --- /dev/null +++ b/molecule/services_dhcpv4/verify.yml @@ -0,0 +1,6 @@ +--- +- name: Verify connectivity to server + hosts: all + tasks: + - name: Ping the server + ansible.builtin.ping: diff --git a/plugins/module_utils/module_index.py b/plugins/module_utils/module_index.py index e074e52e..b29079a1 100644 --- a/plugins/module_utils/module_index.py +++ b/plugins/module_utils/module_index.py @@ -76,6 +76,21 @@ "configure_params": ["true"], }, }, + }, + "services_dhcpv4": { + "enable": interface+"/enable", + "range_from": interface+"/range/from", + "range_to": interface+"/range/to", + # Add other mappings here. + "php_requirements": [ + "", + ], + "configure_functions": { + "reconfigure_dhcpd": { + "name": "reconfigure_dhcpd", + "configure_params": ["true"], + }, + }, } }, "OPNsense 23.1": { @@ -125,6 +140,21 @@ "configure_params": ["true"], }, }, + }, + "services_dhcpv4": { + "enable": interface+"/enable", + "range_from": interface+"/range/from", + "range_to": interface+"/range/to", + # Add other mappings here. + "php_requirements": [ + "", + ], + "configure_functions": { + "reconfigure_dhcpd": { + "name": "reconfigure_dhcpd", + "configure_params": ["true"], + }, + }, }, }, "OPNsense 23.7": { @@ -175,6 +205,21 @@ }, }, }, + "services_dhcpv4": { + "enable": interface+"/enable", + "range_from": interface+"/range/from", + "range_to": interface+"/range/to", + # Add other mappings here. + "php_requirements": [ + "", + ], + "configure_functions": { + "reconfigure_dhcpd": { + "name": "reconfigure_dhcpd", + "configure_params": ["true"], + }, + }, + }, }, "OPNsense 24.1": { "system_settings_general": { @@ -223,6 +268,21 @@ "configure_params": ["true"], }, }, + }, + "services_dhcpv4": { + "enable": interface+"/enable", + "range_from": interface+"/range/from", + "range_to": interface+"/range/to", + # Add other mappings here. + "php_requirements": [ + "", + ], + "configure_functions": { + "reconfigure_dhcpd": { + "name": "reconfigure_dhcpd", + "configure_params": ["true"], + }, + }, } }, } diff --git a/plugins/modules/services_dhcpv4.py b/plugins/modules/services_dhcpv4.py index 08c12040..84a91a7e 100644 --- a/plugins/modules/services_dhcpv4.py +++ b/plugins/modules/services_dhcpv4.py @@ -54,21 +54,20 @@ range_to: 192.168.10.254 ''' -RETURN = ''' # TODO +RETURN = ''' opnsense_configure_output: description: A List of the executed OPNsense configure function along with their respective stdout, stderr and rc returned: always type: list sample: - - function: system_syslog_start + - function: reconfigure_dhcpd params: - 'true' rc: 0 stderr: '' stderr_lines: [] - stdout: 'Configuring system logging...done.' - stdout_lines: - - 'Configuring system logging...done.' + stdout: '' + stdout_lines: [] ''' # fmt: on @@ -76,3 +75,66 @@ from ansible_collections.puzzle.opnsense.plugins.module_utils.config_utils import ( OPNsenseModuleConfig, ) + +def main(): + """ + Main function of the services_dhcpv4 module + """ + + module_args = { + "interface": {"type": "string", "required": True }, + "enable": {"type": "bool", "default": True, "required": False}, + "range_from": {"type": "string", "required": False}, + "range_to": {"type": "string", "required": False}, + } + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True, + ) + + result = { + "changed": False, + "invocation": module.params, + "diff": None, + } + + interface = module.params.get("interface") + enable = module.params.get("enable") + range_from = module.params.get("range_from") + range_to = module.params.get("range_to") + + with OPNsenseModuleConfig( + module_name="services_dhcpv4", + config_context_names=["services_dhcpv4"], + check_mode=module.check_mode, + ) as config: + if enable != config.get("enable").text: + config.set(value=str(enable), setting="enable") + + if range_from != config.get("range_from").text: + config.set(value=str(range_from), setting="range_from") + + if range_to != config.get("range_to").text: + config.set(value=str(range_to), setting="range_to") + + if config.changed: + result["diff"] = config.diff + result["changed"] = True + + if config.changed and not module.check_mode: + config.save() + result["opnsense_configure_output"] = config.apply_settings() + for cmd_result in result["opnsense_configure_output"]: + if cmd_result["rc"] != 0: + module.fail_json( + msg="Apply of the OPNsense settings failed", + details=cmd_result, + ) + + # Return results + module.exit_json(**result) + + +if __name__ == "__main__": + main() From f3aef659c16a24c94870089fa2ac496888331c79 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Sat, 23 Mar 2024 14:57:25 +0100 Subject: [PATCH 3/8] black --- plugins/module_utils/module_index.py | 32 +++++++++--------- plugins/modules/services_dhcpv4.py | 50 ++++++++++++++-------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/plugins/module_utils/module_index.py b/plugins/module_utils/module_index.py index b29079a1..c480be23 100644 --- a/plugins/module_utils/module_index.py +++ b/plugins/module_utils/module_index.py @@ -78,9 +78,9 @@ }, }, "services_dhcpv4": { - "enable": interface+"/enable", - "range_from": interface+"/range/from", - "range_to": interface+"/range/to", + "enable": interface + "/enable", + "range_from": interface + "/range/from", + "range_to": interface + "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -91,7 +91,7 @@ "configure_params": ["true"], }, }, - } + }, }, "OPNsense 23.1": { "system_settings_general": { @@ -140,11 +140,11 @@ "configure_params": ["true"], }, }, - }, + }, "services_dhcpv4": { - "enable": interface+"/enable", - "range_from": interface+"/range/from", - "range_to": interface+"/range/to", + "enable": interface + "/enable", + "range_from": interface + "/range/from", + "range_to": interface + "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -154,7 +154,7 @@ "name": "reconfigure_dhcpd", "configure_params": ["true"], }, - }, + }, }, }, "OPNsense 23.7": { @@ -206,9 +206,9 @@ }, }, "services_dhcpv4": { - "enable": interface+"/enable", - "range_from": interface+"/range/from", - "range_to": interface+"/range/to", + "enable": interface + "/enable", + "range_from": interface + "/range/from", + "range_to": interface + "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -270,9 +270,9 @@ }, }, "services_dhcpv4": { - "enable": interface+"/enable", - "range_from": interface+"/range/from", - "range_to": interface+"/range/to", + "enable": interface + "/enable", + "range_from": interface + "/range/from", + "range_to": interface + "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -283,6 +283,6 @@ "configure_params": ["true"], }, }, - } + }, }, } diff --git a/plugins/modules/services_dhcpv4.py b/plugins/modules/services_dhcpv4.py index 84a91a7e..538f406d 100644 --- a/plugins/modules/services_dhcpv4.py +++ b/plugins/modules/services_dhcpv4.py @@ -9,7 +9,6 @@ __metaclass__ = type - # https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html # fmt: off DOCUMENTATION = r''' @@ -76,21 +75,22 @@ OPNsenseModuleConfig, ) + def main(): """ Main function of the services_dhcpv4 module """ module_args = { - "interface": {"type": "string", "required": True }, + "interface": {"type": "string", "required": True}, "enable": {"type": "bool", "default": True, "required": False}, "range_from": {"type": "string", "required": False}, "range_to": {"type": "string", "required": False}, } module = AnsibleModule( - argument_spec=module_args, - supports_check_mode=True, + argument_spec=module_args, + supports_check_mode=True, ) result = { @@ -105,27 +105,27 @@ def main(): range_to = module.params.get("range_to") with OPNsenseModuleConfig( - module_name="services_dhcpv4", - config_context_names=["services_dhcpv4"], - check_mode=module.check_mode, - ) as config: - if enable != config.get("enable").text: - config.set(value=str(enable), setting="enable") - - if range_from != config.get("range_from").text: - config.set(value=str(range_from), setting="range_from") - - if range_to != config.get("range_to").text: - config.set(value=str(range_to), setting="range_to") - - if config.changed: - result["diff"] = config.diff - result["changed"] = True - - if config.changed and not module.check_mode: - config.save() - result["opnsense_configure_output"] = config.apply_settings() - for cmd_result in result["opnsense_configure_output"]: + module_name="services_dhcpv4", + config_context_names=["services_dhcpv4"], + check_mode=module.check_mode, + ) as config: + if enable != config.get("enable").text: + config.set(value=str(enable), setting="enable") + + if range_from != config.get("range_from").text: + config.set(value=str(range_from), setting="range_from") + + if range_to != config.get("range_to").text: + config.set(value=str(range_to), setting="range_to") + + if config.changed: + result["diff"] = config.diff + result["changed"] = True + + if config.changed and not module.check_mode: + config.save() + result["opnsense_configure_output"] = config.apply_settings() + for cmd_result in result["opnsense_configure_output"]: if cmd_result["rc"] != 0: module.fail_json( msg="Apply of the OPNsense settings failed", From ca2eacb500b3175953c15b7c46a44ed1563783f9 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Sat, 23 Mar 2024 15:03:49 +0100 Subject: [PATCH 4/8] added first test --- molecule/services_dhcpv4/converge.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 molecule/services_dhcpv4/converge.yml diff --git a/molecule/services_dhcpv4/converge.yml b/molecule/services_dhcpv4/converge.yml new file mode 100644 index 00000000..344cd466 --- /dev/null +++ b/molecule/services_dhcpv4/converge.yml @@ -0,0 +1,11 @@ +--- +- name: converge + hosts: all + become: true + tasks: + - name: Converge - Enable DHCP Server on LAN interface + puzzle.opnsense.services_dhcpv4: + interface: lan + enable: true + range_from: 10.2.0.100 + range_to: 10.2.0.200 From 23f8f7aaa769ccdb24e403395d6cff8135e76553 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Sat, 23 Mar 2024 15:11:48 +0100 Subject: [PATCH 5/8] fix typo --- molecule/services_dhcpv4/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/services_dhcpv4/molecule.yml b/molecule/services_dhcpv4/molecule.yml index 8c715fc8..fbc4ee82 100644 --- a/molecule/services_dhcpv4/molecule.yml +++ b/molecule/services_dhcpv4/molecule.yml @@ -1,6 +1,6 @@ --- scenario: - name: service_dhcpv4 + name: services_dhcpv4 test_sequence: # - dependency not relevant unless we have requirements - destroy From a73e5e56a7aa024f4d3b19f5329763a5d4111553 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Wed, 17 Apr 2024 16:16:58 +0200 Subject: [PATCH 6/8] try to do things differently --- plugins/module_utils/services_dhcpv4.py | 15 +++++++++++++++ plugins/modules/services_dhcpv4.py | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 plugins/module_utils/services_dhcpv4.py diff --git a/plugins/module_utils/services_dhcpv4.py b/plugins/module_utils/services_dhcpv4.py new file mode 100644 index 00000000..dc282385 --- /dev/null +++ b/plugins/module_utils/services_dhcpv4.py @@ -0,0 +1,15 @@ +from ansible_collections.puzzle.opnsense.plugins.module_utils import ( + xml_utils, + opnsense_utils, +) +from ansible_collections.puzzle.opnsense.plugins.module_utils.config_utils import ( + OPNsenseModuleConfig, +) + +class DHCPv4Set(OPNsenseModuleConfig): + def __init__(self, interface, path: str = "/conf/config.xml"): + super().__init__( + module_name="services_dhcpv4", + config_context_names=["services_dhcpv4", "enable", "range_from", "range_to"], + path=interface + path, + ) diff --git a/plugins/modules/services_dhcpv4.py b/plugins/modules/services_dhcpv4.py index 538f406d..50d3479a 100644 --- a/plugins/modules/services_dhcpv4.py +++ b/plugins/modules/services_dhcpv4.py @@ -71,8 +71,8 @@ # fmt: on from ansible.module_utils.basic import AnsibleModule -from ansible_collections.puzzle.opnsense.plugins.module_utils.config_utils import ( - OPNsenseModuleConfig, +from ansible_collections.puzzle.opnsense.plugins.module_utils.service_dhcpv4 import ( + DHCPv4Set, ) @@ -104,9 +104,10 @@ def main(): range_from = module.params.get("range_from") range_to = module.params.get("range_to") - with OPNsenseModuleConfig( + with DHCPv4Set( module_name="services_dhcpv4", config_context_names=["services_dhcpv4"], + interface=interface, check_mode=module.check_mode, ) as config: if enable != config.get("enable").text: From 4166e6451e4e0de3c618356edc34a6151a31fd19 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Wed, 17 Apr 2024 21:39:35 +0200 Subject: [PATCH 7/8] remove old stuff --- plugins/module_utils/module_index.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/module_utils/module_index.py b/plugins/module_utils/module_index.py index 311959c9..d24c274d 100644 --- a/plugins/module_utils/module_index.py +++ b/plugins/module_utils/module_index.py @@ -136,9 +136,9 @@ }, }, "services_dhcpv4": { - "enable": interface + "/enable", - "range_from": interface + "/range/from", - "range_to": interface + "/range/to", + "enable": "/enable", + "range_from": "/range/from", + "range_to": "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -258,9 +258,9 @@ }, }, "services_dhcpv4": { - "enable": interface + "/enable", - "range_from": interface + "/range/from", - "range_to": interface + "/range/to", + "enable": "/enable", + "range_from": "/range/from", + "range_to": "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -380,9 +380,9 @@ }, }, "services_dhcpv4": { - "enable": interface + "/enable", - "range_from": interface + "/range/from", - "range_to": interface + "/range/to", + "enable": "/enable", + "range_from": "/range/from", + "range_to": "/range/to", # Add other mappings here. "php_requirements": [ "", @@ -503,9 +503,9 @@ }, }, "services_dhcpv4": { - "enable": interface + "/enable", - "range_from": interface + "/range/from", - "range_to": interface + "/range/to", + "enable": "/enable", + "range_from": "/range/from", + "range_to": "/range/to", # Add other mappings here. "php_requirements": [ "", From 73e367fd7c20a6fb4076582f8b73323683111518 Mon Sep 17 00:00:00 2001 From: Lukas Grimm Date: Thu, 20 Jun 2024 15:08:39 +0200 Subject: [PATCH 8/8] rename --- .../{services_dhcpv4.py => services_dhcpv4_utils.py} | 0 plugins/modules/services_dhcpv4.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename plugins/module_utils/{services_dhcpv4.py => services_dhcpv4_utils.py} (100%) diff --git a/plugins/module_utils/services_dhcpv4.py b/plugins/module_utils/services_dhcpv4_utils.py similarity index 100% rename from plugins/module_utils/services_dhcpv4.py rename to plugins/module_utils/services_dhcpv4_utils.py diff --git a/plugins/modules/services_dhcpv4.py b/plugins/modules/services_dhcpv4.py index 50d3479a..da2d11c6 100644 --- a/plugins/modules/services_dhcpv4.py +++ b/plugins/modules/services_dhcpv4.py @@ -71,7 +71,7 @@ # fmt: on from ansible.module_utils.basic import AnsibleModule -from ansible_collections.puzzle.opnsense.plugins.module_utils.service_dhcpv4 import ( +from ansible_collections.puzzle.opnsense.plugins.module_utils.service_dhcpv4_utils import ( DHCPv4Set, )