Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_volume_info.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
10 DOCUMENTATION = r"""
11 module: podman_volume_info
12 author:
13 - "Sagi Shnaidman (@sshnaidm)"
14 short_description: Gather info about podman volumes
15 notes: []
16 description:
17 - Gather info about podman volumes with podman inspect command.
18 requirements:
19 - "Podman installed on host"
20 options:
21 name:
22 description:
23 - Name of the volume
24 type: str
25 executable:
26 description:
27 - Path to C(podman) executable if it is not in the C($PATH) on the
28 machine running C(podman)
29 default: 'podman'
30 type: str
31 """
32
33 EXAMPLES = r"""
34 - name: Gather info about all present volumes
35 podman_volume_info:
36
37 - name: Gather info about specific volume
38 podman_volume_info:
39 name: specific_volume
40 """
41
42 RETURN = r"""
43 volumes:
44 description: Facts from all or specified volumes
45 returned: always
46 type: list
47 sample: [
48 {
49 "Name": "postgres9",
50 "Driver": "local",
51 "Mountpoint": "/home/user/.local/share/containers/storage/volumes/postgres9/_data",
52 "CreatedAt": "2023-11-30T16:41:31.310865559+02:00",
53 "Labels": {},
54 "Scope": "local",
55 "Options": {},
56 "MountCount": 0,
57 "NeedsCopyUp": true,
58 "NeedsChown": true,
59 "LockNumber": 18
60 }
61 ]
62 """
63
64 import json
65 from ansible.module_utils.basic import AnsibleModule
66
67
68 def get_volume_info(module, executable, name):
69 command = [executable, "volume", "inspect"]
70 if name:
71 command.append(name)
72 else:
73 command.append("--all")
74 rc, out, err = module.run_command(command)
75 if rc != 0 or "no such volume" in err:
76 module.fail_json(msg="Unable to gather info for %s: %s" % (name or "all volumes", err))
77 if not out or json.loads(out) is None:
78 return [], out, err
79 return json.loads(out), out, err
80
81
82 def main():
83 module = AnsibleModule(
84 argument_spec=dict(executable=dict(type="str", default="podman"), name=dict(type="str")),
85 supports_check_mode=True,
86 )
87
88 name = module.params["name"]
89 executable = module.get_bin_path(module.params["executable"], required=True)
90
91 inspect_results, out, err = get_volume_info(module, executable, name)
92
93 results = {"changed": False, "volumes": inspect_results, "stderr": err}
94
95 module.exit_json(**results)
96
97
98 if __name__ == "__main__":
99 main()