| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2019 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 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: reset_info |
| 14 |
short_description: Query information on the resetter of a dedicated server |
| 15 |
version_added: 2.2.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Query information on the resetter of a dedicated server. |
| 20 |
seealso: |
| 21 |
- module: community.hrobot.reset |
| 22 |
description: Reset dedicated server. |
| 23 |
extends_documentation_fragment: |
| 24 |
- community.hrobot.robot |
| 25 |
- community.hrobot.attributes |
| 26 |
- community.hrobot.attributes.actiongroup_robot |
| 27 |
- community.hrobot.attributes.idempotent_not_modify_state |
| 28 |
- community.hrobot.attributes.info_module |
| 29 |
|
| 30 |
options: |
| 31 |
server_number: |
| 32 |
description: |
| 33 |
- The server number of the server to query its resetter. |
| 34 |
type: int |
| 35 |
required: true |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
--- |
| 40 |
- name: Query resetter information for server 1234 |
| 41 |
community.hrobot.reset_info: |
| 42 |
hetzner_user: foo |
| 43 |
hetzner_password: bar |
| 44 |
server_number: 1234 |
| 45 |
register: result |
| 46 |
|
| 47 |
- name: Show reset methods |
| 48 |
ansible.builtin.debug: |
| 49 |
msg: "{{ result.reset.type }}" |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = r""" |
| 53 |
reset: |
| 54 |
description: |
| 55 |
- Information on the server's resetter. |
| 56 |
type: dict |
| 57 |
returned: success |
| 58 |
contains: |
| 59 |
server_ip: |
| 60 |
description: |
| 61 |
- The primary IPv4 address of the server. |
| 62 |
type: str |
| 63 |
returned: success |
| 64 |
sample: 123.123.123.123 |
| 65 |
server_ipv6_net: |
| 66 |
description: |
| 67 |
- The primary IPv6 network of the server. |
| 68 |
type: str |
| 69 |
returned: success |
| 70 |
sample: "2a01:4f8:111:4221::" |
| 71 |
server_number: |
| 72 |
description: |
| 73 |
- The server's ID. |
| 74 |
type: int |
| 75 |
returned: success |
| 76 |
sample: 321 |
| 77 |
type: |
| 78 |
description: |
| 79 |
- The reset types supported by the resetter. |
| 80 |
- "Can be used for the O(community.hrobot.reset#module:reset_type) option of the M(community.hrobot.reset) module." |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
returned: success |
| 84 |
sample: [software, hardware, manual] |
| 85 |
choices: |
| 86 |
- software |
| 87 |
- hardware |
| 88 |
- power |
| 89 |
- manual |
| 90 |
operating_status: |
| 91 |
description: |
| 92 |
- The server's operating status. |
| 93 |
type: str |
| 94 |
returned: success |
| 95 |
sample: not supported |
| 96 |
""" |
| 97 |
|
| 98 |
from ansible.module_utils.basic import AnsibleModule |
| 99 |
|
| 100 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 101 |
BASE_URL, |
| 102 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 103 |
fetch_url_json, |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def main(): |
| 108 |
argument_spec = dict( |
| 109 |
server_number=dict(type='int', required=True), |
| 110 |
) |
| 111 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 112 |
module = AnsibleModule( |
| 113 |
argument_spec=argument_spec, |
| 114 |
supports_check_mode=True, |
| 115 |
) |
| 116 |
|
| 117 |
server_number = module.params['server_number'] |
| 118 |
|
| 119 |
url = "{0}/reset/{1}".format(BASE_URL, server_number) |
| 120 |
result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND', 'RESET_NOT_AVAILABLE']) |
| 121 |
if error == 'SERVER_NOT_FOUND': |
| 122 |
module.fail_json(msg='This server does not exist, or you do not have access rights for it') |
| 123 |
if error == 'RESET_NOT_AVAILABLE': |
| 124 |
module.fail_json(msg='The server has no reset option available') |
| 125 |
|
| 126 |
reset = dict(result['reset']) |
| 127 |
|
| 128 |
reset_types = reset.get('type') |
| 129 |
if isinstance(reset_types, list): |
| 130 |
translation = { |
| 131 |
'sw': 'software', |
| 132 |
'hw': 'hardware', |
| 133 |
'power': 'power', |
| 134 |
'man': 'manual', |
| 135 |
} |
| 136 |
reset['type'] = [translation.get(elt, elt) for elt in reset_types] |
| 137 |
|
| 138 |
module.exit_json(reset=reset, changed=True) |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == '__main__': # pragma: no cover |
| 142 |
main() # pragma: no cover |