add check mode to firewall module

This commit is contained in:
Valentin Gurmeza 2017-06-01 17:40:53 -07:00
parent 48f593bae2
commit caa99b3e5d
2 changed files with 60 additions and 30 deletions

View file

@ -83,7 +83,8 @@ def main():
choices = ['present', 'absent'], choices = ['present', 'absent'],
type = 'str' type = 'str'
), ),
) ),
supports_check_mode=True
) )
hostname = module.params['hostname'] hostname = module.params['hostname']
@ -92,6 +93,7 @@ def main():
rule = module.params['rule'] rule = module.params['rule']
state = module.params['state'] state = module.params['state']
api_path = '/ip/firewall/' + module.params['parameter'] api_path = '/ip/firewall/' + module.params['parameter']
check_mode = module.check_mode
# ############################################## # ##############################################
# Check if "place-before" is an integer # Check if "place-before" is an integer
# ############################################# # #############################################
@ -153,6 +155,7 @@ def main():
# if we don't have an existing rule to match # if we don't have an existing rule to match
# the desired we create a new one # the desired we create a new one
if not current_rule: if not current_rule:
if not check_mode:
mk.api_add(api_path, rule) mk.api_add(api_path, rule)
changed = True, changed = True,
# if current_rule is true we need to ensure the changes # if current_rule is true we need to ensure the changes
@ -176,6 +179,7 @@ def main():
if current_id is not None: if current_id is not None:
out_params['.id'] = current_id out_params['.id'] = current_id
if not check_mode:
mk.api_edit( mk.api_edit(
base_path = api_path, base_path = api_path,
params = out_params params = out_params
@ -207,6 +211,7 @@ def main():
'destination': desired_order 'destination': desired_order
} }
if params: if params:
if not check_mode:
mk.api_command(api_path, params) mk.api_command(api_path, params)
changed_msg.append({ changed_msg.append({
"moved": existing_order, "moved": existing_order,
@ -219,6 +224,7 @@ def main():
##################################### #####################################
elif state == "absent": elif state == "absent":
if current_rule: if current_rule:
if not check_mode:
mk.api_remove(api_path, current_id) mk.api_remove(api_path, current_id)
changed = True changed = True
changed_msg.append("removed rule: " + str(desired_order)) changed_msg.append("removed rule: " + str(desired_order))

View file

@ -805,7 +805,11 @@
comment: 'Ansible - fw filter rule5' comment: 'Ansible - fw filter rule5'
place-before: '4' place-before: '4'
register: check_idem register: check_idem
failed_when: ( check_idem | changed ) failed_when: (
not ansible_check_mode
) and (
( check_idem | changed )
)
tags: test-firewall tags: test-firewall
- name: ALWAYS_CHANGES Test editing existing rule - name: ALWAYS_CHANGES Test editing existing rule
@ -823,7 +827,11 @@
src-address: 192.168.0.0/16 src-address: 192.168.0.0/16
place-before: '3' place-before: '3'
register: edit_filter_rule register: edit_filter_rule
failed_when: not ( edit_filter_rule | changed ) failed_when: (
not ansible_check_mode
) and (
not ( edit_filter_rule | changed )
)
- name: NEVER_CHANGES Test editing existing rule check idempotency again - name: NEVER_CHANGES Test editing existing rule check idempotency again
mt_ip_firewall: mt_ip_firewall:
@ -840,7 +848,11 @@
src-address: 192.168.0.0/16 src-address: 192.168.0.0/16
place-before: '3' place-before: '3'
register: edit_filter_rule_2 register: edit_filter_rule_2
failed_when: ( edit_filter_rule_2 | changed ) failed_when: (
not ansible_check_mode
) and (
( edit_filter_rule_2 | changed )
)
tags: test-firewall tags: test-firewall
- name: add a rule to the bottom of the chain - name: add a rule to the bottom of the chain
@ -873,7 +885,11 @@
src-address: 192.150.0.0/16 src-address: 192.150.0.0/16
place-before: '20' place-before: '20'
register: edit_filter_rule_3 register: edit_filter_rule_3
failed_when: not ( edit_filter_rule_3 | changed ) failed_when: (
not ansible_check_mode
) and (
not ( edit_filter_rule_3 | changed )
)
- name: NEVER_CHANGES add a rule to the bottom of the chain, check_idempotency - name: NEVER_CHANGES add a rule to the bottom of the chain, check_idempotency
mt_ip_firewall: mt_ip_firewall:
@ -890,7 +906,11 @@
src-address: 192.150.0.0/16 src-address: 192.150.0.0/16
place-before: '20' place-before: '20'
register: edit_filter_rule_4 register: edit_filter_rule_4
failed_when: ( edit_filter_rule_4 | changed ) failed_when: (
not ansible_check_mode
) and (
( edit_filter_rule_4 | changed )
)
- name: ALWAYS_CHANGES Test removing existing rule - name: ALWAYS_CHANGES Test removing existing rule
mt_ip_firewall: mt_ip_firewall:
@ -903,7 +923,11 @@
with_items: with_items:
- place-before: '4' - place-before: '4'
register: rem_filter_rule register: rem_filter_rule
failed_when: not ( rem_filter_rule | changed ) failed_when: (
not ansible_check_mode
) and (
not ( rem_filter_rule | changed )
)
tags: firewall-filter tags: firewall-filter
################### ###################