Debian Codegraph @

ansible_collections/community/general/plugins/modules/smartos_image_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2015, Adam Števko <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: smartos_image_info
11 short_description: Get SmartOS image details
12 description:
13 - Retrieve information about all installed images on SmartOS.
14 author: Adam Števko (@xen0l)
15 extends_documentation_fragment:
16 - community.general._attributes
17 - community.general._attributes.info_module
18 attributes:
19 check_mode:
20 version_added: 3.3.0
21 # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
22 options:
23 filters:
24 description:
25 - Criteria for selecting image. Can be any value from image manifest and V(published_date), V(published), V(source),
26 V(clones), and V(size).
27 - More information can be found at U(https://smartos.org/man/8/imgadm) under C(imgadm list).
28 type: str
29 seealso:
30 - name: imgadm(8)
31 description: Complete manual page for the command C(imgadm).
32 link: https://smartos.org/man/8/imgadm
33 """
34
35 EXAMPLES = r"""
36 - name: Return information about all installed images
37 community.general.smartos_image_info:
38 register: result
39
40 - name: Return all private active Linux images
41 community.general.smartos_image_info:
42 filters: "os=linux state=active public=false"
43 register: result
44
45 - name: Show, how many clones does every image have
46 community.general.smartos_image_info:
47 register: result
48
49 - name: Print information
50 ansible.builtin.debug:
51 msg: >-
52 {{
53 result.smartos_images[item]['name'] }}-{{ result.smartos_images[item]['version'] }}
54 has {{ result.smartos_images[item]['clones']
55 }} VM(s)
56 with_items: "{{ result.smartos_images.keys() | list }}"
57
58 - name: Print information
59 ansible.builtin.debug:
60 msg: >-
61 {{
62 smartos_images[item]['name'] }}-{{ smartos_images[item]['version'] }}
63 has {{ smartos_images[item]['clones']
64 }} VM(s)
65 with_items: "{{ smartos_images.keys() | list }}"
66 """
67
68 RETURN = r"""
69 """
70
71 import json
72
73 from ansible.module_utils.basic import AnsibleModule
74
75
76 class ImageFacts:
77 def __init__(self, module):
78 self.module = module
79
80 self.filters = module.params["filters"]
81
82 def return_all_installed_images(self):
83 cmd = [self.module.get_bin_path("imgadm"), "list", "-j"]
84
85 if self.filters:
86 cmd.append(self.filters)
87
88 (rc, out, err) = self.module.run_command(cmd)
89
90 if rc != 0:
91 self.module.exit_json(msg="Failed to get all installed images", stderr=err)
92
93 images = json.loads(out)
94
95 result = {}
96 for image in images:
97 result[image["manifest"]["uuid"]] = image["manifest"]
98 # Merge additional attributes with the image manifest.
99 for attrib in ["clones", "source", "zpool"]:
100 result[image["manifest"]["uuid"]][attrib] = image[attrib]
101
102 return result
103
104
105 def main():
106 module = AnsibleModule(
107 argument_spec=dict(
108 filters=dict(),
109 ),
110 supports_check_mode=True,
111 )
112 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
113
114 image_facts = ImageFacts(module)
115
116 data = dict(smartos_images=image_facts.return_all_installed_images())
117
118 module.exit_json(**data)
119
120
121 if __name__ == "__main__":
122 main()