-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from woelfle/manage-dhcp-general
add module to manage general Kea DHCP settings
- Loading branch information
Showing
9 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.api import \ | ||
Session | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import GeneralModule | ||
|
||
|
||
class General(GeneralModule): | ||
CMDS = { | ||
'set': 'set', | ||
'search': 'get' | ||
} | ||
API_KEY_PATH = 'dhcpv4.general' | ||
API_KEY_PATH_REQ = API_KEY_PATH | ||
API_MOD = 'kea' | ||
API_CONT = 'dhcpv4' | ||
API_CONT_REL = 'service' | ||
FIELDS_CHANGE = [ | ||
'enabled', 'interfaces', 'socket_type', 'fw_rules', 'lifetime' | ||
] | ||
FIELDS_ALL = FIELDS_CHANGE | ||
FIELDS_TRANSLATE = { | ||
'lifetime': 'valid_lifetime', | ||
'fw_rules': 'fwrules', | ||
'socket_type': 'dhcp_socket_type', | ||
} | ||
FIELDS_TYPING = { | ||
'bool': ['enabled', 'fw_rules'], | ||
'int': ['lifetime'], | ||
'list': ['interfaces'], | ||
'select': ['socket_type'], | ||
} | ||
INT_VALIDATIONS = { | ||
'lifetime': {'min': 0}, | ||
} | ||
|
||
def __init__(self, module: AnsibleModule, result: dict, session: Session = None): | ||
GeneralModule.__init__(self=self, m=module, r=result, s=session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (C) 2024, AnsibleGuy <[email protected]> | ||
# GNU General Public License v3.0+ (see https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# see: https://docs.opnsense.org/development/api/plugins/nginx.html | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.handler import \ | ||
module_dependency_error, MODULE_EXCEPTIONS | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.wrapper import module_wrapper | ||
|
||
try: | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.defaults.main import \ | ||
EN_ONLY_MOD_ARG, OPN_MOD_ARGS, RELOAD_MOD_ARG | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.dhcp_general import General | ||
|
||
|
||
except MODULE_EXCEPTIONS: | ||
module_dependency_error() | ||
|
||
# DOCUMENTATION = 'https://opnsense.ansibleguy.net/modules/dhcp.html' | ||
# EXAMPLES = 'https://opnsense.ansibleguy.net/modules/dhcp.html' | ||
|
||
|
||
def run_module(): | ||
module_args = dict( | ||
interfaces=dict( | ||
type='list', elements='str', required=False, default=[], aliases=['ints'], | ||
description='Comma separated list of network interfaces to listen on for DHCP requests' | ||
), | ||
socket_type=dict( | ||
type='str', required=False, default='raw', choices=['raw', 'udp'], aliases=['dhcp_socket_type'], | ||
description='Socket type used for DHCP communication', | ||
), | ||
fw_rules=dict( | ||
type='bool', required=False, default=True, aliases=['fwrules', 'rules'], | ||
description='Automatically add a basic set of firewall rules to allow dhcp traffic, ' | ||
'more fine grained controls can be offered manually when disabling this option', | ||
), | ||
lifetime=dict( | ||
type='int', required=False, default=4000, aliases=['valid_lifetime'], | ||
description='Defines how long the addresses (leases) given out by the server are valid (in seconds)', | ||
), | ||
**EN_ONLY_MOD_ARG, | ||
**RELOAD_MOD_ARG, | ||
**OPN_MOD_ARGS, | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=module_args, | ||
supports_check_mode=True, | ||
) | ||
|
||
result = dict( | ||
changed=False, | ||
diff={ | ||
'before': {}, | ||
'after': {}, | ||
} | ||
) | ||
|
||
module_wrapper(General(module=module, result=result)) | ||
module.exit_json(**result) | ||
|
||
|
||
def main(): | ||
run_module() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
|
||
- name: Testing DHCP Setting | ||
hosts: localhost | ||
gather_facts: no | ||
module_defaults: | ||
group/ansibleguy.opnsense.all: | ||
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}" | ||
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}" | ||
ssl_verify: false | ||
|
||
ansibleguy.opnsense.list: | ||
target: 'dhcp_general' | ||
|
||
tasks: | ||
- name: Listing | ||
ansibleguy.opnsense.list: | ||
register: opn_pre1 | ||
failed_when: > | ||
opn_pre1.failed or | ||
'data' not in opn_pre1 | ||
- name: Configuring - failing because of invalid lifetime | ||
ansibleguy.opnsense.dhcp_general: | ||
lifetime: -1 | ||
register: opn_fail1 | ||
failed_when: not opn_fail1.failed | ||
|
||
- name: Configuring | ||
ansibleguy.opnsense.dhcp_general: | ||
enabled: true | ||
interfaces: ['opt1'] | ||
register: opn1 | ||
failed_when: > | ||
opn1.failed or | ||
not opn1.changed | ||
- name: Changing | ||
ansibleguy.opnsense.dhcp_general: | ||
enabled: true | ||
interfaces: ['opt1', 'lan'] | ||
fw_rules: false | ||
lifetime: 5000 | ||
register: opn2 | ||
failed_when: > | ||
opn2.failed or | ||
not opn2.changed | ||
when: not ansible_check_mode | ||
|
||
- name: Nothing changed | ||
ansibleguy.opnsense.dhcp_general: | ||
enabled: true | ||
interfaces: ['opt1', 'lan'] | ||
fw_rules: false | ||
lifetime: 5000 | ||
register: opn3 | ||
failed_when: > | ||
opn3.failed or | ||
opn3.changed | ||
when: not ansible_check_mode | ||
|
||
- name: Cleanup | ||
ansibleguy.opnsense.dhcp_general: | ||
enabled: false |