| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2025 Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: storagebox_set_password |
| 13 |
short_description: (Re)set the password for a storage box |
| 14 |
version_added: 2.1.0 |
| 15 |
author: |
| 16 |
- Matthias Hurdebise (@matthiashurdebise) |
| 17 |
description: |
| 18 |
- (Re)set the password for a storage box. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.hrobot.api._robot_compat_shim_deprecation # must come before api and robot |
| 21 |
- community.hrobot.api |
| 22 |
- community.hrobot.robot |
| 23 |
- community.hrobot.attributes |
| 24 |
- community.hrobot.attributes._actiongroup_robot_and_api_deprecation # must come before the other two! |
| 25 |
- community.hrobot.attributes.actiongroup_api |
| 26 |
- community.hrobot.attributes.actiongroup_robot |
| 27 |
|
| 28 |
attributes: |
| 29 |
check_mode: |
| 30 |
support: none |
| 31 |
diff_mode: |
| 32 |
support: none |
| 33 |
idempotent: |
| 34 |
support: none |
| 35 |
details: |
| 36 |
- This module performs an action on every invocation. |
| 37 |
|
| 38 |
options: |
| 39 |
hetzner_token: |
| 40 |
version_added: 2.5.0 |
| 41 |
id: |
| 42 |
description: |
| 43 |
- The ID of the storage box to modify. |
| 44 |
type: int |
| 45 |
required: true |
| 46 |
password: |
| 47 |
description: |
| 48 |
- The new password for the storage box. |
| 49 |
- If not provided, a random password will be created by the Robot API |
| 50 |
and returned as RV(password). |
| 51 |
- This option is required if O(hetzner_token) is provided, since the new API does not support setting (and returning) a random password. |
| 52 |
type: str |
| 53 |
""" |
| 54 |
|
| 55 |
EXAMPLES = r""" |
| 56 |
--- |
| 57 |
- name: Set the password |
| 58 |
community.hrobot.storagebox_set_password: |
| 59 |
id: 123 |
| 60 |
password: "newpassword" |
| 61 |
|
| 62 |
- name: Set a random password (only works with the legacy Robot API) |
| 63 |
community.hrobot.storagebox_set_password: |
| 64 |
id: 123 |
| 65 |
register: result |
| 66 |
|
| 67 |
- name: Output new password |
| 68 |
ansible.builtin.debug: |
| 69 |
msg: "New password: {{ result.password }}" |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
password: |
| 74 |
description: |
| 75 |
- The new password for the storage box. |
| 76 |
- Note that if the password has been provided as O(password), Ansible will censor this return value to something |
| 77 |
like C(VALUE_SPECIFIED_IN_NO_LOG_PARAMETER). |
| 78 |
returned: success |
| 79 |
type: str |
| 80 |
sample: "newpassword" |
| 81 |
""" |
| 82 |
|
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
|
| 85 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 86 |
BASE_URL, |
| 87 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 88 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 89 |
fetch_url_json, |
| 90 |
) |
| 91 |
|
| 92 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 93 |
API_BASE_URL, |
| 94 |
API_DEFAULT_ARGUMENT_SPEC, |
| 95 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 96 |
ApplyActionError, |
| 97 |
api_apply_action, |
| 98 |
) |
| 99 |
|
| 100 |
try: |
| 101 |
from urllib.parse import urlencode |
| 102 |
except ImportError: |
| 103 |
# Python 2.x fallback: |
| 104 |
from urllib import urlencode |
| 105 |
|
| 106 |
|
| 107 |
def main(): |
| 108 |
argument_spec = dict( |
| 109 |
id=dict(type="int", required=True), |
| 110 |
password=dict(type="str", no_log=True), |
| 111 |
) |
| 112 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 113 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 114 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 115 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 116 |
module = AnsibleModule( |
| 117 |
argument_spec, |
| 118 |
supports_check_mode=False, |
| 119 |
required_by={"hetzner_token": "password"}, |
| 120 |
) |
| 121 |
|
| 122 |
id = module.params["id"] |
| 123 |
password = module.params.get("password") |
| 124 |
|
| 125 |
if module.params["hetzner_user"] is not None: |
| 126 |
module.deprecate( |
| 127 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 128 |
collection_name="community.hrobot", |
| 129 |
version="3.0.0", |
| 130 |
) |
| 131 |
# DEPRECATED: old API |
| 132 |
url = "{0}/storagebox/{1}/password".format(BASE_URL, id) |
| 133 |
accepted_errors = ["STORAGEBOX_NOT_FOUND", "STORAGEBOX_INVALID_PASSWORD"] |
| 134 |
|
| 135 |
if password: |
| 136 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 137 |
result, error = fetch_url_json( |
| 138 |
module, url, method="POST", accept_errors=accepted_errors, data=urlencode({"password": password}), headers=headers) |
| 139 |
else: |
| 140 |
result, error = fetch_url_json( |
| 141 |
module, url, method="POST", accept_errors=accepted_errors) |
| 142 |
|
| 143 |
if error == 'STORAGEBOX_NOT_FOUND': |
| 144 |
module.fail_json( |
| 145 |
msg='Storage Box with ID {0} not found'.format(id)) |
| 146 |
|
| 147 |
if error == 'STORAGEBOX_INVALID_PASSWORD': |
| 148 |
module.fail_json( |
| 149 |
msg="The chosen password has been considered insecure or does not comply with Hetzner's password guideline") |
| 150 |
|
| 151 |
module.exit_json(changed=True, password=result["password"]) |
| 152 |
|
| 153 |
else: |
| 154 |
# NEW API! |
| 155 |
action_url = "{0}/v1/storage_boxes/{1}/actions/reset_password".format(API_BASE_URL, id) |
| 156 |
action = { |
| 157 |
"password": password, |
| 158 |
} |
| 159 |
try: |
| 160 |
dummy, error = api_apply_action( |
| 161 |
module, |
| 162 |
action_url, |
| 163 |
action, |
| 164 |
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
| 165 |
check_done_delay=1, |
| 166 |
check_done_timeout=60, |
| 167 |
accept_errors=["not_found"], |
| 168 |
) |
| 169 |
except ApplyActionError as exc: |
| 170 |
module.fail_json(msg='Error while resetting password: {0}'.format(exc)) |
| 171 |
|
| 172 |
if error == "not_found": |
| 173 |
module.fail_json(msg='Storage Box with ID {0} not found'.format(id)) |
| 174 |
|
| 175 |
module.exit_json(changed=True, password=password) |
| 176 |
|
| 177 |
|
| 178 |
if __name__ == '__main__': # pragma: no cover |
| 179 |
main() # pragma: no cover |