2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-12-18 15:53:46 +00:00
|
|
|
# Copyright: (c) 2013, Adam Miller <maxamillion@fedoraproject.org>
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# 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
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
2017-05-18 15:28:41 +00:00
|
|
|
'supported_by': 'community'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2018-12-18 15:53:46 +00:00
|
|
|
DOCUMENTATION = r'''
|
2014-09-26 01:01:01 +00:00
|
|
|
---
|
|
|
|
module: firewalld
|
|
|
|
short_description: Manage arbitrary ports/services with firewalld
|
|
|
|
description:
|
2014-11-20 20:48:41 +00:00
|
|
|
- This module allows for addition or deletion of services and ports either tcp or udp in either running or permanent firewalld rules.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.4"
|
|
|
|
options:
|
|
|
|
service:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Name of a service to add/remove to/from firewalld.
|
|
|
|
- The service must be listed in output of firewall-cmd --get-services.
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
port:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Name of a port or port range to add/remove to/from firewalld.
|
|
|
|
- Must be in the form PORT/PROTOCOL or PORT-PORT/PROTOCOL for port ranges.
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
rich_rule:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Rich rule to add/remove to/from firewalld.
|
|
|
|
type: str
|
2015-06-29 12:18:09 +00:00
|
|
|
source:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- The source/network you would like to add/remove to/from firewalld.
|
|
|
|
type: str
|
2015-06-29 12:18:09 +00:00
|
|
|
version_added: "2.0"
|
2016-01-10 22:18:09 +00:00
|
|
|
interface:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- The interface you would like to add/remove to/from a zone in firewalld.
|
|
|
|
type: str
|
2016-01-16 22:02:58 +00:00
|
|
|
version_added: "2.1"
|
2018-12-12 20:01:35 +00:00
|
|
|
icmp_block:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- The icmp block you would like to add/remove to/from a zone in firewalld.
|
|
|
|
type: str
|
2018-12-12 20:01:35 +00:00
|
|
|
version_added: "2.8"
|
|
|
|
icmp_block_inversion:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Enable/Disable inversion of icmp blocks for a zone in firewalld.
|
|
|
|
type: str
|
2018-12-12 20:01:35 +00:00
|
|
|
version_added: "2.8"
|
2014-09-26 01:01:01 +00:00
|
|
|
zone:
|
|
|
|
description:
|
2019-02-19 14:42:14 +00:00
|
|
|
- The firewalld zone to add/remove to/from.
|
|
|
|
- Note that the default zone can be configured per system but C(public) is default from upstream.
|
2018-12-18 15:53:46 +00:00
|
|
|
- Available choices can be extended based on per-system configs, listed here are "out of the box" defaults).
|
|
|
|
- Possible values include C(block), C(dmz), C(drop), C(external), C(home), C(internal), C(public), C(trusted), C(work) ]
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
permanent:
|
|
|
|
description:
|
2019-01-18 02:24:47 +00:00
|
|
|
- Should this configuration be in the running firewalld configuration or persist across reboots.
|
|
|
|
- As of Ansible 2.3, permanent operations can operate on firewalld configs when it is not running (requires firewalld >= 3.0.9).
|
|
|
|
- Note that if this is C(no), immediate is assumed C(yes).
|
2018-08-23 18:06:06 +00:00
|
|
|
type: bool
|
2014-09-27 00:50:10 +00:00
|
|
|
immediate:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Should this configuration be applied immediately, if set as permanent.
|
2018-03-15 21:15:24 +00:00
|
|
|
type: bool
|
2018-12-18 15:53:46 +00:00
|
|
|
default: no
|
2014-09-27 00:50:10 +00:00
|
|
|
version_added: "1.9"
|
2014-09-26 01:01:01 +00:00
|
|
|
state:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- Enable or disable a setting.
|
|
|
|
- 'For ports: Should this port accept(enabled) or reject(disabled) connections.'
|
|
|
|
- The states C(present) and C(absent) can only be used in zone level operations (i.e. when no other parameters but zone and state are set).
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
2018-12-18 15:53:46 +00:00
|
|
|
choices: [ absent, disabled, enabled, present ]
|
2014-09-26 01:01:01 +00:00
|
|
|
timeout:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- The amount of time the rule should be in effect for when non-permanent.
|
|
|
|
type: int
|
2014-09-26 01:01:01 +00:00
|
|
|
default: 0
|
2016-04-16 12:15:00 +00:00
|
|
|
masquerade:
|
|
|
|
description:
|
2018-12-18 15:53:46 +00:00
|
|
|
- The masquerade setting you would like to enable/disable to/from zones within firewalld.
|
|
|
|
type: str
|
2016-04-16 12:15:00 +00:00
|
|
|
version_added: "2.1"
|
2018-12-18 15:53:46 +00:00
|
|
|
offline:
|
|
|
|
description:
|
|
|
|
- Whether to run this module even when firewalld is offline.
|
|
|
|
type: bool
|
|
|
|
version_added: "2.3"
|
2014-09-26 01:01:01 +00:00
|
|
|
notes:
|
2015-05-27 18:54:26 +00:00
|
|
|
- Not tested on any Debian based system.
|
2018-05-14 13:38:10 +00:00
|
|
|
- Requires the python2 bindings of firewalld, which may not be installed by default.
|
|
|
|
- For distributions where the python2 firewalld bindings are unavailable (e.g Fedora 28 and later) you will have to set the
|
|
|
|
ansible_python_interpreter for these hosts to the python3 interpreter path and install the python3 bindings.
|
2017-12-12 08:34:41 +00:00
|
|
|
- Zone transactions (creating, deleting) can be performed by using only the zone and state parameters "present" or "absent".
|
|
|
|
Note that zone transactions must explicitly be permanent. This is a limitation in firewalld.
|
2018-05-10 10:13:53 +00:00
|
|
|
This also means that you will have to reload firewalld after adding a zone that you wish to perform immediate actions on.
|
2017-12-12 08:34:41 +00:00
|
|
|
The module will not take care of this for you implicitly because that would undo any previously performed immediate actions which were not
|
2018-07-12 19:09:33 +00:00
|
|
|
permanent. Therefore, if you require immediate access to a newly created zone it is recommended you reload firewalld immediately after the zone
|
2017-12-12 08:34:41 +00:00
|
|
|
creation returns with a changed state and before you perform any other immediate, non-permanent actions on that zone.
|
2019-02-19 14:42:14 +00:00
|
|
|
requirements:
|
|
|
|
- firewalld >= 0.2.11
|
|
|
|
author:
|
|
|
|
- Adam Miller (@maxamillion)
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2018-12-18 15:53:46 +00:00
|
|
|
EXAMPLES = r'''
|
2016-12-02 15:48:22 +00:00
|
|
|
- firewalld:
|
|
|
|
service: https
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
port: 8081/tcp
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: disabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
port: 161-162/udp
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
zone: dmz
|
|
|
|
service: http
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
2018-12-18 15:53:46 +00:00
|
|
|
rich_rule: rule service name="ftp" audit limit value="1/m" accept
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
source: 192.0.2.0/24
|
|
|
|
zone: internal
|
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
zone: trusted
|
|
|
|
interface: eth2
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
state: enabled
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
masquerade: yes
|
|
|
|
state: enabled
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2016-12-02 15:48:22 +00:00
|
|
|
zone: dmz
|
2017-12-12 08:34:41 +00:00
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
zone: custom
|
|
|
|
state: present
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
2018-05-01 21:33:34 +00:00
|
|
|
|
2018-12-12 20:01:35 +00:00
|
|
|
- firewalld:
|
|
|
|
zone: drop
|
|
|
|
state: present
|
|
|
|
permanent: yes
|
|
|
|
icmp_block_inversion: yes
|
|
|
|
|
|
|
|
- firewalld:
|
|
|
|
zone: drop
|
|
|
|
state: present
|
|
|
|
permanent: yes
|
|
|
|
icmp_block: echo-request
|
|
|
|
|
2018-05-01 21:33:34 +00:00
|
|
|
- name: Redirect port 443 to 8443 with Rich Rule
|
|
|
|
firewalld:
|
2018-12-18 15:53:46 +00:00
|
|
|
rich_rule: rule forward-port port=443 protocol=tcp to-port=8443
|
|
|
|
zone: public
|
2018-08-24 08:17:56 +00:00
|
|
|
permanent: yes
|
|
|
|
immediate: yes
|
2018-12-18 15:53:46 +00:00
|
|
|
state: enabled
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2016-10-12 20:26:54 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-04-30 20:33:28 +00:00
|
|
|
from ansible.module_utils.firewalld import FirewallTransaction, fw_offline
|
2016-10-12 20:26:54 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
try:
|
|
|
|
from firewall.client import Rich_Rule
|
2017-12-12 08:34:41 +00:00
|
|
|
from firewall.client import FirewallClientZoneSettings
|
2017-10-19 21:09:06 +00:00
|
|
|
except ImportError:
|
2018-04-30 20:33:28 +00:00
|
|
|
# The import errors are handled via FirewallTransaction, don't need to
|
|
|
|
# duplicate that here
|
|
|
|
pass
|
2016-10-05 22:31:50 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-12-12 20:01:35 +00:00
|
|
|
class IcmpBlockTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
IcmpBlockTransaction
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
|
|
|
super(IcmpBlockTransaction, self).__init__(
|
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_enabled_immediate(self, icmp_block, timeout):
|
|
|
|
return icmp_block in self.fw.getIcmpBlocks(self.zone)
|
|
|
|
|
|
|
|
def get_enabled_permanent(self, icmp_block, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
return icmp_block in fw_settings.getIcmpBlocks()
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, icmp_block, timeout):
|
|
|
|
self.fw.addIcmpBlock(self.zone, icmp_block, timeout)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self, icmp_block, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.addIcmpBlock(icmp_block)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, icmp_block, timeout):
|
|
|
|
self.fw.removeIcmpBlock(self.zone, icmp_block)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self, icmp_block, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removeIcmpBlock(icmp_block)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class IcmpBlockInversionTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
IcmpBlockInversionTransaction
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
|
|
|
super(IcmpBlockInversionTransaction, self).__init__(
|
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_enabled_immediate(self):
|
|
|
|
if self.fw.queryIcmpBlockInversion(self.zone) is True:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
if fw_settings.getIcmpBlockInversion() is True:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self):
|
|
|
|
self.fw.addIcmpBlockInversion(self.zone)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.setIcmpBlockInversion(True)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self):
|
|
|
|
self.fw.removeIcmpBlockInversion(self.zone)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.setIcmpBlockInversion(False)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
class ServiceTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
ServiceTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(ServiceTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
def get_enabled_immediate(self, service, timeout):
|
|
|
|
if service in self.fw.getServices(self.zone):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
def get_enabled_permanent(self, service, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
|
|
|
|
if service in fw_settings.getServices():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, service, timeout):
|
|
|
|
self.fw.addService(self.zone, service, timeout)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self, service, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.addService(service)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, service, timeout):
|
|
|
|
self.fw.removeService(self.zone, service)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self, service, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removeService(service)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class MasqueradeTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
MasqueradeTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(MasqueradeTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
self.enabled_msg = "Added masquerade to zone %s" % self.zone
|
|
|
|
self.disabled_msg = "Removed masquerade from zone %s" % self.zone
|
|
|
|
|
|
|
|
def get_enabled_immediate(self):
|
|
|
|
if self.fw.queryMasquerade(self.zone) is True:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
if fw_settings.getMasquerade() is True:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self):
|
|
|
|
self.fw.addMasquerade(self.zone)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.setMasquerade(True)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self):
|
|
|
|
self.fw.removeMasquerade(self.zone)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.setMasquerade(False)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class PortTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
PortTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(PortTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
def get_enabled_immediate(self, port, protocol, timeout):
|
|
|
|
port_proto = [port, protocol]
|
|
|
|
if self.fw_offline:
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
ports_list = fw_settings.getPorts()
|
|
|
|
else:
|
|
|
|
ports_list = self.fw.getPorts(self.zone)
|
|
|
|
|
|
|
|
if port_proto in ports_list:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self, port, protocol, timeout):
|
|
|
|
port_proto = (port, protocol)
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
|
|
|
|
if port_proto in fw_settings.getPorts():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, port, protocol, timeout):
|
|
|
|
self.fw.addPort(self.zone, port, protocol, timeout)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self, port, protocol, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.addPort(port, protocol)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, port, protocol, timeout):
|
|
|
|
self.fw.removePort(self.zone, port, protocol)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self, port, protocol, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removePort(port, protocol)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class InterfaceTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
InterfaceTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(InterfaceTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
self.enabled_msg = "Changed %s to zone %s" % \
|
|
|
|
(self.action_args[0], self.zone)
|
|
|
|
|
|
|
|
self.disabled_msg = "Removed %s from zone %s" % \
|
|
|
|
(self.action_args[0], self.zone)
|
|
|
|
|
|
|
|
def get_enabled_immediate(self, interface):
|
|
|
|
if self.fw_offline:
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
interface_list = fw_settings.getInterfaces()
|
|
|
|
else:
|
|
|
|
interface_list = self.fw.getInterfaces(self.zone)
|
|
|
|
if interface in interface_list:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self, interface):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
|
|
|
|
if interface in fw_settings.getInterfaces():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, interface):
|
|
|
|
self.fw.changeZoneOfInterface(self.zone, interface)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self, interface):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
if self.fw_offline:
|
|
|
|
iface_zone_objs = []
|
|
|
|
for zone in self.fw.config.get_zones():
|
|
|
|
old_zone_obj = self.fw.config.get_zone(zone)
|
|
|
|
if interface in old_zone_obj.interfaces:
|
|
|
|
iface_zone_objs.append(old_zone_obj)
|
|
|
|
if len(iface_zone_objs) > 1:
|
|
|
|
# Even it shouldn't happen, it's actually possible that
|
|
|
|
# the same interface is in several zone XML files
|
2018-04-30 20:33:28 +00:00
|
|
|
self.module.fail_json(
|
2019-02-14 20:02:27 +00:00
|
|
|
msg='ERROR: interface {0} is in {1} zone XML file, can only be in one'.format(
|
2017-09-11 21:10:07 +00:00
|
|
|
interface,
|
|
|
|
len(iface_zone_objs)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
old_zone_obj = iface_zone_objs[0]
|
|
|
|
if old_zone_obj.name != self.zone:
|
|
|
|
old_zone_settings = FirewallClientZoneSettings(
|
|
|
|
self.fw.config.get_zone_config(old_zone_obj)
|
|
|
|
)
|
|
|
|
old_zone_settings.removeInterface(interface) # remove from old
|
|
|
|
self.fw.config.set_zone_config(
|
|
|
|
old_zone_obj,
|
|
|
|
old_zone_settings.settings
|
|
|
|
)
|
|
|
|
fw_settings.addInterface(interface) # add to new
|
|
|
|
self.fw.config.set_zone_config(fw_zone, fw_settings.settings)
|
|
|
|
else:
|
|
|
|
old_zone_name = self.fw.config().getZoneOfInterface(interface)
|
|
|
|
if old_zone_name != self.zone:
|
|
|
|
if old_zone_name:
|
|
|
|
old_zone_obj = self.fw.config().getZoneByName(old_zone_name)
|
|
|
|
old_zone_settings = old_zone_obj.getSettings()
|
|
|
|
old_zone_settings.removeInterface(interface) # remove from old
|
|
|
|
old_zone_obj.update(old_zone_settings)
|
|
|
|
fw_settings.addInterface(interface) # add to new
|
|
|
|
fw_zone.update(fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, interface):
|
|
|
|
self.fw.removeInterface(self.zone, interface)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self, interface):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removeInterface(interface)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class RichRuleTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
RichRuleTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(RichRuleTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
def get_enabled_immediate(self, rule, timeout):
|
|
|
|
# Convert the rule string to standard format
|
|
|
|
# before checking whether it is present
|
|
|
|
rule = str(Rich_Rule(rule_str=rule))
|
|
|
|
if rule in self.fw.getRichRules(self.zone):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self, rule, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
# Convert the rule string to standard format
|
|
|
|
# before checking whether it is present
|
|
|
|
rule = str(Rich_Rule(rule_str=rule))
|
|
|
|
if rule in fw_settings.getRichRules():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, rule, timeout):
|
|
|
|
self.fw.addRichRule(self.zone, rule, timeout)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
def set_enabled_permanent(self, rule, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.addRichRule(rule)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, rule, timeout):
|
2018-04-30 20:33:28 +00:00
|
|
|
self.fw.removeRichRule(self.zone, rule)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
def set_disabled_permanent(self, rule, timeout):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removeRichRule(rule)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
|
|
|
|
class SourceTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
SourceTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False):
|
2017-09-11 21:10:07 +00:00
|
|
|
super(SourceTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate
|
|
|
|
)
|
2017-09-11 21:10:07 +00:00
|
|
|
|
|
|
|
self.enabled_msg = "Added %s to zone %s" % \
|
|
|
|
(self.action_args[0], self.zone)
|
|
|
|
|
|
|
|
self.disabled_msg = "Removed %s from zone %s" % \
|
|
|
|
(self.action_args[0], self.zone)
|
|
|
|
|
|
|
|
def get_enabled_immediate(self, source):
|
|
|
|
if source in self.fw.getSources(self.zone):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_enabled_permanent(self, source):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
if source in fw_settings.getSources():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self, source):
|
|
|
|
self.fw.addSource(self.zone, source)
|
|
|
|
|
|
|
|
def set_enabled_permanent(self, source):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.addSource(source)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
|
|
|
|
|
|
|
def set_disabled_immediate(self, source):
|
|
|
|
self.fw.removeSource(self.zone, source)
|
|
|
|
|
|
|
|
def set_disabled_permanent(self, source):
|
|
|
|
fw_zone, fw_settings = self.get_fw_zone_settings()
|
|
|
|
fw_settings.removeSource(source)
|
|
|
|
self.update_fw_settings(fw_zone, fw_settings)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
2017-12-12 08:34:41 +00:00
|
|
|
class ZoneTransaction(FirewallTransaction):
|
|
|
|
"""
|
|
|
|
ZoneTransaction
|
|
|
|
"""
|
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
def __init__(self, module, action_args=None, zone=None, desired_state=None,
|
|
|
|
permanent=True, immediate=False, enabled_values=None, disabled_values=None):
|
2017-12-12 08:34:41 +00:00
|
|
|
super(ZoneTransaction, self).__init__(
|
2018-04-30 20:33:28 +00:00
|
|
|
module, action_args=action_args, desired_state=desired_state, zone=zone,
|
|
|
|
permanent=permanent, immediate=immediate,
|
2017-12-12 08:34:41 +00:00
|
|
|
enabled_values=enabled_values or ["present"],
|
|
|
|
disabled_values=disabled_values or ["absent"])
|
|
|
|
|
|
|
|
self.enabled_msg = "Added zone %s" % \
|
|
|
|
(self.zone)
|
|
|
|
|
|
|
|
self.disabled_msg = "Removed zone %s" % \
|
|
|
|
(self.zone)
|
|
|
|
|
|
|
|
self.tx_not_permanent_error_msg = "Zone operations must be permanent. " \
|
|
|
|
"Make sure you didn't set the 'permanent' flag to 'false' or the 'immediate' flag to 'true'."
|
|
|
|
|
|
|
|
def get_enabled_immediate(self):
|
2018-04-30 20:33:28 +00:00
|
|
|
self.module.fail_json(msg=self.tx_not_permanent_error_msg)
|
2017-12-12 08:34:41 +00:00
|
|
|
|
|
|
|
def get_enabled_permanent(self):
|
2018-08-12 07:08:05 +00:00
|
|
|
zones = self.fw.config().listZones()
|
|
|
|
zone_names = [self.fw.config().getZone(z).get_property("name") for z in zones]
|
|
|
|
if self.zone in zone_names:
|
2017-12-12 08:34:41 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def set_enabled_immediate(self):
|
2018-04-30 20:33:28 +00:00
|
|
|
self.module.fail_json(msg=self.tx_not_permanent_error_msg)
|
2017-12-12 08:34:41 +00:00
|
|
|
|
|
|
|
def set_enabled_permanent(self):
|
2018-04-30 20:33:28 +00:00
|
|
|
self.fw.config().addZone(self.zone, FirewallClientZoneSettings())
|
2017-12-12 08:34:41 +00:00
|
|
|
|
|
|
|
def set_disabled_immediate(self):
|
2018-04-30 20:33:28 +00:00
|
|
|
self.module.fail_json(msg=self.tx_not_permanent_error_msg)
|
2017-12-12 08:34:41 +00:00
|
|
|
|
|
|
|
def set_disabled_permanent(self):
|
|
|
|
zone_obj = self.fw.config().getZoneByName(self.zone)
|
|
|
|
zone_obj.remove()
|
|
|
|
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2017-09-11 21:10:07 +00:00
|
|
|
argument_spec=dict(
|
2018-12-18 15:53:46 +00:00
|
|
|
icmp_block=dict(type='str'),
|
|
|
|
icmp_block_inversion=dict(type='str'),
|
|
|
|
service=dict(type='str'),
|
|
|
|
port=dict(type='str'),
|
|
|
|
rich_rule=dict(type='str'),
|
|
|
|
zone=dict(type='str'),
|
2017-09-11 21:10:07 +00:00
|
|
|
immediate=dict(type='bool', default=False),
|
2018-12-18 15:53:46 +00:00
|
|
|
source=dict(type='str'),
|
|
|
|
permanent=dict(type='bool'),
|
|
|
|
state=dict(type='str', required=True, choices=['absent', 'disabled', 'enabled', 'present']),
|
|
|
|
timeout=dict(type='int', default=0),
|
|
|
|
interface=dict(type='str'),
|
|
|
|
masquerade=dict(type='str'),
|
|
|
|
offline=dict(type='bool'),
|
2014-09-26 01:01:01 +00:00
|
|
|
),
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
required_by=dict(
|
2019-02-19 19:29:51 +00:00
|
|
|
interface=('zone',),
|
|
|
|
source=('permanent',),
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
),
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
2016-10-05 22:31:50 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
permanent = module.params['permanent']
|
|
|
|
desired_state = module.params['state']
|
|
|
|
immediate = module.params['immediate']
|
|
|
|
timeout = module.params['timeout']
|
|
|
|
interface = module.params['interface']
|
|
|
|
masquerade = module.params['masquerade']
|
2016-10-05 22:31:50 +00:00
|
|
|
|
2018-04-30 20:33:28 +00:00
|
|
|
# Sanity checks
|
|
|
|
FirewallTransaction.sanity_check(module)
|
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
# If neither permanent or immediate is provided, assume immediate (as
|
|
|
|
# written in the module's docs)
|
|
|
|
if not permanent and not immediate:
|
|
|
|
immediate = True
|
2016-01-10 22:18:09 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
# Verify required params are provided
|
|
|
|
if immediate and fw_offline:
|
2018-04-12 13:43:00 +00:00
|
|
|
module.fail_json(msg='firewall is not currently running, unable to perform immediate actions without a running firewall daemon')
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed = False
|
2014-09-26 01:01:01 +00:00
|
|
|
msgs = []
|
2018-12-12 20:01:35 +00:00
|
|
|
icmp_block = module.params['icmp_block']
|
|
|
|
icmp_block_inversion = module.params['icmp_block_inversion']
|
2014-09-26 01:01:01 +00:00
|
|
|
service = module.params['service']
|
|
|
|
rich_rule = module.params['rich_rule']
|
2016-12-08 05:34:16 +00:00
|
|
|
source = module.params['source']
|
2018-04-30 20:33:28 +00:00
|
|
|
zone = module.params['zone']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
if module.params['port'] is not None:
|
2019-01-02 17:09:42 +00:00
|
|
|
if '/' in module.params['port']:
|
|
|
|
port, protocol = module.params['port'].strip().split('/')
|
|
|
|
else:
|
|
|
|
protocol = None
|
|
|
|
if not protocol:
|
2014-09-26 01:01:01 +00:00
|
|
|
module.fail_json(msg='improper port format (missing protocol?)')
|
|
|
|
else:
|
|
|
|
port = None
|
|
|
|
|
|
|
|
modification_count = 0
|
2018-12-12 20:01:35 +00:00
|
|
|
if icmp_block is not None:
|
|
|
|
modification_count += 1
|
|
|
|
if icmp_block_inversion is not None:
|
|
|
|
modification_count += 1
|
2017-01-28 08:12:11 +00:00
|
|
|
if service is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
modification_count += 1
|
2017-01-28 08:12:11 +00:00
|
|
|
if port is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
modification_count += 1
|
2017-01-28 08:12:11 +00:00
|
|
|
if rich_rule is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
modification_count += 1
|
2017-01-28 08:12:11 +00:00
|
|
|
if interface is not None:
|
2016-01-10 22:18:09 +00:00
|
|
|
modification_count += 1
|
2017-01-28 08:12:11 +00:00
|
|
|
if masquerade is not None:
|
2016-04-16 12:15:00 +00:00
|
|
|
modification_count += 1
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if modification_count > 1:
|
2017-09-11 21:10:07 +00:00
|
|
|
module.fail_json(
|
2018-12-12 20:01:35 +00:00
|
|
|
msg='can only operate on port, service, rich_rule, masquerade, icmp_block, icmp_block_inversion, or interface at once'
|
2017-09-11 21:10:07 +00:00
|
|
|
)
|
2017-12-12 08:34:41 +00:00
|
|
|
elif modification_count > 0 and desired_state in ['absent', 'present']:
|
|
|
|
module.fail_json(
|
|
|
|
msg='absent and present state can only be used in zone level operations'
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-12-12 20:01:35 +00:00
|
|
|
if icmp_block is not None:
|
|
|
|
|
|
|
|
transaction = IcmpBlockTransaction(
|
|
|
|
module,
|
|
|
|
action_args=(icmp_block, timeout),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
|
|
|
|
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
|
|
|
if changed is True:
|
|
|
|
msgs.append("Changed icmp-block %s to %s" % (icmp_block, desired_state))
|
|
|
|
|
|
|
|
if icmp_block_inversion is not None:
|
|
|
|
|
|
|
|
transaction = IcmpBlockInversionTransaction(
|
|
|
|
module,
|
|
|
|
action_args=(),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
|
|
|
|
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
|
|
|
if changed is True:
|
|
|
|
msgs.append("Changed icmp-block-inversion %s to %s" % (icmp_block_inversion, desired_state))
|
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
if service is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = ServiceTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(service, timeout),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2017-02-24 22:49:43 +00:00
|
|
|
if changed is True:
|
2014-09-26 01:01:01 +00:00
|
|
|
msgs.append("Changed service %s to %s" % (service, desired_state))
|
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
if source is not None:
|
2016-01-10 22:18:09 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = SourceTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(source,),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
if port is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = PortTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(port, protocol, timeout),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2017-02-24 22:49:43 +00:00
|
|
|
if changed is True:
|
2017-09-11 21:10:07 +00:00
|
|
|
msgs.append(
|
|
|
|
"Changed port %s to %s" % (
|
|
|
|
"%s/%s" % (port, protocol), desired_state
|
|
|
|
)
|
2016-10-06 21:38:39 +00:00
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
if rich_rule is not None:
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = RichRuleTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(rich_rule, timeout),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2017-02-24 22:49:43 +00:00
|
|
|
if changed is True:
|
2014-09-26 01:01:01 +00:00
|
|
|
msgs.append("Changed rich_rule %s to %s" % (rich_rule, desired_state))
|
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
if interface is not None:
|
2016-10-06 21:38:39 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = InterfaceTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(interface,),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2016-05-02 15:16:07 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2016-01-10 22:18:09 +00:00
|
|
|
|
2017-01-28 08:12:11 +00:00
|
|
|
if masquerade is not None:
|
2016-04-16 12:15:00 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
transaction = MasqueradeTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-09-11 21:10:07 +00:00
|
|
|
action_args=(),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
2016-04-16 12:15:00 +00:00
|
|
|
|
2017-09-11 21:10:07 +00:00
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
2016-04-16 12:15:00 +00:00
|
|
|
|
2017-12-12 08:34:41 +00:00
|
|
|
''' If there are no changes within the zone we are operating on the zone itself '''
|
|
|
|
if modification_count == 0 and desired_state in ['absent', 'present']:
|
|
|
|
|
|
|
|
transaction = ZoneTransaction(
|
2018-04-30 20:33:28 +00:00
|
|
|
module,
|
2017-12-12 08:34:41 +00:00
|
|
|
action_args=(),
|
|
|
|
zone=zone,
|
|
|
|
desired_state=desired_state,
|
|
|
|
permanent=permanent,
|
|
|
|
immediate=immediate,
|
|
|
|
)
|
|
|
|
|
|
|
|
changed, transaction_msgs = transaction.run()
|
|
|
|
msgs = msgs + transaction_msgs
|
|
|
|
if changed is True:
|
|
|
|
msgs.append("Changed zone %s to %s" % (zone, desired_state))
|
|
|
|
|
2016-10-05 22:31:50 +00:00
|
|
|
if fw_offline:
|
|
|
|
msgs.append("(offline operation: only on-disk configs were altered)")
|
2017-09-11 21:10:07 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
module.exit_json(changed=changed, msg=', '.join(msgs))
|
|
|
|
|
|
|
|
|
2016-10-12 20:26:54 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|