ansible-collection-hetzner-.../plugins/modules/server_network.py

247 lines
7.3 KiB
Python
Raw Normal View History

2020-03-09 13:36:01 +00:00
#!/usr/bin/python
# Copyright: (c) 2019, Hetzner Cloud GmbH <info@hetzner-cloud.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
DOCUMENTATION = """
2020-03-09 13:36:01 +00:00
---
module: server_network
2020-03-09 13:36:01 +00:00
short_description: Manage the relationship between Hetzner Cloud Networks and servers
description:
- Create and delete the relationship Hetzner Cloud Networks and servers
author:
- Lukas Kaemmerling (@lkaemmerling)
options:
network:
description:
- Name or ID of the Hetzner Cloud Networks.
2020-03-09 13:36:01 +00:00
type: str
required: true
server:
description:
- Name or ID of the Hetzner Cloud server.
2020-03-09 13:36:01 +00:00
type: str
required: true
ip:
description:
- The IP the server should have.
type: str
alias_ips:
description:
- Alias IPs the server has.
type: list
elements: str
2020-03-09 13:36:01 +00:00
state:
description:
- State of the server_network.
default: present
choices: [ absent, present ]
type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud
"""
2020-03-09 13:36:01 +00:00
EXAMPLES = """
- name: Create a basic server network
hetzner.hcloud.server_network:
2020-03-09 13:36:01 +00:00
network: my-network
server: my-server
state: present
- name: Create a server network and specify the ip address
hetzner.hcloud.server_network:
2020-03-09 13:36:01 +00:00
network: my-network
server: my-server
ip: 10.0.0.1
state: present
- name: Create a server network and add alias ips
hetzner.hcloud.server_network:
2020-03-09 13:36:01 +00:00
network: my-network
server: my-server
ip: 10.0.0.1
alias_ips:
deps: update pre-commit hook ansible/ansible-lint to v6.21.0 (#365) [![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [ansible/ansible-lint](https://togithub.com/ansible/ansible-lint) | repository | minor | `v6.20.3` -> `v6.21.0` | Note: The `pre-commit` manager in Renovate is not supported by the `pre-commit` maintainers or community. Please do not report any problems there, instead [create a Discussion in the Renovate repository](https://togithub.com/renovatebot/renovate/discussions/new) if you have any questions. --- ### Release Notes <details> <summary>ansible/ansible-lint (ansible/ansible-lint)</summary> ### [`v6.21.0`](https://togithub.com/ansible/ansible-lint/releases/tag/v6.21.0) [Compare Source](https://togithub.com/ansible/ansible-lint/compare/v6.20.3...v6.21.0) #### Minor Changes - Allow linting plugin EXAMPLES as playbooks ([#&#8203;3309](https://togithub.com/ansible/ansible-lint/issues/3309)) [@&#8203;Qalthos](https://togithub.com/Qalthos) #### Bugfixes - Add support for Rocky ([#&#8203;3843](https://togithub.com/ansible/ansible-lint/issues/3843)) [@&#8203;facorazza](https://togithub.com/facorazza) - Update supported Ubuntu versions in `meta.json` ([#&#8203;3845](https://togithub.com/ansible/ansible-lint/issues/3845)) [@&#8203;mcdonnnj](https://togithub.com/mcdonnnj) - Avoid false positives for handler in roles handlers directory ([#&#8203;3838](https://togithub.com/ansible/ansible-lint/issues/3838)) [@&#8203;ajinkyau](https://togithub.com/ajinkyau) - Hide stacktrace when loading invalid yaml ([#&#8203;3844](https://togithub.com/ansible/ansible-lint/issues/3844)) [@&#8203;ajinkyau](https://togithub.com/ajinkyau) - Add some platforms to `meta.json` ([#&#8203;3841](https://togithub.com/ansible/ansible-lint/issues/3841)) [@&#8203;mcdonnnj](https://togithub.com/mcdonnnj) - Temporary avoid auto-fixing YAML files not owned by ansible ([#&#8203;3837](https://togithub.com/ansible/ansible-lint/issues/3837)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) - Add environment variable for skipping schema update ([#&#8203;3835](https://togithub.com/ansible/ansible-lint/issues/3835)) [@&#8203;ajinkyau](https://togithub.com/ajinkyau) - Avoid creating temporary YAML files inside source tree ([#&#8203;3819](https://togithub.com/ansible/ansible-lint/issues/3819)) [@&#8203;Qalthos](https://togithub.com/Qalthos) - Document environment variables ([#&#8203;3833](https://togithub.com/ansible/ansible-lint/issues/3833)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) - Update schemas ([#&#8203;3832](https://togithub.com/ansible/ansible-lint/issues/3832)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) - Support complex requirements in argument_specs.yml ([#&#8203;3823](https://togithub.com/ansible/ansible-lint/issues/3823)) [@&#8203;tapetersen](https://togithub.com/tapetersen) - Fix SARIF-formatter severity levels ([#&#8203;3824](https://togithub.com/ansible/ansible-lint/issues/3824)) [@&#8203;4ch1m](https://togithub.com/4ch1m) - Add play level autofix for key-order rule ([#&#8203;3815](https://togithub.com/ansible/ansible-lint/issues/3815)) [@&#8203;ajinkyau](https://togithub.com/ajinkyau) - Add support for python 3.12 ([#&#8203;3813](https://togithub.com/ansible/ansible-lint/issues/3813)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) - Update SPDX license list ([#&#8203;3814](https://togithub.com/ansible/ansible-lint/issues/3814)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) - Use checkout action in install docs ([#&#8203;3810](https://togithub.com/ansible/ansible-lint/issues/3810)) [@&#8203;gma](https://togithub.com/gma) - Fix actions-tagger arguments ([#&#8203;3808](https://togithub.com/ansible/ansible-lint/issues/3808)) [@&#8203;ssbarnea](https://togithub.com/ssbarnea) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/ansible-collections/hetzner.hcloud). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xOS4yIiwidXBkYXRlZEluVmVyIjoiMzcuMTkuMiIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
2023-10-19 12:41:44 +02:00
- 10.1.0.1
- 10.2.0.1
2020-03-09 13:36:01 +00:00
state: present
- name: Ensure the server network is absent (remove if needed)
hetzner.hcloud.server_network:
2020-03-09 13:36:01 +00:00
network: my-network
server: my-server
state: absent
"""
RETURN = """
hcloud_server_network:
description: The relationship between a server and a network
returned: always
type: complex
contains:
network:
description: Name of the Network
type: str
returned: always
sample: my-network
server:
description: Name of the server
type: str
returned: always
sample: my-server
ip:
description: IP of the server within the Network ip range
type: str
returned: always
sample: 10.0.0.8
alias_ips:
description: Alias IPs of the server within the Network ip range
type: list
elements: str
2020-03-09 13:36:01 +00:00
returned: always
sample: [10.1.0.1, ...]
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import APIException, HCloudException
from ..module_utils.vendor.hcloud.networks import BoundNetwork
from ..module_utils.vendor.hcloud.servers import BoundServer, PrivateNet
2020-03-09 13:36:01 +00:00
class AnsibleHCloudServerNetwork(AnsibleHCloud):
represent = "hcloud_server_network"
hcloud_network: BoundNetwork | None = None
hcloud_server: BoundServer | None = None
hcloud_server_network: PrivateNet | None = None
2020-03-09 13:36:01 +00:00
def _prepare_result(self):
return {
"network": to_native(self.hcloud_network.name),
"server": to_native(self.hcloud_server.name),
"ip": to_native(self.hcloud_server_network.ip),
"alias_ips": self.hcloud_server_network.alias_ips,
}
def _get_server_and_network(self):
try:
self.hcloud_network = self._client_get_by_name_or_id(
"networks",
self.module.params.get("network"),
)
self.hcloud_server = self._client_get_by_name_or_id(
"servers",
self.module.params.get("server"),
)
2020-03-09 13:36:01 +00:00
self.hcloud_server_network = None
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
def _get_server_network(self):
for private_net in self.hcloud_server.private_net:
if private_net.network.id == self.hcloud_network.id:
self.hcloud_server_network = private_net
2020-03-09 13:36:01 +00:00
def _create_server_network(self):
params = {
"network": self.hcloud_network,
2020-03-09 13:36:01 +00:00
}
if self.module.params.get("ip") is not None:
params["ip"] = self.module.params.get("ip")
if self.module.params.get("alias_ips") is not None:
params["alias_ips"] = self.module.params.get("alias_ips")
if not self.module.check_mode:
try:
self.hcloud_server.attach_to_network(**params).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
self._mark_as_changed()
self._get_server_and_network()
self._get_server_network()
def _update_server_network(self):
params = {
"network": self.hcloud_network,
}
alias_ips = self.module.params.get("alias_ips")
2022-06-13 08:58:32 +02:00
if alias_ips is not None and sorted(self.hcloud_server_network.alias_ips) != sorted(alias_ips):
params["alias_ips"] = alias_ips
if not self.module.check_mode:
try:
self.hcloud_server.change_alias_ips(**params).wait_until_finished()
except APIException as exception:
self.fail_json_hcloud(exception)
self._mark_as_changed()
self._get_server_and_network()
self._get_server_network()
2020-03-09 13:36:01 +00:00
def present_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is None:
self._create_server_network()
else:
self._update_server_network()
2020-03-09 13:36:01 +00:00
def delete_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is not None and self.hcloud_server is not None:
if not self.module.check_mode:
try:
self.hcloud_server.detach_from_network(self.hcloud_server_network.network).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
self._mark_as_changed()
self.hcloud_server_network = None
@classmethod
def define_module(cls):
2020-03-09 13:36:01 +00:00
return AnsibleModule(
argument_spec=dict(
network={"type": "str", "required": True},
server={"type": "str", "required": True},
ip={"type": "str"},
alias_ips={"type": "list", "elements": "str"},
2020-03-09 13:36:01 +00:00
state={
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments(),
2020-03-09 13:36:01 +00:00
),
supports_check_mode=True,
)
def main():
module = AnsibleHCloudServerNetwork.define_module()
2020-03-09 13:36:01 +00:00
hcloud = AnsibleHCloudServerNetwork(module)
2020-03-09 13:36:01 +00:00
state = module.params["state"]
if state == "absent":
hcloud.delete_server_network()
elif state == "present":
hcloud.present_server_network()
module.exit_json(**hcloud.get_result())
if __name__ == "__main__":
main()