| 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: datacenter_info |
| 12 |
|
| 13 |
short_description: Gather info about the Hetzner Cloud datacenters. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather info about your Hetzner Cloud datacenters. |
| 17 |
|
| 18 |
author: |
| 19 |
- Lukas Kaemmerling (@LKaemmerling) |
| 20 |
|
| 21 |
options: |
| 22 |
id: |
| 23 |
description: |
| 24 |
- The ID of the datacenter you want to get. |
| 25 |
- The module will fail if the provided ID is invalid. |
| 26 |
type: int |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the datacenter you want to get. |
| 30 |
type: str |
| 31 |
extends_documentation_fragment: |
| 32 |
- hetzner.hcloud.hcloud |
| 33 |
|
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = """ |
| 37 |
- name: Gather hcloud datacenter info |
| 38 |
hetzner.hcloud.datacenter_info: |
| 39 |
register: output |
| 40 |
|
| 41 |
- name: Print the gathered info |
| 42 |
debug: |
| 43 |
var: output |
| 44 |
|
| 45 |
- name: List available server_types in a datacenter |
| 46 |
block: |
| 47 |
- name: Gather a hcloud datacenter |
| 48 |
hetzner.hcloud.datacenter_info: |
| 49 |
name: fsn1-dc14 |
| 50 |
register: output |
| 51 |
|
| 52 |
- name: Gather a hcloud datacenter available server_types |
| 53 |
hetzner.hcloud.server_type_info: |
| 54 |
id: "{{ item }}" |
| 55 |
loop: "{{ output.hcloud_datacenter_info[0].server_types.available }}" |
| 56 |
register: available_server_types |
| 57 |
|
| 58 |
- name: Print a hcloud datacenter available server_types |
| 59 |
ansible.builtin.debug: |
| 60 |
var: available_server_types.results | map(attribute='hcloud_server_type_info') |
| 61 |
""" |
| 62 |
|
| 63 |
RETURN = """ |
| 64 |
hcloud_datacenter_info: |
| 65 |
description: |
| 66 |
- The datacenter info as list |
| 67 |
returned: always |
| 68 |
type: list |
| 69 |
contains: |
| 70 |
id: |
| 71 |
description: Numeric identifier of the datacenter |
| 72 |
returned: always |
| 73 |
type: int |
| 74 |
sample: 1937415 |
| 75 |
name: |
| 76 |
description: Name of the datacenter |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: fsn1-dc8 |
| 80 |
description: |
| 81 |
description: Detail description of the datacenter |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: Falkenstein DC 8 |
| 85 |
location: |
| 86 |
description: Name of the location where the datacenter resides in |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: fsn1 |
| 90 |
city: |
| 91 |
description: City of the location |
| 92 |
returned: always |
| 93 |
type: str |
| 94 |
sample: fsn1 |
| 95 |
server_types: |
| 96 |
description: | |
| 97 |
The Server types the Datacenter can handle. |
| 98 |
|
| 99 |
B(Deprecated:) The RV(hcloud_datacenter_info[].server_types) value is deprecated and will be removed |
| 100 |
after 1 October 2026. Please use the RV(hetzner.hcloud.server_type_info#module:hcloud_server_type_info[].locations) value instead. |
| 101 |
See https://docs.hetzner.cloud/changelog#2026-04-01-datacenter-deprecations. |
| 102 |
returned: always |
| 103 |
type: dict |
| 104 |
contains: |
| 105 |
available: |
| 106 |
description: IDs of Server types that are supported and for which the Datacenter has enough resources left |
| 107 |
returned: always |
| 108 |
type: list |
| 109 |
elements: int |
| 110 |
sample: [1, 2, 3] |
| 111 |
available_for_migration: |
| 112 |
description: IDs of Server types that are supported and for which the Datacenter has enough resources left |
| 113 |
returned: always |
| 114 |
type: list |
| 115 |
elements: int |
| 116 |
sample: [1, 2, 3] |
| 117 |
supported: |
| 118 |
description: IDs of Server types that are supported in the Datacenter |
| 119 |
returned: always |
| 120 |
type: list |
| 121 |
elements: int |
| 122 |
sample: [1, 2, 3] |
| 123 |
""" |
| 124 |
|
| 125 |
from ansible.module_utils.basic import AnsibleModule |
| 126 |
|
| 127 |
from ..module_utils._base import AnsibleHCloud |
| 128 |
from ..module_utils._vendor.hcloud import HCloudException |
| 129 |
from ..module_utils._vendor.hcloud.datacenters import BoundDatacenter |
| 130 |
|
| 131 |
|
| 132 |
class AnsibleHCloudDatacenterInfo(AnsibleHCloud): |
| 133 |
represent = "hcloud_datacenter_info" |
| 134 |
|
| 135 |
hcloud_datacenter_info: list[BoundDatacenter] | None = None |
| 136 |
|
| 137 |
def _prepare_result(self): |
| 138 |
tmp = [] |
| 139 |
|
| 140 |
for datacenter in self.hcloud_datacenter_info: |
| 141 |
if datacenter is None: |
| 142 |
continue |
| 143 |
|
| 144 |
result = { |
| 145 |
"id": datacenter.id, |
| 146 |
"name": datacenter.name, |
| 147 |
"description": datacenter.description, |
| 148 |
"location": datacenter.location.name, |
| 149 |
} |
| 150 |
if datacenter.server_types is not None: |
| 151 |
result["server_types"] = { |
| 152 |
"available": [o.id for o in datacenter.server_types.available], |
| 153 |
"available_for_migration": [o.id for o in datacenter.server_types.available_for_migration], |
| 154 |
"supported": [o.id for o in datacenter.server_types.supported], |
| 155 |
} |
| 156 |
tmp.append(result) |
| 157 |
|
| 158 |
return tmp |
| 159 |
|
| 160 |
def get_datacenters(self): |
| 161 |
try: |
| 162 |
if self.module.params.get("id") is not None: |
| 163 |
self.hcloud_datacenter_info = [self.client.datacenters.get_by_id(self.module.params.get("id"))] |
| 164 |
elif self.module.params.get("name") is not None: |
| 165 |
self.hcloud_datacenter_info = [self.client.datacenters.get_by_name(self.module.params.get("name"))] |
| 166 |
else: |
| 167 |
self.hcloud_datacenter_info = self.client.datacenters.get_all() |
| 168 |
|
| 169 |
except HCloudException as exception: |
| 170 |
self.fail_json_hcloud(exception) |
| 171 |
|
| 172 |
@classmethod |
| 173 |
def define_module(cls): |
| 174 |
return AnsibleModule( |
| 175 |
argument_spec=dict( |
| 176 |
id={"type": "int"}, |
| 177 |
name={"type": "str"}, |
| 178 |
**super().base_module_arguments(), |
| 179 |
), |
| 180 |
supports_check_mode=True, |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
def main(): |
| 185 |
module = AnsibleHCloudDatacenterInfo.define_module() |
| 186 |
hcloud = AnsibleHCloudDatacenterInfo(module) |
| 187 |
|
| 188 |
hcloud.get_datacenters() |
| 189 |
result = hcloud.get_result() |
| 190 |
|
| 191 |
ansible_info = {"hcloud_datacenter_info": result["hcloud_datacenter_info"]} |
| 192 |
module.exit_json(**ansible_info) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
main() |