| 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: primary_ip_info |
| 12 |
|
| 13 |
short_description: Gather infos about the Hetzner Cloud Primary IPs. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather facts about your Hetzner Cloud Primary IPs. |
| 17 |
|
| 18 |
author: |
| 19 |
- Lukas Kaemmerling (@LKaemmerling) |
| 20 |
- Kevin Castner (@kcastner) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- The ID of the Primary IP 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 for the Primary IP you want to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- The label selector for the Primary IP you want to get. |
| 35 |
type: str |
| 36 |
extends_documentation_fragment: |
| 37 |
- hetzner.hcloud.hcloud |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Gather hcloud Primary IP infos |
| 43 |
hetzner.hcloud.primary_ip_info: |
| 44 |
register: output |
| 45 |
|
| 46 |
- name: Gather hcloud Primary IP infos by id |
| 47 |
hetzner.hcloud.primary_ip_info: |
| 48 |
id: 673954 |
| 49 |
register: output |
| 50 |
|
| 51 |
- name: Gather hcloud Primary IP infos by name |
| 52 |
hetzner.hcloud.primary_ip_info: |
| 53 |
name: srv1-v4 |
| 54 |
register: output |
| 55 |
|
| 56 |
- name: Gather hcloud Primary IP infos by label |
| 57 |
hetzner.hcloud.primary_ip_info: |
| 58 |
label_selector: srv03-ips |
| 59 |
register: output |
| 60 |
|
| 61 |
- name: Print the gathered infos |
| 62 |
debug: |
| 63 |
var: output |
| 64 |
""" |
| 65 |
|
| 66 |
RETURN = """ |
| 67 |
hcloud_primary_ip_info: |
| 68 |
description: The Primary IP infos as list |
| 69 |
returned: always |
| 70 |
type: list |
| 71 |
contains: |
| 72 |
id: |
| 73 |
description: Numeric identifier of the Primary IP |
| 74 |
returned: always |
| 75 |
type: int |
| 76 |
sample: 1937415 |
| 77 |
name: |
| 78 |
description: Name of the Primary IP |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: my-primary-ip |
| 82 |
ip: |
| 83 |
description: IP address of the Primary IP |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: 131.232.99.1 |
| 87 |
type: |
| 88 |
description: Type of the Primary IP |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: ipv4 |
| 92 |
assignee_id: |
| 93 |
description: ID of the resource where the Primary IP is assigned to, none if unassigned. |
| 94 |
returned: always |
| 95 |
type: int |
| 96 |
sample: 19584637 |
| 97 |
assignee_type: |
| 98 |
description: Type of the resource where the Primary IP is assigned to. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: server |
| 102 |
location: |
| 103 |
description: Location where the Primary IP was created in. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: fsn1 |
| 107 |
home_location: |
| 108 |
description: | |
| 109 |
Datacenter where the Primary IP was created in. |
| 110 |
|
| 111 |
B(Deprecated:) The RV(hcloud_primary_ip_info[].home_location) value is deprecated and will be removed |
| 112 |
after 1 July 2026. Please use the RV(hcloud_primary_ip_info[].location) value instead. |
| 113 |
See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: fsn1-dc1 |
| 117 |
dns_ptr: |
| 118 |
description: Shows the DNS PTR Record for Primary IP. |
| 119 |
returned: always |
| 120 |
type: str |
| 121 |
sample: srv01.example.com |
| 122 |
labels: |
| 123 |
description: User-defined labels (key-value pairs) |
| 124 |
returned: always |
| 125 |
type: dict |
| 126 |
delete_protection: |
| 127 |
description: True if the Primary IP is protected for deletion |
| 128 |
returned: always |
| 129 |
type: bool |
| 130 |
auto_delete: |
| 131 |
description: Delete the Primary IP when the resource it is assigned to is deleted. |
| 132 |
type: bool |
| 133 |
returned: always |
| 134 |
sample: false |
| 135 |
""" |
| 136 |
|
| 137 |
from ansible.module_utils.basic import AnsibleModule |
| 138 |
|
| 139 |
from ..module_utils._base import AnsibleHCloud |
| 140 |
from ..module_utils._vendor.hcloud import HCloudException |
| 141 |
from ..module_utils._vendor.hcloud.primary_ips import BoundPrimaryIP |
| 142 |
|
| 143 |
|
| 144 |
class AnsibleHCloudPrimaryIPInfo(AnsibleHCloud): |
| 145 |
represent = "hcloud_primary_ip_info" |
| 146 |
|
| 147 |
hcloud_primary_ip_info: list[BoundPrimaryIP] | None = None |
| 148 |
|
| 149 |
def _prepare_result(self): |
| 150 |
tmp = [] |
| 151 |
|
| 152 |
for primary_ip in self.hcloud_primary_ip_info: |
| 153 |
if primary_ip is None: |
| 154 |
continue |
| 155 |
|
| 156 |
tmp.append( |
| 157 |
{ |
| 158 |
"id": primary_ip.id, |
| 159 |
"name": primary_ip.name, |
| 160 |
"ip": primary_ip.ip, |
| 161 |
"type": primary_ip.type, |
| 162 |
"assignee_id": primary_ip.assignee_id if primary_ip.assignee_id is not None else None, |
| 163 |
"assignee_type": primary_ip.assignee_type, |
| 164 |
"auto_delete": primary_ip.auto_delete, |
| 165 |
"location": primary_ip.location.name, |
| 166 |
"home_location": primary_ip.datacenter and primary_ip.datacenter.name, |
| 167 |
"dns_ptr": primary_ip.dns_ptr[0]["dns_ptr"] if len(primary_ip.dns_ptr) else None, |
| 168 |
"labels": primary_ip.labels, |
| 169 |
"delete_protection": primary_ip.protection["delete"], |
| 170 |
} |
| 171 |
) |
| 172 |
|
| 173 |
return tmp |
| 174 |
|
| 175 |
def get_primary_ips(self): |
| 176 |
try: |
| 177 |
if self.module.params.get("id") is not None: |
| 178 |
self.hcloud_primary_ip_info = [self.client.primary_ips.get_by_id(self.module.params.get("id"))] |
| 179 |
elif self.module.params.get("name") is not None: |
| 180 |
self.hcloud_primary_ip_info = [self.client.primary_ips.get_by_name(self.module.params.get("name"))] |
| 181 |
elif self.module.params.get("label_selector") is not None: |
| 182 |
self.hcloud_primary_ip_info = self.client.primary_ips.get_all( |
| 183 |
label_selector=self.module.params.get("label_selector") |
| 184 |
) |
| 185 |
else: |
| 186 |
self.hcloud_primary_ip_info = self.client.primary_ips.get_all() |
| 187 |
|
| 188 |
except HCloudException as exception: |
| 189 |
self.fail_json_hcloud(exception) |
| 190 |
|
| 191 |
@classmethod |
| 192 |
def define_module(cls): |
| 193 |
return AnsibleModule( |
| 194 |
argument_spec=dict( |
| 195 |
id={"type": "int"}, |
| 196 |
label_selector={"type": "str"}, |
| 197 |
name={"type": "str"}, |
| 198 |
**super().base_module_arguments(), |
| 199 |
), |
| 200 |
supports_check_mode=True, |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
def main(): |
| 205 |
module = AnsibleHCloudPrimaryIPInfo.define_module() |
| 206 |
hcloud = AnsibleHCloudPrimaryIPInfo(module) |
| 207 |
|
| 208 |
hcloud.get_primary_ips() |
| 209 |
result = hcloud.get_result() |
| 210 |
|
| 211 |
ansible_info = {"hcloud_primary_ip_info": result["hcloud_primary_ip_info"]} |
| 212 |
module.exit_json(**ansible_info) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
main() |