| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: image_info |
| 12 |
|
| 13 |
short_description: Gather infos about your Hetzner Cloud images. |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Gather infos about your Hetzner Cloud images. |
| 18 |
|
| 19 |
author: |
| 20 |
- Lukas Kaemmerling (@LKaemmerling) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- The ID of the image you want to get. |
| 26 |
- The module will fail if the provided ID is invalid. |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the image you want to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- The label selector for the images you want to get. |
| 35 |
type: str |
| 36 |
type: |
| 37 |
description: |
| 38 |
- The type for the images you want to get. |
| 39 |
default: system |
| 40 |
choices: [ system, snapshot, backup ] |
| 41 |
type: str |
| 42 |
architecture: |
| 43 |
description: |
| 44 |
- The architecture for the images you want to get. |
| 45 |
type: str |
| 46 |
choices: [ x86, arm ] |
| 47 |
extends_documentation_fragment: |
| 48 |
- hetzner.hcloud.hcloud |
| 49 |
|
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = """ |
| 53 |
- name: Gather hcloud image infos |
| 54 |
hetzner.hcloud.image_info: |
| 55 |
register: output |
| 56 |
|
| 57 |
- name: Print the gathered infos |
| 58 |
debug: |
| 59 |
var: output |
| 60 |
""" |
| 61 |
|
| 62 |
RETURN = """ |
| 63 |
hcloud_image_info: |
| 64 |
description: The image infos as list |
| 65 |
returned: always |
| 66 |
type: list |
| 67 |
contains: |
| 68 |
id: |
| 69 |
description: Numeric identifier of the image |
| 70 |
returned: always |
| 71 |
type: int |
| 72 |
sample: 1937415 |
| 73 |
type: |
| 74 |
description: Type of the image |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: system |
| 78 |
status: |
| 79 |
description: Status of the image |
| 80 |
returned: always |
| 81 |
type: str |
| 82 |
sample: available |
| 83 |
name: |
| 84 |
description: Name of the image |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: ubuntu-22.04 |
| 88 |
description: |
| 89 |
description: Detail description of the image |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: Ubuntu 18.04 Standard 64 bit |
| 93 |
os_flavor: |
| 94 |
description: OS flavor of the image |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: ubuntu |
| 98 |
os_version: |
| 99 |
description: OS version of the image |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: 18.04 |
| 103 |
architecture: |
| 104 |
description: Image is compatible with this architecture |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: x86 |
| 108 |
labels: |
| 109 |
description: User-defined labels (key-value pairs) |
| 110 |
returned: always |
| 111 |
type: dict |
| 112 |
""" |
| 113 |
|
| 114 |
from ansible.module_utils.basic import AnsibleModule |
| 115 |
|
| 116 |
from ..module_utils._base import AnsibleHCloud |
| 117 |
from ..module_utils._vendor.hcloud import HCloudException |
| 118 |
from ..module_utils._vendor.hcloud.images import BoundImage |
| 119 |
|
| 120 |
|
| 121 |
class AnsibleHCloudImageInfo(AnsibleHCloud): |
| 122 |
represent = "hcloud_image_info" |
| 123 |
|
| 124 |
hcloud_image_info: list[BoundImage] | None = None |
| 125 |
|
| 126 |
def _prepare_result(self): |
| 127 |
tmp = [] |
| 128 |
|
| 129 |
for image in self.hcloud_image_info: |
| 130 |
if image is None: |
| 131 |
continue |
| 132 |
|
| 133 |
tmp.append( |
| 134 |
{ |
| 135 |
"id": image.id, |
| 136 |
"status": image.status, |
| 137 |
"type": image.type, |
| 138 |
"name": image.name, |
| 139 |
"description": image.description, |
| 140 |
"os_flavor": image.os_flavor, |
| 141 |
"os_version": image.os_version, |
| 142 |
"architecture": image.architecture, |
| 143 |
"labels": image.labels, |
| 144 |
} |
| 145 |
) |
| 146 |
return tmp |
| 147 |
|
| 148 |
def get_images(self): |
| 149 |
try: |
| 150 |
if self.module.params.get("id") is not None: |
| 151 |
self.hcloud_image_info = [self.client.images.get_by_id(self.module.params.get("id"))] |
| 152 |
elif self.module.params.get("name") is not None and self.module.params.get("architecture") is not None: |
| 153 |
self.hcloud_image_info = [ |
| 154 |
self.client.images.get_by_name_and_architecture( |
| 155 |
self.module.params.get("name"), |
| 156 |
self.module.params.get("architecture"), |
| 157 |
) |
| 158 |
] |
| 159 |
elif self.module.params.get("name") is not None: |
| 160 |
self.module.warn( |
| 161 |
"This module only returns x86 images by default. Please set architecture:x86|arm to hide this message." |
| 162 |
) |
| 163 |
self.hcloud_image_info = [self.client.images.get_by_name(self.module.params.get("name"))] |
| 164 |
else: |
| 165 |
params = {} |
| 166 |
label_selector = self.module.params.get("label_selector") |
| 167 |
if label_selector: |
| 168 |
params["label_selector"] = label_selector |
| 169 |
|
| 170 |
image_type = self.module.params.get("type") |
| 171 |
if image_type: |
| 172 |
params["type"] = image_type |
| 173 |
|
| 174 |
architecture = self.module.params.get("architecture") |
| 175 |
if architecture: |
| 176 |
params["architecture"] = architecture |
| 177 |
|
| 178 |
self.hcloud_image_info = self.client.images.get_all(**params) |
| 179 |
|
| 180 |
except HCloudException as exception: |
| 181 |
self.fail_json_hcloud(exception) |
| 182 |
|
| 183 |
@classmethod |
| 184 |
def define_module(cls): |
| 185 |
return AnsibleModule( |
| 186 |
argument_spec=dict( |
| 187 |
id={"type": "int"}, |
| 188 |
name={"type": "str"}, |
| 189 |
label_selector={"type": "str"}, |
| 190 |
type={"choices": ["system", "snapshot", "backup"], "default": "system", "type": "str"}, |
| 191 |
architecture={"choices": ["x86", "arm"], "type": "str"}, |
| 192 |
**super().base_module_arguments(), |
| 193 |
), |
| 194 |
supports_check_mode=True, |
| 195 |
) |
| 196 |
|
| 197 |
|
| 198 |
def main(): |
| 199 |
module = AnsibleHCloudImageInfo.define_module() |
| 200 |
hcloud = AnsibleHCloudImageInfo(module) |
| 201 |
|
| 202 |
hcloud.get_images() |
| 203 |
result = hcloud.get_result() |
| 204 |
|
| 205 |
ansible_info = {"hcloud_image_info": result["hcloud_image_info"]} |
| 206 |
module.exit_json(**ansible_info) |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |