Skip to content

Commit 469234d

Browse files
committed
allow-firewalld-take-multiple-input
The currentl firewalld module does not take multiple values such as `source` or `interface`, etc.. There are many cases that we need to pass multi volume to the module rather than flatening the input so I implement it in this PR. This change is backward compatible, that is the behaviour wont change after this change, an existing user uses single value will work as is.
1 parent f6f436f commit 469234d

File tree

2 files changed

+105
-83
lines changed

2 files changed

+105
-83
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
minor_changes:
2+
- firewalld - Allow multiple values input as a list or coma separated string
3+
for input types source, service, port, icmp_block, interface, rich_rule.

plugins/modules/firewalld.py

Lines changed: 102 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
description:
1919
- Name of a service to add/remove to/from firewalld.
2020
- The service must be listed in output of firewall-cmd --get-services.
21-
type: str
21+
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
22+
type: list
23+
elements: str
2224
port:
2325
description:
2426
- Name of a port or port range to add/remove to/from firewalld.
2527
- Must be in the form PORT/PROTOCOL or PORT-PORT/PROTOCOL for port ranges.
26-
type: str
28+
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
29+
type: list
30+
elements: str
2731
port_forward:
2832
description:
2933
- Port and protocol to forward using firewalld.
@@ -54,19 +58,27 @@
5458
description:
5559
- Rich rule to add/remove to/from firewalld.
5660
- See L(Syntax for firewalld rich language rules,https://firewalld.org/documentation/man-pages/firewalld.richlanguage.html).
57-
type: str
61+
- Multiple values can be provided using a list.
62+
type: list
63+
elements: str
5864
source:
5965
description:
6066
- The source/network you would like to add/remove to/from firewalld.
61-
type: str
67+
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
68+
type: list
69+
elements: str
6270
interface:
6371
description:
6472
- The interface you would like to add/remove to/from a zone in firewalld.
65-
type: str
73+
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
74+
type: list
75+
elements: str
6676
icmp_block:
6777
description:
6878
- The ICMP block you would like to add/remove to/from a zone in firewalld.
69-
type: str
79+
- Multiple values can be provided using a list or a comma separated list (space after comma is allowed).
80+
type: list
81+
elements: str
7082
icmp_block_inversion:
7183
description:
7284
- Enable/Disable inversion of ICMP blocks for a zone in firewalld.
@@ -757,19 +769,19 @@ def main():
757769

758770
module = AnsibleModule(
759771
argument_spec=dict(
760-
icmp_block=dict(type='str'),
772+
icmp_block=dict(type='list', elements='str'),
761773
icmp_block_inversion=dict(type='str'),
762-
service=dict(type='str'),
763-
port=dict(type='str'),
774+
service=dict(type='list', elements='str'),
775+
port=dict(type='list', elements='str'),
764776
port_forward=dict(type='list', elements='dict'),
765-
rich_rule=dict(type='str'),
777+
rich_rule=dict(type='list', elements='str'),
766778
zone=dict(type='str'),
767779
immediate=dict(type='bool', default=False),
768-
source=dict(type='str'),
780+
source=dict(type='list', elements='str'),
769781
permanent=dict(type='bool'),
770782
state=dict(type='str', required=True, choices=['absent', 'disabled', 'enabled', 'present']),
771783
timeout=dict(type='int', default=0),
772-
interface=dict(type='str'),
784+
interface=dict(type='list', elements='str'),
773785
masquerade=dict(type='str'),
774786
offline=dict(type='bool'),
775787
target=dict(type='str', choices=['default', 'ACCEPT', 'DROP', 'REJECT']),
@@ -868,20 +880,21 @@ def main():
868880
)
869881

870882
if icmp_block is not None:
883+
for _icmp_block in icmp_block:
884+
_icmp_block = _icmp_block.strip()
885+
transaction = IcmpBlockTransaction(
886+
module,
887+
action_args=(_icmp_block, timeout),
888+
zone=zone,
889+
desired_state=desired_state,
890+
permanent=permanent,
891+
immediate=immediate,
892+
)
871893

872-
transaction = IcmpBlockTransaction(
873-
module,
874-
action_args=(icmp_block, timeout),
875-
zone=zone,
876-
desired_state=desired_state,
877-
permanent=permanent,
878-
immediate=immediate,
879-
)
880-
881-
changed, transaction_msgs = transaction.run()
882-
msgs = msgs + transaction_msgs
883-
if changed is True:
884-
msgs.append("Changed icmp-block %s to %s" % (icmp_block, desired_state))
894+
changed, transaction_msgs = transaction.run()
895+
msgs = msgs + transaction_msgs
896+
if changed is True:
897+
msgs.append("Changed icmp-block %s to %s" % (_icmp_block, desired_state))
885898

886899
if icmp_block_inversion is not None:
887900

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

902915
if service is not None:
916+
for _service in service:
917+
_service = _service.strip()
918+
transaction = ServiceTransaction(
919+
module,
920+
action_args=(_service, timeout),
921+
zone=zone,
922+
desired_state=desired_state,
923+
permanent=permanent,
924+
immediate=immediate,
925+
)
903926

904-
transaction = ServiceTransaction(
905-
module,
906-
action_args=(service, timeout),
907-
zone=zone,
908-
desired_state=desired_state,
909-
permanent=permanent,
910-
immediate=immediate,
911-
)
912-
913-
changed, transaction_msgs = transaction.run()
914-
msgs = msgs + transaction_msgs
915-
if changed is True:
916-
msgs.append("Changed service %s to %s" % (service, desired_state))
927+
changed, transaction_msgs = transaction.run()
928+
msgs = msgs + transaction_msgs
929+
if changed is True:
930+
msgs.append("Changed service %s to %s" % (_service, desired_state))
917931

918932
if source is not None:
933+
for _source in source:
934+
_source = _source.strip()
935+
transaction = SourceTransaction(
936+
module,
937+
action_args=(_source,),
938+
zone=zone,
939+
desired_state=desired_state,
940+
permanent=permanent,
941+
immediate=immediate,
942+
)
919943

920-
transaction = SourceTransaction(
921-
module,
922-
action_args=(source,),
923-
zone=zone,
924-
desired_state=desired_state,
925-
permanent=permanent,
926-
immediate=immediate,
927-
)
928-
929-
changed, transaction_msgs = transaction.run()
930-
msgs = msgs + transaction_msgs
944+
changed, transaction_msgs = transaction.run()
945+
msgs = msgs + transaction_msgs
931946

932947
if port is not None:
933-
934-
transaction = PortTransaction(
935-
module,
936-
action_args=(port, protocol, timeout),
937-
zone=zone,
938-
desired_state=desired_state,
939-
permanent=permanent,
940-
immediate=immediate,
941-
)
948+
for _port in port:
949+
_port = _port.strip()
950+
transaction = PortTransaction(
951+
module,
952+
action_args=(_port, protocol, timeout),
953+
zone=zone,
954+
desired_state=desired_state,
955+
permanent=permanent,
956+
immediate=immediate,
957+
)
942958

943959
changed, transaction_msgs = transaction.run()
944960
msgs = msgs + transaction_msgs
945961
if changed is True:
946962
msgs.append(
947963
"Changed port %s to %s" % (
948-
"%s/%s" % (port, protocol), desired_state
964+
"%s/%s" % (_port, protocol), desired_state
949965
)
950966
)
951967

@@ -973,34 +989,37 @@ def main():
973989
)
974990

975991
if rich_rule is not None:
992+
for _rich_rule in rich_rule:
993+
if _rich_rule == '':
994+
continue
995+
transaction = RichRuleTransaction(
996+
module,
997+
action_args=(_rich_rule, timeout),
998+
zone=zone,
999+
desired_state=desired_state,
1000+
permanent=permanent,
1001+
immediate=immediate,
1002+
)
9761003

977-
transaction = RichRuleTransaction(
978-
module,
979-
action_args=(rich_rule, timeout),
980-
zone=zone,
981-
desired_state=desired_state,
982-
permanent=permanent,
983-
immediate=immediate,
984-
)
985-
986-
changed, transaction_msgs = transaction.run()
987-
msgs = msgs + transaction_msgs
988-
if changed is True:
989-
msgs.append("Changed rich_rule %s to %s" % (rich_rule, desired_state))
1004+
changed, transaction_msgs = transaction.run()
1005+
msgs = msgs + transaction_msgs
1006+
if changed is True:
1007+
msgs.append("Changed rich_rule %s to %s" % (_rich_rule, desired_state))
9901008

9911009
if interface is not None:
1010+
for _interface in interface:
1011+
_interface = _interface.strip()
1012+
transaction = InterfaceTransaction(
1013+
module,
1014+
action_args=(_interface,),
1015+
zone=zone,
1016+
desired_state=desired_state,
1017+
permanent=permanent,
1018+
immediate=immediate,
1019+
)
9921020

993-
transaction = InterfaceTransaction(
994-
module,
995-
action_args=(interface,),
996-
zone=zone,
997-
desired_state=desired_state,
998-
permanent=permanent,
999-
immediate=immediate,
1000-
)
1001-
1002-
changed, transaction_msgs = transaction.run()
1003-
msgs = msgs + transaction_msgs
1021+
changed, transaction_msgs = transaction.run()
1022+
msgs = msgs + transaction_msgs
10041023

10051024
if masquerade is not None:
10061025

0 commit comments

Comments
 (0)