1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
---
module: boot
short_description: Set boot configuration
version_added: 1.2.0
author:
- Felix Fontein (@felixfontein)
description:
- Set the boot configuration for a dedicated server.
seealso:
- module: community.hrobot.ssh_key
description: Add, remove or update SSH key
- module: community.hrobot.ssh_key_info
description: Query information on SSH keys
extends_documentation_fragment:
- community.hrobot.robot
- community.hrobot.attributes
- community.hrobot.attributes.actiongroup_robot
attributes:
action_group:
version_added: 1.6.0
check_mode:
support: full
diff_mode:
support: none
options:
server_number:
description:
- The server number of the server whose boot configuration to adjust.
type: int
required: true
regular_boot:
description:
- If this option is provided, all special boot configurations are removed and
the installed operating system will be booted up next (assuming it is bootable).
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: bool
choices:
- true
rescue:
description:
- If this option is provided, the rescue system will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
os:
description:
- The operating system to use for the rescue system. Possible choices can
change over time.
- Currently, V(linux), V(linuxold), V(freebsd), V(freebsdold), V(freebsdax),
V(freebsdbetaax), V(vkvm), and V(vkvmold) seem to be available.
type: str
required: true
arch:
description:
- The architecture to use for the rescue system.
- Not all architectures are available for all operating systems.
- Defaults to V(64).
type: int
choices:
- 32
- 64
authorized_keys:
description:
- One or more SSH key fingerprints to equip the rescue system with.
- Only fingerprints for SSH keys deposited in the Robot API can be used.
- You can use the M(community.hrobot.ssh_key_info) module to query the
SSH keys you can use, and the M(community.hrobot.ssh_key) module to
add or update SSH keys.
type: list
elements: str
install_linux:
description:
- If this option is provided, a Linux system install will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
dist:
description:
- The distribution to install.
type: str
required: true
arch:
description:
- The architecture to use for the install.
- Not all architectures are available for all distributions.
- Defaults to V(64).
type: int
choices:
- 32
- 64
lang:
description:
- The language to use for the operating system.
type: str
required: true
authorized_keys:
description:
- One or more SSH key fingerprints to equip the rescue system with.
- Only fingerprints for SSH keys deposited in the Robot API can be used.
- You can use the M(community.hrobot.ssh_key_info) module to query the
SSH keys you can use, and the M(community.hrobot.ssh_key) module to
add or update SSH keys.
type: list
elements: str
install_vnc:
description:
- If this option is provided, a VNC installation will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
dist:
description:
- The distribution to install.
type: str
required: true
arch:
description:
- The architecture to use for the install.
- Not all architectures are available for all distributions.
- Defaults to V(64).
type: int
choices:
- 32
- 64
lang:
description:
- The language to use for the operating system.
type: str
required: true
install_windows:
description:
- If this option is provided, a Windows installation will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
lang:
description:
- The language to use for Windows.
type: str
required: true
install_plesk:
description:
- If this option is provided, a Plesk installation will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
dist:
description:
- The distribution to install.
type: str
required: true
arch:
description:
- The architecture to use for the install.
- Not all architectures are available for all distributions.
- Defaults to V(64).
type: int
choices:
- 32
- 64
lang:
description:
- The language to use for the operating system.
type: str
required: true
hostname:
description:
- The hostname.
type: str
required: true
install_cpanel:
description:
- If this option is provided, a cPanel installation will be activated for the next boot.
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc),
O(install_windows), O(install_plesk), and O(install_cpanel) must be provided.
type: dict
suboptions:
dist:
description:
- The distribution to install.
type: str
required: true
arch:
description:
- The architecture to use for the install.
- Not all architectures are available for all distributions.
- Defaults to V(64).
type: int
choices:
- 32
- 64
lang:
description:
- The language to use for the operating system.
type: str
required: true
hostname:
description:
- The hostname.
type: str
required: true
'''
EXAMPLES = r'''
- name: Disable all special boot configurations
community.hrobot.boot:
hetzner_user: foo
hetzner_password: bar
regular_boot: true
- name: Enable a rescue system (64bit Linux) for the next boot
community.hrobot.boot:
hetzner_user: foo
hetzner_password: bar
rescue:
os: linux
- name: Enable a Linux install for the next boot
community.hrobot.boot:
hetzner_user: foo
hetzner_password: bar
install_linux:
dist: CentOS 5.5 minimal
lang: en
authorized_keys:
- 56:29:99:a4:5d:ed:ac:95:c1:f5:88:82:90:5d:dd:10
- 15:28:b0:03:95:f0:77:b3:10:56:15:6b:77:22:a5:bb
'''
RETURN = r'''
configuration_type:
description:
- Describes the active boot configuration.
returned: success
type: str
choices:
- regular_boot
- rescue
- install_linux
- install_vnc
- install_windows
- install_plesk
- install_cpanel
password:
description:
- The root password for the active boot configuration, if available.
- For non-rescue boot configurations, it is avised to change the root password
as soon as possible.
returned: success and if RV(configuration_type) is not V(regular_boot)
type: str
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
BASE_URL,
ROBOT_DEFAULT_ARGUMENT_SPEC,
fetch_url_json,
)
BOOT_CONFIGURATION_DATA = [
('rescue', 'rescue', {
'os': ('os', 'os'),
'arch': ('arch', 'arch'),
'authorized_keys': ('authorized_key', 'authorized_key'),
}),
('install_linux', 'linux', {
'dist': ('dist', 'dist'),
'arch': ('arch', 'arch'),
'lang': ('lang', 'lang'),
'authorized_keys': ('authorized_key', 'authorized_key'),
}),
('install_vnc', 'vnc', {
'dist': ('dist', 'dist'),
'arch': ('arch', 'arch'),
'lang': ('lang', 'lang'),
}),
('install_windows', 'windows', {
'lang': ('lang', 'lang'),
}),
('install_plesk', 'plesk', {
'dist': ('dist', 'dist'),
'arch': ('arch', 'arch'),
'lang': ('lang', 'lang'),
'hostname': ('hostname', 'hostname'),
}),
('install_cpanel', 'cpanel', {
'dist': ('dist', 'dist'),
'arch': ('arch', 'arch'),
'lang': ('lang', 'lang'),
'hostname': ('hostname', 'hostname'),
}),
]
def main():
argument_spec = dict(
server_number=dict(type='int', required=True),
regular_boot=dict(type='bool', choices=[True]),
rescue=dict(type='dict', options=dict(
os=dict(type='str', required=True),
arch=dict(type='int', choices=[32, 64]),
authorized_keys=dict(type='list', elements='str', no_log=False),
)),
install_linux=dict(type='dict', options=dict(
dist=dict(type='str', required=True),
arch=dict(type='int', choices=[32, 64]),
lang=dict(type='str', required=True),
authorized_keys=dict(type='list', elements='str', no_log=False),
)),
install_vnc=dict(type='dict', options=dict(
dist=dict(type='str', required=True),
arch=dict(type='int', choices=[32, 64]),
lang=dict(type='str', required=True),
)),
install_windows=dict(type='dict', options=dict(
lang=dict(type='str', required=True),
)),
install_plesk=dict(type='dict', options=dict(
dist=dict(type='str', required=True),
arch=dict(type='int', choices=[32, 64]),
lang=dict(type='str', required=True),
hostname=dict(type='str', required=True),
)),
install_cpanel=dict(type='dict', options=dict(
dist=dict(type='str', required=True),
arch=dict(type='int', choices=[32, 64]),
lang=dict(type='str', required=True),
hostname=dict(type='str', required=True),
)),
)
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[('regular_boot', 'rescue', 'install_linux', 'install_vnc', 'install_windows', 'install_plesk', 'install_cpanel')],
required_one_of=[('regular_boot', 'rescue', 'install_linux', 'install_vnc', 'install_windows', 'install_plesk', 'install_cpanel')],
)
server_number = module.params['server_number']
changed = False
# Retrieve current boot config
url = "{0}/boot/{1}".format(BASE_URL, server_number)
result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND', 'BOOT_NOT_AVAILABLE'])
if error is not None:
if error == 'SERVER_NOT_FOUND':
module.fail_json(msg='This server does not exist, or you do not have access rights for it')
if error == 'BOOT_NOT_AVAILABLE':
module.fail_json(msg='There is no boot configuration available for this server')
raise AssertionError('Unexpected error {0}'.format(error)) # pragma: no cover
# Deactivate current boot configurations that are not requested
for option_name, other_name, dummy in BOOT_CONFIGURATION_DATA:
if (result['boot'].get(other_name) or {}).get('active') and not module.params[option_name]:
changed = True
if not module.check_mode:
url = "{0}/boot/{1}/{2}".format(BASE_URL, server_number, other_name)
fetch_url_json(module, url, method='DELETE', allow_empty_result=True)
# Enable/compare boot configuration
return_values = {
'configuration_type': 'regular_boot',
'password': None,
}
for option_name, other_name, options in BOOT_CONFIGURATION_DATA:
if module.params[option_name]:
return_values['configuration_type'] = option_name
existing = result['boot'].get(other_name) or {}
return_values['password'] = existing.get('password')
data = {}
for option_key, (result_key, data_key) in options.items():
option = module.params[option_name][option_key]
if option is None or option == []:
continue
data[data_key] = option
if existing.get('active'):
# Idempotence check
needs_change = False
for option_key, (result_key, data_key) in options.items():
should = module.params[option_name][option_key]
if should is None:
continue
# unfold the return object for the idempotence check to work correctly
has = existing.get(data_key)
if has and option_key == 'authorized_keys':
has = [x['key']['fingerprint'] for x in has]
if isinstance(has, list):
has = sorted(has)
if not isinstance(should, list):
should = [should]
should = sorted(should)
if should != has:
needs_change = True
else:
needs_change = True
if needs_change:
changed = True
if not module.check_mode:
url = "{0}/boot/{1}/{2}".format(BASE_URL, server_number, other_name)
if existing.get('active'):
# Deactivate existing boot configuration
fetch_url_json(module, url, method='DELETE', allow_empty_result=True)
# Enable new boot configuration
headers = {"Content-type": "application/x-www-form-urlencoded"}
result, dummy = fetch_url_json(
module,
url,
data=urlencode(data, True),
headers=headers,
method='POST',
)
return_values['password'] = (result.get(other_name) or {}).get('password')
else:
return_values['password'] = None
module.exit_json(changed=changed, **return_values)
if __name__ == '__main__': # pragma: no cover
main() # pragma: no cover
|