Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_containers.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2020 Red Hat
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9 DOCUMENTATION = """
10 ---
11 module: podman_containers
12 author:
13 - "Sagi Shnaidman (@sshnaidm)"
14 version_added: '1.4.0'
15 short_description: Manage podman containers in a batch
16 description:
17 - Manage groups of podman containers
18 requirements:
19 - "podman"
20 options:
21 containers:
22 description:
23 - List of dictionaries with data for running containers for podman_container module.
24 required: True
25 type: list
26 elements: dict
27 debug:
28 description:
29 - Return additional information which can be helpful for investigations.
30 type: bool
31 default: False
32 """
33
34 EXAMPLES = """
35 - name: Run three containers at once
36 podman_containers:
37 containers:
38 - name: alpine
39 image: alpine
40 command: sleep 1d
41 - name: web
42 image: nginx
43 - name: test
44 image: python:3.10-alpine
45 command: python -V
46 """
47
48 from ansible.module_utils.basic import AnsibleModule # noqa: F402
49 from ..module_utils.podman.podman_container_lib import PodmanManager # noqa: F402
50 from ..module_utils.podman.podman_container_lib import set_container_opts # noqa: F402
51
52
53 def combine(results):
54 changed = any(i.get("changed", False) for i in results)
55 failed = any(i.get("failed", False) for i in results)
56 actions = []
57 podman_actions = []
58 containers = []
59 podman_version = ""
60 diffs = {}
61 stderr = ""
62 stdout = ""
63 for i in results:
64 if "actions" in i and i["actions"]:
65 actions += i["actions"]
66 if "podman_actions" in i and i["podman_actions"]:
67 podman_actions += i["podman_actions"]
68 if "container" in i and i["container"]:
69 containers.append(i["container"])
70 if "podman_version" in i:
71 podman_version = i["podman_version"]
72 if "diff" in i:
73 diffs[i["container"]["Name"]] = i["diff"]
74 if "stderr" in i:
75 stderr += i["stderr"]
76 if "stdout" in i:
77 stdout += i["stdout"]
78
79 total = {
80 "changed": changed,
81 "failed": failed,
82 "actions": actions,
83 "podman_actions": podman_actions,
84 "containers": containers,
85 "stdout": stdout,
86 "stderr": stderr,
87 }
88 if podman_version:
89 total["podman_version"] = podman_version
90 if diffs:
91 before = after = ""
92 for k, v in diffs.items():
93 before += "".join([str(k), ": ", str(v["before"]), "\n"])
94 after += "".join([str(k), ": ", str(v["after"]), "\n"])
95 total["diff"] = {"before": before, "after": after}
96 return total
97
98
99 def check_input_strict(container):
100 if container["state"] in ["started", "present"] and not container["image"]:
101 return "State '%s' required image to be configured!" % container["state"]
102
103
104 def main():
105 module = AnsibleModule(
106 argument_spec=dict(
107 containers=dict(type="list", elements="dict", required=True),
108 debug=dict(type="bool", default=False),
109 ),
110 supports_check_mode=True,
111 )
112 # work on input vars
113
114 results = []
115 for container in module.params["containers"]:
116 options_dict = set_container_opts(container)
117 options_dict["debug"] = module.params["debug"] or options_dict["debug"]
118 test_input = check_input_strict(options_dict)
119 if test_input:
120 module.fail_json(msg="Failed to run container %s because: %s" % (options_dict["name"], test_input))
121 res = PodmanManager(module, options_dict).execute()
122 results.append(res)
123 total_results = combine(results)
124 module.exit_json(**total_results)
125
126
127 if __name__ == "__main__":
128 main()