diff --git a/changelogs/fragments/add-server_types-to-datacenter-info.yaml b/changelogs/fragments/add-server_types-to-datacenter-info.yaml new file mode 100644 index 0000000..5fa3466 --- /dev/null +++ b/changelogs/fragments/add-server_types-to-datacenter-info.yaml @@ -0,0 +1,2 @@ +minor_changes: + - hcloud_datacenter_info - Add `server_types` field diff --git a/plugins/modules/hcloud_datacenter_info.py b/plugins/modules/hcloud_datacenter_info.py index d21cac5..1cfb6e7 100644 --- a/plugins/modules/hcloud_datacenter_info.py +++ b/plugins/modules/hcloud_datacenter_info.py @@ -35,9 +35,27 @@ EXAMPLES = """ - name: Gather hcloud datacenter info hetzner.hcloud.hcloud_datacenter_info: register: output + - name: Print the gathered info debug: var: output + +- name: List available server_types in a datacenter + block: + - name: Gather a hcloud datacenter + hetzner.hcloud.hcloud_datacenter_info: + name: fsn1-dc14 + register: output + + - name: Gather a hcloud datacenter available server_types + hetzner.hcloud.hcloud_server_type_info: + id: "{{ item }}" + loop: "{{ output.hcloud_datacenter_info[0].server_types.available }}" + register: available_server_types + + - name: Print a hcloud datacenter available server_types + ansible.builtin.debug: + var: available_server_types.results | map(attribute='hcloud_server_type_info') """ RETURN = """ @@ -72,6 +90,29 @@ hcloud_datacenter_info: returned: always type: str sample: fsn1 + server_types: + description: The Server types the Datacenter can handle + returned: always + type: dict + contains: + available: + description: IDs of Server types that are supported and for which the Datacenter has enough resources left + returned: always + type: list + elements: int + sample: [1, 2, 3] + available_for_migration: + description: IDs of Server types that are supported and for which the Datacenter has enough resources left + returned: always + type: list + elements: int + sample: [1, 2, 3] + supported: + description: IDs of Server types that are supported in the Datacenter + returned: always + type: list + elements: int + sample: [1, 2, 3] """ from typing import List, Optional @@ -93,15 +134,22 @@ class AnsibleHCloudDatacenterInfo(AnsibleHCloud): tmp = [] for datacenter in self.hcloud_datacenter_info: - if datacenter is not None: - tmp.append( - { - "id": to_native(datacenter.id), - "name": to_native(datacenter.name), - "description": to_native(datacenter.description), - "location": to_native(datacenter.location.name), - } - ) + if datacenter is None: + continue + + tmp.append( + { + "id": to_native(datacenter.id), + "name": to_native(datacenter.name), + "description": to_native(datacenter.description), + "location": to_native(datacenter.location.name), + "server_types": { + "available": [o.id for o in datacenter.server_types.available], + "available_for_migration": [o.id for o in datacenter.server_types.available_for_migration], + "supported": [o.id for o in datacenter.server_types.supported], + }, + } + ) return tmp