| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2025, 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: volume_attachment |
| 12 |
|
| 13 |
short_description: Manage the attachment of Hetzner Cloud Volumes |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Attach and detach Volumes from Hetzner Cloud Servers. |
| 18 |
|
| 19 |
author: |
| 20 |
- Amirhossein Shaerpour (@shaerpour) |
| 21 |
|
| 22 |
options: |
| 23 |
volume: |
| 24 |
description: |
| 25 |
- Name or ID of the Hetzner Cloud Volume to attach/detach. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
server: |
| 29 |
description: |
| 30 |
- Name or ID of the Hetzner Cloud Server to attach the Volume to. |
| 31 |
- Required if O(state=present). |
| 32 |
type: str |
| 33 |
automount: |
| 34 |
description: |
| 35 |
- Automatically mount the Volume in the Server. |
| 36 |
type: bool |
| 37 |
state: |
| 38 |
description: |
| 39 |
- State of the Volume. |
| 40 |
type: str |
| 41 |
default: present |
| 42 |
choices: [ present, absent ] |
| 43 |
|
| 44 |
extends_documentation_fragment: |
| 45 |
- hetzner.hcloud.hcloud |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = """ |
| 49 |
- name: Attach my-volume to my-server |
| 50 |
hetzner.hcloud.volume_attachment: |
| 51 |
volume: my-volume |
| 52 |
server: my-server |
| 53 |
|
| 54 |
- name: Detach my-volume from my-server |
| 55 |
hetzner.hcloud.volume_attachment: |
| 56 |
volume: my-volume |
| 57 |
state: absent |
| 58 |
|
| 59 |
- name: Attach my-volume using id to my-server with automount enabled |
| 60 |
hetzner.hcloud.volume_attachment: |
| 61 |
volume: 123456 |
| 62 |
server: my-server |
| 63 |
automount: true |
| 64 |
state: present |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = """ |
| 68 |
hcloud_volume_attachment: |
| 69 |
description: The relationship between a Server and a Volume |
| 70 |
returned: always |
| 71 |
type: dict |
| 72 |
contains: |
| 73 |
volume: |
| 74 |
description: Name of the Volume |
| 75 |
type: str |
| 76 |
returned: always |
| 77 |
sample: my-volume |
| 78 |
server: |
| 79 |
description: Name of the attached Server |
| 80 |
type: str |
| 81 |
returned: always |
| 82 |
sample: my-server |
| 83 |
""" |
| 84 |
|
| 85 |
from ansible.module_utils.basic import AnsibleModule |
| 86 |
|
| 87 |
from ..module_utils._base import AnsibleHCloud |
| 88 |
from ..module_utils._vendor.hcloud import HCloudException |
| 89 |
from ..module_utils._vendor.hcloud.servers import BoundServer |
| 90 |
from ..module_utils._vendor.hcloud.volumes import BoundVolume |
| 91 |
|
| 92 |
|
| 93 |
class AnsibleHCloudVolumeAttachment(AnsibleHCloud): |
| 94 |
represent = "hcloud_volume_attachment" |
| 95 |
|
| 96 |
# We must the hcloud_volume_attachment name instead of hcloud_volume, because |
| 97 |
# AnsibleHCloud.get_result does funny things. |
| 98 |
hcloud_volume_attachment: BoundVolume | None = None |
| 99 |
hcloud_server: BoundServer | None = None |
| 100 |
|
| 101 |
def _prepare_result(self): |
| 102 |
return { |
| 103 |
"volume": self.hcloud_volume_attachment.name, |
| 104 |
"server": ( |
| 105 |
self.hcloud_volume_attachment.server.name if self.hcloud_volume_attachment.server is not None else None |
| 106 |
), |
| 107 |
} |
| 108 |
|
| 109 |
def _get_volume(self): |
| 110 |
try: |
| 111 |
self.hcloud_volume_attachment = self._client_get_by_name_or_id( |
| 112 |
"volumes", |
| 113 |
self.module.params.get("volume"), |
| 114 |
) |
| 115 |
except HCloudException as exception: |
| 116 |
self.fail_json_hcloud(exception) |
| 117 |
|
| 118 |
def _get_server(self): |
| 119 |
try: |
| 120 |
self.hcloud_server = self._client_get_by_name_or_id( |
| 121 |
"servers", |
| 122 |
self.module.params.get("server"), |
| 123 |
) |
| 124 |
except HCloudException as exception: |
| 125 |
self.fail_json_hcloud(exception) |
| 126 |
|
| 127 |
def attach_volume(self): |
| 128 |
self.module.fail_on_missing_params(required_params=["server"]) |
| 129 |
|
| 130 |
try: |
| 131 |
self._get_volume() |
| 132 |
self._get_server() |
| 133 |
|
| 134 |
if self.hcloud_volume_attachment.server is not None: |
| 135 |
if self.hcloud_volume_attachment.server.id == self.hcloud_server.id: |
| 136 |
return |
| 137 |
|
| 138 |
if not self.module.check_mode: |
| 139 |
action = self.hcloud_volume_attachment.detach() |
| 140 |
action.wait_until_finished() |
| 141 |
|
| 142 |
self.hcloud_volume_attachment.server = None |
| 143 |
self._mark_as_changed() |
| 144 |
|
| 145 |
if not self.module.check_mode: |
| 146 |
action = self.hcloud_volume_attachment.attach( |
| 147 |
server=self.hcloud_server, |
| 148 |
automount=self.module.params.get("automount"), |
| 149 |
) |
| 150 |
action.wait_until_finished() |
| 151 |
|
| 152 |
self.hcloud_volume_attachment.server = self.hcloud_server |
| 153 |
self._mark_as_changed() |
| 154 |
|
| 155 |
except HCloudException as exception: |
| 156 |
self.fail_json_hcloud(exception) |
| 157 |
|
| 158 |
def detach_volume(self): |
| 159 |
try: |
| 160 |
self._get_volume() |
| 161 |
|
| 162 |
if self.hcloud_volume_attachment.server is not None: |
| 163 |
if not self.module.check_mode: |
| 164 |
action = self.hcloud_volume_attachment.detach() |
| 165 |
action.wait_until_finished() |
| 166 |
|
| 167 |
self.hcloud_volume_attachment.server = None |
| 168 |
self._mark_as_changed() |
| 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 |
volume={"type": "str", "required": True}, |
| 177 |
server={"type": "str"}, |
| 178 |
automount={"type": "bool"}, |
| 179 |
state={ |
| 180 |
"choices": ["present", "absent"], |
| 181 |
"default": "present", |
| 182 |
}, |
| 183 |
**super().base_module_arguments(), |
| 184 |
), |
| 185 |
supports_check_mode=True, |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
def main(): |
| 190 |
module = AnsibleHCloudVolumeAttachment.define_module() |
| 191 |
|
| 192 |
hcloud = AnsibleHCloudVolumeAttachment(module) |
| 193 |
state = module.params["state"] |
| 194 |
if state == "present": |
| 195 |
hcloud.attach_volume() |
| 196 |
elif state == "absent": |
| 197 |
hcloud.detach_volume() |
| 198 |
|
| 199 |
module.exit_json(**hcloud.get_result()) |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
main() |