File: ansibull.py

package info (click to toggle)
ansible-core 2.19.0~beta6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,628 kB
  • sloc: python: 180,313; cs: 4,929; sh: 4,601; xml: 34; makefile: 21
file content (46 lines) | stat: -rw-r--r-- 1,258 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from __future__ import annotations

from ansible.module_utils.basic import AnsibleModule


class Die(BaseException):
    """Raise an exception that is unlikely to be caught (and will thus catastrophically exit the process)."""


def main():
    module = AnsibleModule(
        argument_spec=dict(
            errors=dict(type='list', elements='str', default=[]),
            trigger_unhandled_exception=dict(type='str', default=''),
            return_from_main=dict(type='bool', default=False),
        ),
        supports_check_mode=True,
        mutually_exclusive=[['errors', 'trigger_unhandled_exception', 'return_from_main']],
    )

    if module.params['return_from_main']:
        return

    if trigger_unhandled_exception := module.params['trigger_unhandled_exception']:
        raise Die(trigger_unhandled_exception)

    if errors := module.params['errors']:
        raise_exceptions(errors)

    result = dict()

    module.exit_json(**result)


def raise_exceptions(exceptions: list[str]) -> None:
    if len(exceptions) > 1:
        try:
            raise_exceptions(exceptions[1:])
        except Exception as ex:
            raise Exception(exceptions[0]) from ex

    raise Exception(exceptions[0])


if __name__ == '__main__':
    main()