Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/module_utils/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,17 @@ def _copy_and_update_target(self):
if changed:
if self.params['enable']:
self.setup_interface_cmds += "interface_bring_down('{0}', false);\n".format(self.target_elt.tag)

# possibly kill remaining dhclient process
if 'ipaddr' in before and before['ipaddr'] == 'dhcp':
self.setup_interface_cmds += "kill_dhclient_process(get_real_interface({0}));\n".format(self.target_elt.tag)

self.setup_interface_cmds += "interface_configure('{0}', true);\n".format(self.target_elt.tag)
else:
self.setup_interface_cmds += "interface_bring_down('{0}', true);\n".format(self.target_elt.tag)
# possibly kill remaining dhclient process
if 'ipaddr' in before and before['ipaddr'] == 'dhcp':
self.setup_interface_cmds += "kill_dhclient_process(get_real_interface({0}));\n".format(self.target_elt.tag)

return (before, changed)

Expand Down
2 changes: 1 addition & 1 deletion plugins/module_utils/nat_outbound.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
before=dict(required=False, type='str'),
)

NAT_OUTBOUD_REQUIRED_IF = [
NAT_OUTBOUND_REQUIRED_IF = [
["state", "present", ["interface", "source", "destination"]]
]

Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/pfsense_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@
INTERFACE_REQUIRED_IF,
INTERFACE_MUTUALLY_EXCLUSIVE,
)
from ansible_collections.pfsensible.core.plugins.module_utils.nat_outbound import PFSenseNatOutboundModule, NAT_OUTBOUND_ARGUMENT_SPEC, NAT_OUTBOUD_REQUIRED_IF
from ansible_collections.pfsensible.core.plugins.module_utils.nat_outbound import PFSenseNatOutboundModule, NAT_OUTBOUND_ARGUMENT_SPEC, NAT_OUTBOUND_REQUIRED_IF
from ansible_collections.pfsensible.core.plugins.module_utils.nat_port_forward import (
PFSenseNatPortForwardModule,
NAT_PORT_FORWARD_ARGUMENT_SPEC,
Expand Down Expand Up @@ -1058,7 +1058,7 @@ def main():
type='list', elements='dict',
options=INTERFACE_ARGUMENT_SPEC, required_if=INTERFACE_REQUIRED_IF, mutually_exclusive=INTERFACE_MUTUALLY_EXCLUSIVE),
aggregated_rules=dict(type='list', elements='dict', options=RULE_ARGUMENT_SPEC, required_if=RULE_REQUIRED_IF),
aggregated_nat_outbounds=dict(type='list', elements='dict', options=NAT_OUTBOUND_ARGUMENT_SPEC, required_if=NAT_OUTBOUD_REQUIRED_IF),
aggregated_nat_outbounds=dict(type='list', elements='dict', options=NAT_OUTBOUND_ARGUMENT_SPEC, required_if=NAT_OUTBOUND_REQUIRED_IF),
aggregated_nat_port_forwards=dict(type='list', elements='dict', options=NAT_PORT_FORWARD_ARGUMENT_SPEC, required_if=NAT_PORT_FORWARD_REQUIRED_IF),
aggregated_rule_separators=dict(
type='list', elements='dict',
Expand Down
146 changes: 146 additions & 0 deletions plugins/modules/pfsense_default_gateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2018, Frederic Bor <frederic.bor@wanadoo.fr>
# Copyright: (c) 2021, Jan Wenzel <jan.wenzel@gonicus.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}

DOCUMENTATION = """
---
module: pfsense_default_gateway
version_added: "0.4.2"
author: Jan Wenzel (@coffeelover)
short_description: Manage pfSense default gateways
description:
- Manage pfSense default gateways for IPv4/IPv6
notes:
options:
defaultgw4:
description: Default Gateway (IPv4) (name of existing gateway, auto or none)
required: false
type: str
defaultgw6:
description: Default Gateway (IPv6) (name of existing gateway, auto or none)
required: false
type: str
"""

EXAMPLES = """
pfsensible.core.pfsense_default_gateway:
defaultgw4: "LANGW"
"""

RETURN = """
"""

from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.pfsensible.core.plugins.module_utils.module_base import PFSenseModuleBase


DEFAULT_GATEWAY_ARGUMENT_SPEC = dict(
defaultgw4=dict(required=False, type='str'),
defaultgw6=dict(required=False, type='str'),
)

# map field names between ansible and pfsense
params_map = {}

# fields with inverted logic
inverted_list = []

# fields that are not written to pfsense
skip_list = ['state']

class PFSenseDefaultGatewayModule(PFSenseModuleBase):
""" module managing pfsense default gateway settings """

@staticmethod
def get_argument_spec():
""" return argument spec """
return DEFAULT_GATEWAY_ARGUMENT_SPEC

##############################
# init
#
def __init__(self, module, pfsense=None):
super(PFSenseDefaultGatewayModule, self).__init__(module, pfsense)
self.name = "default_gateway"
self.root_elt = self.pfsense.get_element('gateways', create_node=True)
self.obj = dict()

##############################
# params processing
#
def _params_to_obj(self):
""" return a dict from module params """
params = self.params

obj = dict()
self.obj = obj

def _set_param(target, param):
if params.get(param) is not None:
if params[param].lower() == 'auto':
target[param] = ''
elif params[param].lower() == 'none':
target[param] = '-'
else:
target[param] = params[param]

for param in DEFAULT_GATEWAY_ARGUMENT_SPEC:
_set_param(obj, param)

return obj


def _validate_params(self):
""" do some extra checks on input parameters """
return

def run(self, params):
self.params = params
self.target_elt = self.root_elt
self._validate_params()
self.obj = self._params_to_obj()
self._add()

@staticmethod
def _get_obj_name():
""" return obj's name """
return "default_gateway"

def _log_fields(self, before=None):
""" generate pseudo-CLI command fields parameters to create an obj """
values = ''

if before is None:
for param in DEFAULT_GATEWAY_ARGUMENT_SPEC:
values += self.format_cli_field(self.obj, param)
else:
for param in DEFAULT_GATEWAY_ARGUMENT_SPEC:
values += self.format_updated_cli_field(self.obj, before, param, add_comma=(values), log_none=False)

return values


def main():
module = AnsibleModule(
argument_spec=DEFAULT_GATEWAY_ARGUMENT_SPEC,
supports_check_mode=True)

pfmodule = PFSenseDefaultGatewayModule(module)
pfmodule.run(module.params)
pfmodule.commit_changes()


if __name__ == '__main__':
main()
Loading