Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow-firewalld-take-multiple-input #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions changelogs/fragments/firewalld_multiple_input_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- firewalld - Allow multiple values input as a list or coma separated string
for input types source, service, port, icmp_block, interface, rich_rule.
185 changes: 102 additions & 83 deletions plugins/modules/firewalld.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
description:
- Name of a service to add/remove to/from firewalld.
- The service must be listed in output of firewall-cmd --get-services.
type: str
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
type: list
elements: str
port:
description:
- 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
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
type: list
elements: str
port_forward:
description:
- Port and protocol to forward using firewalld.
Expand Down Expand Up @@ -54,19 +58,27 @@
description:
- Rich rule to add/remove to/from firewalld.
- See L(Syntax for firewalld rich language rules,https://firewalld.org/documentation/man-pages/firewalld.richlanguage.html).
type: str
- Multiple values can be provided using a list.
type: list
elements: str
source:
description:
- The source/network you would like to add/remove to/from firewalld.
type: str
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
type: list
elements: str
interface:
description:
- The interface you would like to add/remove to/from a zone in firewalld.
type: str
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
type: list
elements: str
icmp_block:
description:
- The ICMP block you would like to add/remove to/from a zone in firewalld.
type: str
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
type: list
elements: str
icmp_block_inversion:
description:
- Enable/Disable inversion of ICMP blocks for a zone in firewalld.
Expand Down Expand Up @@ -757,19 +769,19 @@ def main():

module = AnsibleModule(
argument_spec=dict(
icmp_block=dict(type='str'),
icmp_block=dict(type='list', elements='str'),
icmp_block_inversion=dict(type='str'),
service=dict(type='str'),
port=dict(type='str'),
service=dict(type='list', elements='str'),
port=dict(type='list', elements='str'),
port_forward=dict(type='list', elements='dict'),
rich_rule=dict(type='str'),
rich_rule=dict(type='list', elements='str'),
zone=dict(type='str'),
immediate=dict(type='bool', default=False),
source=dict(type='str'),
source=dict(type='list', elements='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'),
interface=dict(type='list', elements='str'),
masquerade=dict(type='str'),
offline=dict(type='bool'),
target=dict(type='str', choices=['default', 'ACCEPT', 'DROP', 'REJECT']),
Expand Down Expand Up @@ -868,20 +880,21 @@ def main():
)

if icmp_block is not None:
for _icmp_block in icmp_block:
_icmp_block = _icmp_block.strip()
transaction = IcmpBlockTransaction(
module,
action_args=(_icmp_block, timeout),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

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))
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:

Expand All @@ -900,52 +913,55 @@ def main():
msgs.append("Changed icmp-block-inversion %s to %s" % (icmp_block_inversion, desired_state))

if service is not None:
for _service in service:
_service = _service.strip()
transaction = ServiceTransaction(
module,
action_args=(_service, timeout),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

transaction = ServiceTransaction(
module,
action_args=(service, 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 service %s to %s" % (service, desired_state))
changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs
if changed is True:
msgs.append("Changed service %s to %s" % (_service, desired_state))

if source is not None:
for _source in source:
_source = _source.strip()
transaction = SourceTransaction(
module,
action_args=(_source,),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

transaction = SourceTransaction(
module,
action_args=(source,),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs
changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs

if port is not None:

transaction = PortTransaction(
module,
action_args=(port, protocol, timeout),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)
for _port in port:
_port = _port.strip()
transaction = PortTransaction(
module,
action_args=(_port, protocol, 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 port %s to %s" % (
"%s/%s" % (port, protocol), desired_state
"%s/%s" % (_port, protocol), desired_state
)
)

Expand Down Expand Up @@ -973,34 +989,37 @@ def main():
)

if rich_rule is not None:
for _rich_rule in rich_rule:
if _rich_rule == '':
continue
transaction = RichRuleTransaction(
module,
action_args=(_rich_rule, timeout),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

transaction = RichRuleTransaction(
module,
action_args=(rich_rule, 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 rich_rule %s to %s" % (rich_rule, desired_state))
changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs
if changed is True:
msgs.append("Changed rich_rule %s to %s" % (_rich_rule, desired_state))

if interface is not None:
for _interface in interface:
_interface = _interface.strip()
transaction = InterfaceTransaction(
module,
action_args=(_interface,),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

transaction = InterfaceTransaction(
module,
action_args=(interface,),
zone=zone,
desired_state=desired_state,
permanent=permanent,
immediate=immediate,
)

changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs
changed, transaction_msgs = transaction.run()
msgs = msgs + transaction_msgs

if masquerade is not None:

Expand Down