| 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: server_info |
| 14 |
short_description: Query information on one or more servers |
| 15 |
version_added: 1.2.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Query information on one or more servers. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.hrobot.robot |
| 22 |
- community.hrobot.attributes |
| 23 |
- community.hrobot.attributes.actiongroup_robot |
| 24 |
- community.hrobot.attributes.idempotent_not_modify_state |
| 25 |
- community.hrobot.attributes.info_module |
| 26 |
|
| 27 |
attributes: |
| 28 |
action_group: |
| 29 |
version_added: 1.6.0 |
| 30 |
|
| 31 |
options: |
| 32 |
server_number: |
| 33 |
description: |
| 34 |
- Limit result list to server with this number. |
| 35 |
type: int |
| 36 |
server_name: |
| 37 |
description: |
| 38 |
- Limit result list to servers of this name. |
| 39 |
type: str |
| 40 |
full_info: |
| 41 |
description: |
| 42 |
- Whether to provide full information for every server. |
| 43 |
- Setting this to V(true) requires one REST call per server, which is slow and reduces your rate limit. Use with care. |
| 44 |
- When O(server_number) is specified, this option is always treated as having value V(true). |
| 45 |
type: bool |
| 46 |
default: false |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
--- |
| 51 |
- name: Query a list of all servers |
| 52 |
community.hrobot.server_info: |
| 53 |
hetzner_user: foo |
| 54 |
hetzner_password: bar |
| 55 |
register: result |
| 56 |
|
| 57 |
- name: Query a specific server |
| 58 |
community.hrobot.server_info: |
| 59 |
hetzner_user: foo |
| 60 |
hetzner_password: bar |
| 61 |
server_number: 23 |
| 62 |
register: result |
| 63 |
|
| 64 |
- name: Output data on specific server |
| 65 |
ansible.builtin.debug: |
| 66 |
msg: "Server name: {{ result.servers[0].server_name }}" |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = r""" |
| 70 |
servers: |
| 71 |
description: |
| 72 |
- List of servers matching the provided options. |
| 73 |
returned: success |
| 74 |
type: list |
| 75 |
elements: dict |
| 76 |
contains: |
| 77 |
server_ip: |
| 78 |
description: |
| 79 |
- The server's main IP address. |
| 80 |
type: str |
| 81 |
sample: 123.123.123.123 |
| 82 |
returned: success |
| 83 |
server_ipv6_net: |
| 84 |
description: |
| 85 |
- The server's main IPv6 network address. |
| 86 |
type: str |
| 87 |
sample: '2a01:f48:111:4221::' |
| 88 |
returned: success |
| 89 |
server_number: |
| 90 |
description: |
| 91 |
- The server's numeric ID. |
| 92 |
type: int |
| 93 |
sample: 321 |
| 94 |
returned: success |
| 95 |
server_name: |
| 96 |
description: |
| 97 |
- The user-defined server's name. |
| 98 |
type: str |
| 99 |
sample: server1 |
| 100 |
returned: success |
| 101 |
product: |
| 102 |
description: |
| 103 |
- The server product name. |
| 104 |
type: str |
| 105 |
sample: EQ 8 |
| 106 |
returned: success |
| 107 |
dc: |
| 108 |
description: |
| 109 |
- The data center the server is located in. |
| 110 |
type: str |
| 111 |
sample: NBG1-DC1 |
| 112 |
returned: success |
| 113 |
traffic: |
| 114 |
description: |
| 115 |
- Free traffic quota. |
| 116 |
- V(unlimited) in case of unlimited traffic. |
| 117 |
type: str |
| 118 |
sample: 5 TB |
| 119 |
returned: success |
| 120 |
status: |
| 121 |
description: |
| 122 |
- Server status. |
| 123 |
type: str |
| 124 |
choices: |
| 125 |
- ready |
| 126 |
- in process |
| 127 |
sample: ready |
| 128 |
returned: success |
| 129 |
cancelled: |
| 130 |
description: |
| 131 |
- Whether the server is cancelled. |
| 132 |
type: bool |
| 133 |
sample: false |
| 134 |
returned: success |
| 135 |
paid_until: |
| 136 |
description: |
| 137 |
- The date until the server has been paid. |
| 138 |
type: str |
| 139 |
sample: "2018-08-04" |
| 140 |
returned: success |
| 141 |
ip: |
| 142 |
description: |
| 143 |
- List of assigned single IP addresses. |
| 144 |
type: list |
| 145 |
elements: str |
| 146 |
sample: |
| 147 |
- 123.123.123.123 |
| 148 |
returned: success |
| 149 |
subnet: |
| 150 |
description: |
| 151 |
- List of assigned subnets. |
| 152 |
type: list |
| 153 |
elements: dict |
| 154 |
sample: |
| 155 |
- ip: '2a01:4f8:111:4221::' |
| 156 |
mask: 64 |
| 157 |
contains: |
| 158 |
ip: |
| 159 |
description: |
| 160 |
- The first IP in the subnet. |
| 161 |
type: str |
| 162 |
sample: '2a01:4f8:111:4221::' |
| 163 |
mask: |
| 164 |
description: |
| 165 |
- The masks bitlength. |
| 166 |
type: str |
| 167 |
sample: "64" |
| 168 |
returned: success |
| 169 |
reset: |
| 170 |
description: |
| 171 |
- Whether the server can be automatically reset. |
| 172 |
type: bool |
| 173 |
sample: true |
| 174 |
returned: when O(full_info=true) |
| 175 |
rescue: |
| 176 |
description: |
| 177 |
- Whether the rescue system is available. |
| 178 |
type: bool |
| 179 |
sample: false |
| 180 |
returned: when O(full_info=true) |
| 181 |
vnc: |
| 182 |
description: |
| 183 |
- Flag of VNC installation availability. |
| 184 |
type: bool |
| 185 |
sample: true |
| 186 |
returned: when O(full_info=true) |
| 187 |
windows: |
| 188 |
description: |
| 189 |
- Flag of Windows installation availability. |
| 190 |
type: bool |
| 191 |
sample: true |
| 192 |
returned: when O(full_info=true) |
| 193 |
plesk: |
| 194 |
description: |
| 195 |
- Flag of Plesk installation availability. |
| 196 |
type: bool |
| 197 |
sample: true |
| 198 |
returned: when O(full_info=true) |
| 199 |
cpanel: |
| 200 |
description: |
| 201 |
- Flag of cPanel installation availability. |
| 202 |
type: bool |
| 203 |
sample: true |
| 204 |
returned: when O(full_info=true) |
| 205 |
wol: |
| 206 |
description: |
| 207 |
- Flag of Wake On Lan availability. |
| 208 |
type: bool |
| 209 |
sample: true |
| 210 |
returned: when O(full_info=true) |
| 211 |
hot_swap: |
| 212 |
description: |
| 213 |
- Flag of Hot Swap availability. |
| 214 |
type: bool |
| 215 |
sample: true |
| 216 |
returned: when O(full_info=true) |
| 217 |
linked_storagebox: |
| 218 |
description: |
| 219 |
- Linked Storage Box ID. |
| 220 |
type: int |
| 221 |
sample: 12345 |
| 222 |
returned: when O(full_info=true) |
| 223 |
""" |
| 224 |
|
| 225 |
from ansible.module_utils.basic import AnsibleModule |
| 226 |
|
| 227 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 228 |
BASE_URL, |
| 229 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 230 |
fetch_url_json, |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
argument_spec = dict( |
| 236 |
server_number=dict(type='int'), |
| 237 |
server_name=dict(type='str'), |
| 238 |
full_info=dict(type='bool', default=False), |
| 239 |
) |
| 240 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 241 |
module = AnsibleModule( |
| 242 |
argument_spec=argument_spec, |
| 243 |
supports_check_mode=True, |
| 244 |
) |
| 245 |
|
| 246 |
server_number = module.params['server_number'] |
| 247 |
server_name = module.params['server_name'] |
| 248 |
full_info = module.params['full_info'] |
| 249 |
|
| 250 |
servers = [] |
| 251 |
if server_number is not None: |
| 252 |
server_numbers = [server_number] |
| 253 |
else: |
| 254 |
url = "{0}/server".format(BASE_URL) |
| 255 |
result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND']) |
| 256 |
server_numbers = [] |
| 257 |
if not error: |
| 258 |
for entry in result: |
| 259 |
if server_name is not None: |
| 260 |
if entry['server']['server_name'] != server_name: |
| 261 |
continue |
| 262 |
if full_info: |
| 263 |
server_numbers.append(entry['server']['server_number']) |
| 264 |
else: |
| 265 |
servers.append(entry['server']) |
| 266 |
|
| 267 |
for server_number in server_numbers: |
| 268 |
url = "{0}/server/{1}".format(BASE_URL, server_number) |
| 269 |
result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND']) |
| 270 |
if not error: |
| 271 |
if server_name is not None: |
| 272 |
if result['server']['server_name'] != server_name: |
| 273 |
continue |
| 274 |
servers.append(result['server']) |
| 275 |
|
| 276 |
module.exit_json( |
| 277 |
changed=False, |
| 278 |
servers=servers, |
| 279 |
) |
| 280 |
|
| 281 |
|
| 282 |
if __name__ == '__main__': # pragma: no cover |
| 283 |
main() # pragma: no cover |