| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: linode |
| 11 |
short_description: Manage instances on the Linode Public Cloud |
| 12 |
description: |
| 13 |
- Manage Linode Public Cloud instances and optionally wait for it to be 'running'. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: none |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Indicate desired state of the resource. |
| 25 |
choices: [absent, active, deleted, present, restarted, started, stopped] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
api_key: |
| 29 |
description: |
| 30 |
- Linode API key. |
| 31 |
- E(LINODE_API_KEY) environment variable can be used instead. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name to give the instance (alphanumeric, dashes, underscore). |
| 37 |
- To keep sanity on the Linode Web Console, name is prepended with C(LinodeID-). |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
displaygroup: |
| 41 |
description: |
| 42 |
- Add the instance to a Display Group in Linode Manager. |
| 43 |
type: str |
| 44 |
default: '' |
| 45 |
linode_id: |
| 46 |
description: |
| 47 |
- Unique ID of a Linode server. This value is read-only in the sense that if you specify it on creation of a Linode |
| 48 |
it is not used. The Linode API generates these IDs and we can those generated value here to reference a Linode more |
| 49 |
specifically. This is useful for idempotency. |
| 50 |
aliases: [lid] |
| 51 |
type: int |
| 52 |
additional_disks: |
| 53 |
description: |
| 54 |
- List of dictionaries for creating additional disks that are added to the Linode configuration settings. |
| 55 |
- Dictionary takes Size, Label, Type. Size is in MB. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
alert_bwin_enabled: |
| 59 |
description: |
| 60 |
- Set status of bandwidth in alerts. |
| 61 |
type: bool |
| 62 |
alert_bwin_threshold: |
| 63 |
description: |
| 64 |
- Set threshold in MB of bandwidth in alerts. |
| 65 |
type: int |
| 66 |
alert_bwout_enabled: |
| 67 |
description: |
| 68 |
- Set status of bandwidth out alerts. |
| 69 |
type: bool |
| 70 |
alert_bwout_threshold: |
| 71 |
description: |
| 72 |
- Set threshold in MB of bandwidth out alerts. |
| 73 |
type: int |
| 74 |
alert_bwquota_enabled: |
| 75 |
description: |
| 76 |
- Set status of bandwidth quota alerts as percentage of network transfer quota. |
| 77 |
type: bool |
| 78 |
alert_bwquota_threshold: |
| 79 |
description: |
| 80 |
- Set threshold in MB of bandwidth quota alerts. |
| 81 |
type: int |
| 82 |
alert_cpu_enabled: |
| 83 |
description: |
| 84 |
- Set status of receiving CPU usage alerts. |
| 85 |
type: bool |
| 86 |
alert_cpu_threshold: |
| 87 |
description: |
| 88 |
- Set percentage threshold for receiving CPU usage alerts. Each CPU core adds 100% to total. |
| 89 |
type: int |
| 90 |
alert_diskio_enabled: |
| 91 |
description: |
| 92 |
- Set status of receiving disk IO alerts. |
| 93 |
type: bool |
| 94 |
alert_diskio_threshold: |
| 95 |
description: |
| 96 |
- Set threshold for average IO ops/sec over 2 hour period. |
| 97 |
type: int |
| 98 |
backupweeklyday: |
| 99 |
description: |
| 100 |
- Day of the week to take backups. |
| 101 |
type: int |
| 102 |
backupwindow: |
| 103 |
description: |
| 104 |
- The time window in which backups are taken. |
| 105 |
type: int |
| 106 |
plan: |
| 107 |
description: |
| 108 |
- Plan to use for the instance (Linode plan). |
| 109 |
type: int |
| 110 |
payment_term: |
| 111 |
description: |
| 112 |
- Payment term to use for the instance (payment term in months). |
| 113 |
default: 1 |
| 114 |
choices: [1, 12, 24] |
| 115 |
type: int |
| 116 |
password: |
| 117 |
description: |
| 118 |
- Root password to apply to a new server (auto generated if missing). |
| 119 |
type: str |
| 120 |
private_ip: |
| 121 |
description: |
| 122 |
- Add private IPv4 address when Linode is created. |
| 123 |
- Default is V(false). |
| 124 |
type: bool |
| 125 |
ssh_pub_key: |
| 126 |
description: |
| 127 |
- SSH public key applied to root user. |
| 128 |
type: str |
| 129 |
swap: |
| 130 |
description: |
| 131 |
- Swap size in MB. |
| 132 |
default: 512 |
| 133 |
type: int |
| 134 |
distribution: |
| 135 |
description: |
| 136 |
- Distribution to use for the instance (Linode Distribution). |
| 137 |
type: int |
| 138 |
datacenter: |
| 139 |
description: |
| 140 |
- Datacenter to create an instance in (Linode Datacenter). |
| 141 |
type: int |
| 142 |
kernel_id: |
| 143 |
description: |
| 144 |
- Kernel to use for the instance (Linode Kernel). |
| 145 |
type: int |
| 146 |
wait: |
| 147 |
description: |
| 148 |
- Wait for the instance to be in state V(running) before returning. |
| 149 |
type: bool |
| 150 |
default: true |
| 151 |
wait_timeout: |
| 152 |
description: |
| 153 |
- How long before wait gives up, in seconds. |
| 154 |
default: 300 |
| 155 |
type: int |
| 156 |
watchdog: |
| 157 |
description: |
| 158 |
- Set status of Lassie watchdog. |
| 159 |
type: bool |
| 160 |
default: true |
| 161 |
requirements: |
| 162 |
- linode-python |
| 163 |
author: |
| 164 |
- Vincent Viallet (@zbal) |
| 165 |
notes: |
| 166 |
- Please note, linode-python does not have python 3 support. |
| 167 |
- This module uses the now deprecated v3 of the Linode API. |
| 168 |
- Please review U(https://www.linode.com/api/linode) for determining the required parameters. |
| 169 |
""" |
| 170 |
|
| 171 |
EXAMPLES = r""" |
| 172 |
- name: Create a new Linode |
| 173 |
community.general.linode: |
| 174 |
name: linode-test1 |
| 175 |
plan: 1 |
| 176 |
datacenter: 7 |
| 177 |
distribution: 129 |
| 178 |
state: present |
| 179 |
register: linode_creation |
| 180 |
|
| 181 |
- name: Create a server with a private IP Address |
| 182 |
community.general.linode: |
| 183 |
module: linode |
| 184 |
api_key: 'longStringFromLinodeApi' |
| 185 |
name: linode-test1 |
| 186 |
plan: 1 |
| 187 |
datacenter: 2 |
| 188 |
distribution: 99 |
| 189 |
password: 'superSecureRootPassword' |
| 190 |
private_ip: true |
| 191 |
ssh_pub_key: 'ssh-rsa qwerty' |
| 192 |
swap: 768 |
| 193 |
wait: true |
| 194 |
wait_timeout: 600 |
| 195 |
state: present |
| 196 |
delegate_to: localhost |
| 197 |
register: linode_creation |
| 198 |
|
| 199 |
- name: Fully configure new server |
| 200 |
community.general.linode: |
| 201 |
api_key: 'longStringFromLinodeApi' |
| 202 |
name: linode-test1 |
| 203 |
plan: 4 |
| 204 |
datacenter: 2 |
| 205 |
distribution: 99 |
| 206 |
kernel_id: 138 |
| 207 |
password: 'superSecureRootPassword' |
| 208 |
private_ip: true |
| 209 |
ssh_pub_key: 'ssh-rsa qwerty' |
| 210 |
swap: 768 |
| 211 |
wait: true |
| 212 |
wait_timeout: 600 |
| 213 |
state: present |
| 214 |
alert_bwquota_enabled: true |
| 215 |
alert_bwquota_threshold: 80 |
| 216 |
alert_bwin_enabled: true |
| 217 |
alert_bwin_threshold: 10 |
| 218 |
alert_cpu_enabled: true |
| 219 |
alert_cpu_threshold: 210 |
| 220 |
alert_bwout_enabled: true |
| 221 |
alert_bwout_threshold: 10 |
| 222 |
alert_diskio_enabled: true |
| 223 |
alert_diskio_threshold: 10000 |
| 224 |
backupweeklyday: 1 |
| 225 |
backupwindow: 2 |
| 226 |
displaygroup: 'test' |
| 227 |
additional_disks: |
| 228 |
- {Label: 'disk1', Size: 2500, Type: 'raw'} |
| 229 |
- {Label: 'newdisk', Size: 2000} |
| 230 |
watchdog: true |
| 231 |
delegate_to: localhost |
| 232 |
register: linode_creation |
| 233 |
|
| 234 |
- name: Ensure a running server (create if missing) |
| 235 |
community.general.linode: |
| 236 |
api_key: 'longStringFromLinodeApi' |
| 237 |
name: linode-test1 |
| 238 |
plan: 1 |
| 239 |
datacenter: 2 |
| 240 |
distribution: 99 |
| 241 |
password: 'superSecureRootPassword' |
| 242 |
ssh_pub_key: 'ssh-rsa qwerty' |
| 243 |
swap: 768 |
| 244 |
wait: true |
| 245 |
wait_timeout: 600 |
| 246 |
state: present |
| 247 |
delegate_to: localhost |
| 248 |
register: linode_creation |
| 249 |
|
| 250 |
- name: Delete a server |
| 251 |
community.general.linode: |
| 252 |
api_key: 'longStringFromLinodeApi' |
| 253 |
name: linode-test1 |
| 254 |
linode_id: "{{ linode_creation.instance.id }}" |
| 255 |
state: absent |
| 256 |
delegate_to: localhost |
| 257 |
|
| 258 |
- name: Stop a server |
| 259 |
community.general.linode: |
| 260 |
api_key: 'longStringFromLinodeApi' |
| 261 |
name: linode-test1 |
| 262 |
linode_id: "{{ linode_creation.instance.id }}" |
| 263 |
state: stopped |
| 264 |
delegate_to: localhost |
| 265 |
|
| 266 |
- name: Reboot a server |
| 267 |
community.general.linode: |
| 268 |
api_key: 'longStringFromLinodeApi' |
| 269 |
name: linode-test1 |
| 270 |
linode_id: "{{ linode_creation.instance.id }}" |
| 271 |
state: restarted |
| 272 |
delegate_to: localhost |
| 273 |
""" |
| 274 |
|
| 275 |
import random |
| 276 |
import string |
| 277 |
import time |
| 278 |
import traceback |
| 279 |
|
| 280 |
LINODE_IMP_ERR = None |
| 281 |
try: |
| 282 |
from linode import api as linode_api |
| 283 |
|
| 284 |
HAS_LINODE = True |
| 285 |
except ImportError: |
| 286 |
LINODE_IMP_ERR = traceback.format_exc() |
| 287 |
HAS_LINODE = False |
| 288 |
|
| 289 |
from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib |
| 290 |
|
| 291 |
|
| 292 |
def randompass(): |
| 293 |
""" |
| 294 |
Generate a long random password that comply to Linode requirements |
| 295 |
""" |
| 296 |
# Linode API currently requires the following: |
| 297 |
# It must contain at least two of these four character classes: |
| 298 |
# lower case letters - upper case letters - numbers - punctuation |
| 299 |
# we play it safe :) |
| 300 |
|
| 301 |
# as of python 2.4, this reseeds the PRNG from urandom |
| 302 |
random.seed() |
| 303 |
lower = "".join(random.choice(string.ascii_lowercase) for x in range(6)) |
| 304 |
upper = "".join(random.choice(string.ascii_uppercase) for x in range(6)) |
| 305 |
number = "".join(random.choice(string.digits) for x in range(6)) |
| 306 |
punct = "".join(random.choice(string.punctuation) for x in range(6)) |
| 307 |
p = lower + upper + number + punct |
| 308 |
return "".join(random.sample(p, len(p))) |
| 309 |
|
| 310 |
|
| 311 |
def getInstanceDetails(api, server): |
| 312 |
""" |
| 313 |
Return the details of an instance, populating IPs, etc. |
| 314 |
""" |
| 315 |
instance = {"id": server["LINODEID"], "name": server["LABEL"], "public": [], "private": []} |
| 316 |
|
| 317 |
# Populate with ips |
| 318 |
for ip in api.linode_ip_list(LinodeId=server["LINODEID"]): |
| 319 |
if ip["ISPUBLIC"] and "ipv4" not in instance: |
| 320 |
instance["ipv4"] = ip["IPADDRESS"] |
| 321 |
instance["fqdn"] = ip["RDNS_NAME"] |
| 322 |
if ip["ISPUBLIC"]: |
| 323 |
instance["public"].append({"ipv4": ip["IPADDRESS"], "fqdn": ip["RDNS_NAME"], "ip_id": ip["IPADDRESSID"]}) |
| 324 |
else: |
| 325 |
instance["private"].append({"ipv4": ip["IPADDRESS"], "fqdn": ip["RDNS_NAME"], "ip_id": ip["IPADDRESSID"]}) |
| 326 |
return instance |
| 327 |
|
| 328 |
|
| 329 |
def linodeServers( |
| 330 |
module, |
| 331 |
api, |
| 332 |
state, |
| 333 |
name, |
| 334 |
displaygroup, |
| 335 |
plan, |
| 336 |
additional_disks, |
| 337 |
distribution, |
| 338 |
datacenter, |
| 339 |
kernel_id, |
| 340 |
linode_id, |
| 341 |
payment_term, |
| 342 |
password, |
| 343 |
private_ip, |
| 344 |
ssh_pub_key, |
| 345 |
swap, |
| 346 |
wait, |
| 347 |
wait_timeout, |
| 348 |
watchdog, |
| 349 |
**kwargs, |
| 350 |
): |
| 351 |
instances = [] |
| 352 |
changed = False |
| 353 |
new_server = False |
| 354 |
servers = [] |
| 355 |
disks = [] |
| 356 |
configs = [] |
| 357 |
jobs = [] |
| 358 |
|
| 359 |
# See if we can match an existing server details with the provided linode_id |
| 360 |
if linode_id: |
| 361 |
# For the moment we only consider linode_id as criteria for match |
| 362 |
# Later we can use more (size, name, etc.) and update existing |
| 363 |
servers = api.linode_list(LinodeId=linode_id) |
| 364 |
# Attempt to fetch details about disks and configs only if servers are |
| 365 |
# found with linode_id |
| 366 |
if servers: |
| 367 |
disks = api.linode_disk_list(LinodeId=linode_id) |
| 368 |
configs = api.linode_config_list(LinodeId=linode_id) |
| 369 |
|
| 370 |
# Act on the state |
| 371 |
if state in ("active", "present", "started"): |
| 372 |
# TODO: validate all the plan / distribution / datacenter are valid |
| 373 |
|
| 374 |
# Multi step process/validation: |
| 375 |
# - need linode_id (entity) |
| 376 |
# - need disk_id for linode_id - create disk from distrib |
| 377 |
# - need config_id for linode_id - create config (need kernel) |
| 378 |
|
| 379 |
# Any create step triggers a job that need to be waited for. |
| 380 |
if not servers: |
| 381 |
for arg in (name, plan, distribution, datacenter): |
| 382 |
if not arg: |
| 383 |
module.fail_json(msg=f"{arg} is required for {state} state") |
| 384 |
# Create linode entity |
| 385 |
new_server = True |
| 386 |
|
| 387 |
# Get size of all individually listed disks to subtract from Distribution disk |
| 388 |
used_disk_space = 0 if additional_disks is None else sum(disk["Size"] for disk in additional_disks) |
| 389 |
|
| 390 |
try: |
| 391 |
res = api.linode_create(DatacenterID=datacenter, PlanID=plan, PaymentTerm=payment_term) |
| 392 |
linode_id = res["LinodeID"] |
| 393 |
# Update linode Label to match name |
| 394 |
api.linode_update(LinodeId=linode_id, Label=f"{linode_id}-{name}") |
| 395 |
# Update Linode with Ansible configuration options |
| 396 |
api.linode_update(LinodeId=linode_id, LPM_DISPLAYGROUP=displaygroup, WATCHDOG=watchdog, **kwargs) |
| 397 |
# Save server |
| 398 |
servers = api.linode_list(LinodeId=linode_id) |
| 399 |
except Exception as e: |
| 400 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}") |
| 401 |
|
| 402 |
# Add private IP to Linode |
| 403 |
if private_ip: |
| 404 |
try: |
| 405 |
res = api.linode_ip_addprivate(LinodeID=linode_id) |
| 406 |
except Exception as e: |
| 407 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 408 |
|
| 409 |
if not disks: |
| 410 |
for arg in (name, linode_id, distribution): |
| 411 |
if not arg: |
| 412 |
module.fail_json(msg=f"{arg} is required for {state} state") |
| 413 |
# Create disks (1 from distrib, 1 for SWAP) |
| 414 |
new_server = True |
| 415 |
try: |
| 416 |
if not password: |
| 417 |
# Password is required on creation, if not provided generate one |
| 418 |
password = randompass() |
| 419 |
if not swap: |
| 420 |
swap = 512 |
| 421 |
# Create data disk |
| 422 |
size = servers[0]["TOTALHD"] - used_disk_space - swap |
| 423 |
|
| 424 |
if ssh_pub_key: |
| 425 |
res = api.linode_disk_createfromdistribution( |
| 426 |
LinodeId=linode_id, |
| 427 |
DistributionID=distribution, |
| 428 |
rootPass=password, |
| 429 |
rootSSHKey=ssh_pub_key, |
| 430 |
Label=f"{name} data disk (lid: {linode_id})", |
| 431 |
Size=size, |
| 432 |
) |
| 433 |
else: |
| 434 |
res = api.linode_disk_createfromdistribution( |
| 435 |
LinodeId=linode_id, |
| 436 |
DistributionID=distribution, |
| 437 |
rootPass=password, |
| 438 |
Label=f"{name} data disk (lid: {linode_id})", |
| 439 |
Size=size, |
| 440 |
) |
| 441 |
jobs.append(res["JobID"]) |
| 442 |
# Create SWAP disk |
| 443 |
res = api.linode_disk_create( |
| 444 |
LinodeId=linode_id, Type="swap", Label=f"{name} swap disk (lid: {linode_id})", Size=swap |
| 445 |
) |
| 446 |
# Create individually listed disks at specified size |
| 447 |
if additional_disks: |
| 448 |
for disk in additional_disks: |
| 449 |
# If a disk Type is not passed in, default to ext4 |
| 450 |
if disk.get("Type") is None: |
| 451 |
disk["Type"] = "ext4" |
| 452 |
res = api.linode_disk_create( |
| 453 |
LinodeID=linode_id, Label=disk["Label"], Size=disk["Size"], Type=disk["Type"] |
| 454 |
) |
| 455 |
|
| 456 |
jobs.append(res["JobID"]) |
| 457 |
except Exception as e: |
| 458 |
# TODO: destroy linode ? |
| 459 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 460 |
|
| 461 |
if not configs: |
| 462 |
for arg in (name, linode_id, distribution): |
| 463 |
if not arg: |
| 464 |
module.fail_json(msg=f"{arg} is required for {state} state") |
| 465 |
|
| 466 |
# Check architecture |
| 467 |
for distrib in api.avail_distributions(): |
| 468 |
if distrib["DISTRIBUTIONID"] != distribution: |
| 469 |
continue |
| 470 |
arch = "32" |
| 471 |
if distrib["IS64BIT"]: |
| 472 |
arch = "64" |
| 473 |
break |
| 474 |
|
| 475 |
# Get latest kernel matching arch if kernel_id is not specified |
| 476 |
if not kernel_id: |
| 477 |
for kernel in api.avail_kernels(): |
| 478 |
if not kernel["LABEL"].startswith(f"Latest {arch}"): |
| 479 |
continue |
| 480 |
kernel_id = kernel["KERNELID"] |
| 481 |
break |
| 482 |
|
| 483 |
# Get disk list |
| 484 |
disks_id = [] |
| 485 |
for disk in api.linode_disk_list(LinodeId=linode_id): |
| 486 |
if disk["TYPE"] == "ext3": |
| 487 |
disks_id.insert(0, str(disk["DISKID"])) |
| 488 |
continue |
| 489 |
disks_id.append(str(disk["DISKID"])) |
| 490 |
# Trick to get the 9 items in the list |
| 491 |
while len(disks_id) < 9: |
| 492 |
disks_id.append("") |
| 493 |
disks_list = ",".join(disks_id) |
| 494 |
|
| 495 |
# Create config |
| 496 |
new_server = True |
| 497 |
try: |
| 498 |
api.linode_config_create( |
| 499 |
LinodeId=linode_id, KernelId=kernel_id, Disklist=disks_list, Label=f"{name} config" |
| 500 |
) |
| 501 |
configs = api.linode_config_list(LinodeId=linode_id) |
| 502 |
except Exception as e: |
| 503 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 504 |
|
| 505 |
# Start / Ensure servers are running |
| 506 |
for server in servers: |
| 507 |
# Refresh server state |
| 508 |
server = api.linode_list(LinodeId=server["LINODEID"])[0] |
| 509 |
# Ensure existing servers are up and running, boot if necessary |
| 510 |
if server["STATUS"] != 1: |
| 511 |
res = api.linode_boot(LinodeId=linode_id) |
| 512 |
jobs.append(res["JobID"]) |
| 513 |
changed = True |
| 514 |
|
| 515 |
# wait here until the instances are up |
| 516 |
wait_timeout = time.time() + wait_timeout |
| 517 |
while wait and wait_timeout > time.time(): |
| 518 |
# refresh the server details |
| 519 |
server = api.linode_list(LinodeId=server["LINODEID"])[0] |
| 520 |
# status: |
| 521 |
# -2: Boot failed |
| 522 |
# 1: Running |
| 523 |
if server["STATUS"] in (-2, 1): |
| 524 |
break |
| 525 |
time.sleep(5) |
| 526 |
if wait and wait_timeout <= time.time(): |
| 527 |
# waiting took too long |
| 528 |
module.fail_json(msg=f"Timeout waiting on {server['LABEL']} (lid: {server['LINODEID']})") |
| 529 |
# Get a fresh copy of the server details |
| 530 |
server = api.linode_list(LinodeId=server["LINODEID"])[0] |
| 531 |
if server["STATUS"] == -2: |
| 532 |
module.fail_json(msg=f"{server['LABEL']} (lid: {server['LINODEID']}) failed to boot") |
| 533 |
# From now on we know the task is a success |
| 534 |
# Build instance report |
| 535 |
instance = getInstanceDetails(api, server) |
| 536 |
# depending on wait flag select the status |
| 537 |
if wait: |
| 538 |
instance["status"] = "Running" |
| 539 |
else: |
| 540 |
instance["status"] = "Starting" |
| 541 |
|
| 542 |
# Return the root password if this is a new box and no SSH key |
| 543 |
# has been provided |
| 544 |
if new_server and not ssh_pub_key: |
| 545 |
instance["password"] = password |
| 546 |
instances.append(instance) |
| 547 |
|
| 548 |
elif state in ("stopped",): |
| 549 |
if not servers: |
| 550 |
module.fail_json(msg=f"Server (lid: {linode_id}) not found") |
| 551 |
|
| 552 |
for server in servers: |
| 553 |
instance = getInstanceDetails(api, server) |
| 554 |
if server["STATUS"] != 2: |
| 555 |
try: |
| 556 |
res = api.linode_shutdown(LinodeId=linode_id) |
| 557 |
except Exception as e: |
| 558 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 559 |
instance["status"] = "Stopping" |
| 560 |
changed = True |
| 561 |
else: |
| 562 |
instance["status"] = "Stopped" |
| 563 |
instances.append(instance) |
| 564 |
|
| 565 |
elif state in ("restarted",): |
| 566 |
if not servers: |
| 567 |
module.fail_json(msg=f"Server (lid: {linode_id}) not found") |
| 568 |
|
| 569 |
for server in servers: |
| 570 |
instance = getInstanceDetails(api, server) |
| 571 |
try: |
| 572 |
res = api.linode_reboot(LinodeId=server["LINODEID"]) |
| 573 |
except Exception as e: |
| 574 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 575 |
instance["status"] = "Restarting" |
| 576 |
changed = True |
| 577 |
instances.append(instance) |
| 578 |
|
| 579 |
elif state in ("absent", "deleted"): |
| 580 |
for server in servers: |
| 581 |
instance = getInstanceDetails(api, server) |
| 582 |
try: |
| 583 |
api.linode_delete(LinodeId=server["LINODEID"], skipChecks=True) |
| 584 |
except Exception as e: |
| 585 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 586 |
instance["status"] = "Deleting" |
| 587 |
changed = True |
| 588 |
instances.append(instance) |
| 589 |
|
| 590 |
# Ease parsing if only 1 instance |
| 591 |
if len(instances) == 1: |
| 592 |
module.exit_json(changed=changed, instance=instances[0]) |
| 593 |
|
| 594 |
module.exit_json(changed=changed, instances=instances) |
| 595 |
|
| 596 |
|
| 597 |
def main(): |
| 598 |
module = AnsibleModule( |
| 599 |
argument_spec=dict( |
| 600 |
state=dict( |
| 601 |
type="str", |
| 602 |
default="present", |
| 603 |
choices=["absent", "active", "deleted", "present", "restarted", "started", "stopped"], |
| 604 |
), |
| 605 |
api_key=dict(type="str", no_log=True, required=True, fallback=(env_fallback, ["LINODE_API_KEY"])), |
| 606 |
name=dict(type="str", required=True), |
| 607 |
alert_bwin_enabled=dict(type="bool"), |
| 608 |
alert_bwin_threshold=dict(type="int"), |
| 609 |
alert_bwout_enabled=dict(type="bool"), |
| 610 |
alert_bwout_threshold=dict(type="int"), |
| 611 |
alert_bwquota_enabled=dict(type="bool"), |
| 612 |
alert_bwquota_threshold=dict(type="int"), |
| 613 |
alert_cpu_enabled=dict(type="bool"), |
| 614 |
alert_cpu_threshold=dict(type="int"), |
| 615 |
alert_diskio_enabled=dict(type="bool"), |
| 616 |
alert_diskio_threshold=dict(type="int"), |
| 617 |
backupweeklyday=dict(type="int"), |
| 618 |
backupwindow=dict(type="int"), |
| 619 |
displaygroup=dict(type="str", default=""), |
| 620 |
plan=dict(type="int"), |
| 621 |
additional_disks=dict(type="list", elements="dict"), |
| 622 |
distribution=dict(type="int"), |
| 623 |
datacenter=dict(type="int"), |
| 624 |
kernel_id=dict(type="int"), |
| 625 |
linode_id=dict(type="int", aliases=["lid"]), |
| 626 |
payment_term=dict(type="int", default=1, choices=[1, 12, 24]), |
| 627 |
password=dict(type="str", no_log=True), |
| 628 |
private_ip=dict(type="bool"), |
| 629 |
ssh_pub_key=dict(type="str"), |
| 630 |
swap=dict(type="int", default=512), |
| 631 |
wait=dict(type="bool", default=True), |
| 632 |
wait_timeout=dict(type="int", default=300), |
| 633 |
watchdog=dict(type="bool", default=True), |
| 634 |
), |
| 635 |
required_if=[ |
| 636 |
("state", "restarted", ["linode_id"]), |
| 637 |
("state", "stopped", ["linode_id"]), |
| 638 |
], |
| 639 |
) |
| 640 |
|
| 641 |
if not HAS_LINODE: |
| 642 |
module.fail_json(msg=missing_required_lib("linode-python"), exception=LINODE_IMP_ERR) |
| 643 |
|
| 644 |
state = module.params.get("state") |
| 645 |
api_key = module.params.get("api_key") |
| 646 |
name = module.params.get("name") |
| 647 |
alert_bwin_enabled = module.params.get("alert_bwin_enabled") |
| 648 |
alert_bwin_threshold = module.params.get("alert_bwin_threshold") |
| 649 |
alert_bwout_enabled = module.params.get("alert_bwout_enabled") |
| 650 |
alert_bwout_threshold = module.params.get("alert_bwout_threshold") |
| 651 |
alert_bwquota_enabled = module.params.get("alert_bwquota_enabled") |
| 652 |
alert_bwquota_threshold = module.params.get("alert_bwquota_threshold") |
| 653 |
alert_cpu_enabled = module.params.get("alert_cpu_enabled") |
| 654 |
alert_cpu_threshold = module.params.get("alert_cpu_threshold") |
| 655 |
alert_diskio_enabled = module.params.get("alert_diskio_enabled") |
| 656 |
alert_diskio_threshold = module.params.get("alert_diskio_threshold") |
| 657 |
backupweeklyday = module.params.get("backupweeklyday") |
| 658 |
backupwindow = module.params.get("backupwindow") |
| 659 |
displaygroup = module.params.get("displaygroup") |
| 660 |
plan = module.params.get("plan") |
| 661 |
additional_disks = module.params.get("additional_disks") |
| 662 |
distribution = module.params.get("distribution") |
| 663 |
datacenter = module.params.get("datacenter") |
| 664 |
kernel_id = module.params.get("kernel_id") |
| 665 |
linode_id = module.params.get("linode_id") |
| 666 |
payment_term = module.params.get("payment_term") |
| 667 |
password = module.params.get("password") |
| 668 |
private_ip = module.params.get("private_ip") |
| 669 |
ssh_pub_key = module.params.get("ssh_pub_key") |
| 670 |
swap = module.params.get("swap") |
| 671 |
wait = module.params.get("wait") |
| 672 |
wait_timeout = module.params.get("wait_timeout") |
| 673 |
watchdog = int(module.params.get("watchdog")) |
| 674 |
|
| 675 |
check_items = dict( |
| 676 |
alert_bwin_enabled=alert_bwin_enabled, |
| 677 |
alert_bwin_threshold=alert_bwin_threshold, |
| 678 |
alert_bwout_enabled=alert_bwout_enabled, |
| 679 |
alert_bwout_threshold=alert_bwout_threshold, |
| 680 |
alert_bwquota_enabled=alert_bwquota_enabled, |
| 681 |
alert_bwquota_threshold=alert_bwquota_threshold, |
| 682 |
alert_cpu_enabled=alert_cpu_enabled, |
| 683 |
alert_cpu_threshold=alert_cpu_threshold, |
| 684 |
alert_diskio_enabled=alert_diskio_enabled, |
| 685 |
alert_diskio_threshold=alert_diskio_threshold, |
| 686 |
backupweeklyday=backupweeklyday, |
| 687 |
backupwindow=backupwindow, |
| 688 |
) |
| 689 |
|
| 690 |
kwargs = {k: v for k, v in check_items.items() if v is not None} |
| 691 |
|
| 692 |
# setup the auth |
| 693 |
try: |
| 694 |
api = linode_api.Api(api_key) |
| 695 |
api.test_echo() |
| 696 |
except Exception as e: |
| 697 |
module.fail_json(msg=f"{e.value[0]['ERRORMESSAGE']}", exception=traceback.format_exc()) |
| 698 |
|
| 699 |
linodeServers( |
| 700 |
module, |
| 701 |
api, |
| 702 |
state, |
| 703 |
name, |
| 704 |
displaygroup, |
| 705 |
plan, |
| 706 |
additional_disks, |
| 707 |
distribution, |
| 708 |
datacenter, |
| 709 |
kernel_id, |
| 710 |
linode_id, |
| 711 |
payment_term, |
| 712 |
password, |
| 713 |
private_ip, |
| 714 |
ssh_pub_key, |
| 715 |
swap, |
| 716 |
wait, |
| 717 |
wait_timeout, |
| 718 |
watchdog, |
| 719 |
**kwargs, |
| 720 |
) |
| 721 |
|
| 722 |
|
| 723 |
if __name__ == "__main__": |
| 724 |
main() |