2022-05-12 03:56:52 +00:00
|
|
|
import json
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
from ansible.module_utils import basic
|
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleExitJson(Exception):
|
|
|
|
def __init__(self, result: Union[dict, list], *args):
|
|
|
|
self.result = result
|
|
|
|
super().__init__(*args)
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleFailJson(Exception):
|
|
|
|
def __init__(self, result: Union[dict, list], *args):
|
|
|
|
self.result = result
|
|
|
|
super().__init__(*args)
|
|
|
|
|
|
|
|
|
2023-01-31 09:19:06 +00:00
|
|
|
def set_module_args(args, check_mode=False, diff=False):
|
2022-08-24 11:54:46 +00:00
|
|
|
if "_ansible_remote_tmp" not in args:
|
|
|
|
args["_ansible_remote_tmp"] = "/tmp"
|
|
|
|
if "_ansible_keep_remote_files" not in args:
|
|
|
|
args["_ansible_keep_remote_files"] = False
|
2023-01-31 09:19:06 +00:00
|
|
|
if check_mode:
|
|
|
|
args["_ansible_check_mode"] = check_mode
|
|
|
|
if diff:
|
|
|
|
args["_ansible_diff"] = diff
|
2022-05-12 03:56:52 +00:00
|
|
|
|
2022-08-24 11:54:46 +00:00
|
|
|
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
|
2022-05-12 03:56:52 +00:00
|
|
|
basic._ANSIBLE_ARGS = to_bytes(args)
|
|
|
|
|
|
|
|
|
|
|
|
def exit_json(*args, **kwargs):
|
|
|
|
"""function to patch over exit_json; package return data into an exception"""
|
2022-08-24 11:54:46 +00:00
|
|
|
if "changed" not in kwargs:
|
|
|
|
kwargs["changed"] = False
|
2022-05-12 03:56:52 +00:00
|
|
|
raise AnsibleExitJson(kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def fail_json(*args, **kwargs):
|
|
|
|
"""function to patch over fail_json; package return data into an exception"""
|
2022-08-24 11:54:46 +00:00
|
|
|
kwargs["failed"] = True
|
2022-05-12 03:56:52 +00:00
|
|
|
raise AnsibleFailJson(kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def assert_expression(expression):
|
|
|
|
if not expression:
|
|
|
|
raise AssertionError()
|