Skip to content

Commit

Permalink
Merge pull request #148 from scsitteam/purge_modulesofrerror
Browse files Browse the repository at this point in the history
Problem: Handle ModuleSoftError in purge.
  • Loading branch information
ansibleguy authored Feb 7, 2025
2 parents d4560ab + c245e1f commit 6556058
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
23 changes: 14 additions & 9 deletions plugins/module_utils/helper/purge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.handler import \
ModuleSoftError


def purge(
Expand All @@ -12,17 +14,20 @@ def purge(
result['diff']['after'][item_to_purge[diff_param]] = None

if not module.check_mode:
_obj = obj_func(item_to_purge)
_obj.exists = True
try:
_obj = obj_func(item_to_purge)
_obj.exists = True

if module.params['action'] == 'delete':
_obj.delete()
if module.params['action'] == 'delete':
_obj.delete()

else:
if _obj.b.is_enabled():
result['diff']['before'][item_to_purge[diff_param]] = {'enabled': True}
result['diff']['after'][item_to_purge[diff_param]] = {'enabled': False}
_obj.b.disable()
else:
if _obj.b.is_enabled():
result['diff']['before'][item_to_purge[diff_param]] = {'enabled': True}
result['diff']['after'][item_to_purge[diff_param]] = {'enabled': False}
_obj.b.disable()
except ModuleSoftError:
pass


def check_purge_filter(module: AnsibleModule, item: dict) -> bool:
Expand Down
25 changes: 25 additions & 0 deletions tests/alias_purge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@
opn7.data | length != 1
when: not ansible_check_mode

- name: Adding 4
ansibleguy.opnsense.alias_multi:
aliases:
ANSIBLE_TEST_4_1:
content: ['192.168.1.1', '192.168.1.2']
ANSIBLE_TEST_4_2:
content: 'ANSIBLE_TEST_4_1'
when: not ansible_check_mode

- name: Purge used alias
ansibleguy.opnsense.alias_purge:
aliases:
ANSIBLE_TEST_4_2:
when: not ansible_check_mode
register: opn11
failed_when: >
opn11.failed or
'warnings' not in opn11
- name: Purge using alias first
ansibleguy.opnsense.alias_purge:
aliases:
ANSIBLE_TEST_4_1:
when: not ansible_check_mode

- name: Purge ALL - not forced
ansibleguy.opnsense.alias_purge:
when: not ansible_check_mode
Expand Down

0 comments on commit 6556058

Please sign in to comment.