Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/cmd_runner/library/cmd_echo.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2022, Alexei Znamensky <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 import traceback
9
10 DOCUMENTATION = ""
11
12 EXAMPLES = ""
13
14 RETURN = ""
15
16 from ansible.module_utils.basic import AnsibleModule
17
18 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner
19 from ansible_collections.community.general.plugins.module_utils._cmd_runner import cmd_runner_fmt as fmt
20
21
22 def main():
23 module = AnsibleModule(
24 argument_spec=dict(
25 cmd=dict(type="str", default="echo"),
26 path_prefix=dict(type="str"),
27 arg_formats=dict(type="dict", default={}),
28 arg_order=dict(type="raw", required=True),
29 arg_values=dict(type="dict", default={}),
30 check_mode_skip=dict(type="bool", default=False),
31 aa=dict(type="raw"),
32 tt=dict(),
33 ),
34 supports_check_mode=True,
35 )
36 p = module.params
37
38 info = None
39
40 arg_formats = {}
41 for arg, fmt_spec in p["arg_formats"].items():
42 func = getattr(fmt, fmt_spec["func"])
43 args = fmt_spec.get("args", [])
44
45 arg_formats[arg] = func(*args)
46
47 try:
48 runner = CmdRunner(
49 module, [module.params["cmd"], "--"], arg_formats=arg_formats, path_prefix=module.params["path_prefix"]
50 )
51
52 with runner.context(p["arg_order"], check_mode_skip=p["check_mode_skip"]) as ctx:
53 result = ctx.run(**p["arg_values"])
54 info = ctx.run_info
55 rc, out, err = result if result is not None else (None, None, None)
56
57 module.exit_json(rc=rc, out=out, err=err, info=info)
58 except Exception:
59 module.fail_json(rc=1, module_stderr=traceback.format_exc(), msg="Module crashed with exception")
60
61
62 if __name__ == "__main__":
63 main()