| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017-present Alibaba Group Holding Limited. He Guimin <[email protected]> |
| 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 |
# This file is part of Ansible |
| 8 |
# |
| 9 |
# Ansible is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# Ansible is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with Ansible. If not, see http://www.gnu.org/licenses/. |
| 21 |
|
| 22 |
from __future__ import annotations |
| 23 |
|
| 24 |
DOCUMENTATION = r""" |
| 25 |
module: ali_instance |
| 26 |
short_description: Create, Start, Stop, Restart or Terminate an Instance in ECS; Add or Remove Instance to/from a Security |
| 27 |
Group |
| 28 |
description: |
| 29 |
- Create, start, stop, restart, modify or terminate ECS instances. |
| 30 |
- Add or remove ecs instances to/from security group. |
| 31 |
attributes: |
| 32 |
check_mode: |
| 33 |
support: none |
| 34 |
diff_mode: |
| 35 |
support: none |
| 36 |
options: |
| 37 |
state: |
| 38 |
description: |
| 39 |
- The state of the instance after operating. |
| 40 |
default: 'present' |
| 41 |
choices: ['present', 'running', 'stopped', 'restarted', 'absent'] |
| 42 |
type: str |
| 43 |
availability_zone: |
| 44 |
description: |
| 45 |
- Aliyun availability zone ID in which to launch the instance. If it is not specified, it is allocated by system automatically. |
| 46 |
aliases: ['alicloud_zone', 'zone_id'] |
| 47 |
type: str |
| 48 |
image_id: |
| 49 |
description: |
| 50 |
- Image ID used to launch instances. Required when O(state=present) and creating new ECS instances. |
| 51 |
aliases: ['image'] |
| 52 |
type: str |
| 53 |
instance_type: |
| 54 |
description: |
| 55 |
- Instance type used to launch instances. Required when O(state=present) and creating new ECS instances. |
| 56 |
aliases: ['type'] |
| 57 |
type: str |
| 58 |
security_groups: |
| 59 |
description: |
| 60 |
- A list of security group IDs. |
| 61 |
aliases: ['group_ids'] |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
vswitch_id: |
| 65 |
description: |
| 66 |
- The subnet ID in which to launch the instances (VPC). |
| 67 |
aliases: ['subnet_id'] |
| 68 |
type: str |
| 69 |
instance_name: |
| 70 |
description: |
| 71 |
- The name of ECS instance, which is a string of 2 to 128 Chinese or English characters. It must begin with an uppercase/lowercase |
| 72 |
letter or a Chinese character and can contain numerals, V(.), V(_) or V(-). It cannot begin with V(http://) or V(https://). |
| 73 |
aliases: ['name'] |
| 74 |
type: str |
| 75 |
description: |
| 76 |
description: |
| 77 |
- The description of ECS instance, which is a string of 2 to 256 characters. It cannot begin with V(http://) or V(https://). |
| 78 |
type: str |
| 79 |
internet_charge_type: |
| 80 |
description: |
| 81 |
- Internet charge type of ECS instance. |
| 82 |
default: 'PayByBandwidth' |
| 83 |
choices: ['PayByBandwidth', 'PayByTraffic'] |
| 84 |
type: str |
| 85 |
max_bandwidth_in: |
| 86 |
description: |
| 87 |
- Maximum incoming bandwidth from the public network, measured in Mbps (Megabits per second). |
| 88 |
default: 200 |
| 89 |
type: int |
| 90 |
max_bandwidth_out: |
| 91 |
description: |
| 92 |
- Maximum outgoing bandwidth to the public network, measured in Mbps (Megabits per second). Required when O(allocate_public_ip=true). |
| 93 |
Ignored when O(allocate_public_ip=false). |
| 94 |
default: 0 |
| 95 |
type: int |
| 96 |
host_name: |
| 97 |
description: |
| 98 |
- Instance host name. Ordered hostname is not supported. |
| 99 |
type: str |
| 100 |
unique_suffix: |
| 101 |
description: |
| 102 |
- Specifies whether to add sequential suffixes to the host_name. The sequential suffix ranges from 001 to 999. |
| 103 |
default: false |
| 104 |
type: bool |
| 105 |
version_added: '0.2.0' |
| 106 |
password: |
| 107 |
description: |
| 108 |
- The password to login instance. After rebooting instances, modified password is effective. |
| 109 |
type: str |
| 110 |
system_disk_category: |
| 111 |
description: |
| 112 |
- Category of the system disk. |
| 113 |
default: 'cloud_efficiency' |
| 114 |
choices: ['cloud_efficiency', 'cloud_ssd'] |
| 115 |
type: str |
| 116 |
system_disk_size: |
| 117 |
description: |
| 118 |
- Size of the system disk, in GB. The valid values are V(40)~V(500). |
| 119 |
default: 40 |
| 120 |
type: int |
| 121 |
system_disk_name: |
| 122 |
description: |
| 123 |
- Name of the system disk. |
| 124 |
type: str |
| 125 |
system_disk_description: |
| 126 |
description: |
| 127 |
- Description of the system disk. |
| 128 |
type: str |
| 129 |
count: |
| 130 |
description: |
| 131 |
- The number of the new instance. An integer value which indicates how many instances that match O(count_tag) should |
| 132 |
be running. Instances are either created or terminated based on this value. |
| 133 |
default: 1 |
| 134 |
type: int |
| 135 |
count_tag: |
| 136 |
description: |
| 137 |
- O(count) determines how many instances based on a specific tag criteria should be present. This can be expressed in |
| 138 |
multiple ways and is shown in the EXAMPLES section. The specified count_tag must already exist or be passed in as |
| 139 |
the O(tags) option. If it is not specified, it is replaced by O(instance_name). |
| 140 |
type: str |
| 141 |
allocate_public_ip: |
| 142 |
description: |
| 143 |
- Whether allocate a public IP for the new instance. |
| 144 |
default: false |
| 145 |
aliases: ['assign_public_ip'] |
| 146 |
type: bool |
| 147 |
instance_charge_type: |
| 148 |
description: |
| 149 |
- The charge type of the instance. |
| 150 |
choices: ['PrePaid', 'PostPaid'] |
| 151 |
default: 'PostPaid' |
| 152 |
type: str |
| 153 |
period: |
| 154 |
description: |
| 155 |
- The charge duration of the instance, in months. Required when O(instance_charge_type=PrePaid). |
| 156 |
- The valid value are [V(1-9), V(12), V(24), V(36)]. |
| 157 |
default: 1 |
| 158 |
type: int |
| 159 |
auto_renew: |
| 160 |
description: |
| 161 |
- Whether automate renew the charge of the instance. |
| 162 |
type: bool |
| 163 |
default: false |
| 164 |
auto_renew_period: |
| 165 |
description: |
| 166 |
- The duration of the automatic renew the charge of the instance. Required when O(auto_renew=true). |
| 167 |
choices: [1, 2, 3, 6, 12] |
| 168 |
type: int |
| 169 |
instance_ids: |
| 170 |
description: |
| 171 |
- A list of instance IDs. It is required when need to operate existing instances. If it is specified, O(count) is ignored. |
| 172 |
type: list |
| 173 |
elements: str |
| 174 |
force: |
| 175 |
description: |
| 176 |
- Whether the current operation needs to be execute forcibly. |
| 177 |
default: false |
| 178 |
type: bool |
| 179 |
tags: |
| 180 |
description: |
| 181 |
- A hash/dictionaries of instance tags, to add to the new instance or for starting/stopping instance by tag. V({"key":"value"}). |
| 182 |
aliases: ["instance_tags"] |
| 183 |
type: dict |
| 184 |
version_added: '0.2.0' |
| 185 |
purge_tags: |
| 186 |
description: |
| 187 |
- Delete any tags not specified in the task that are on the instance. If V(true), it means you have to specify all the |
| 188 |
desired tags on each task affecting an instance. |
| 189 |
default: false |
| 190 |
type: bool |
| 191 |
version_added: '0.2.0' |
| 192 |
key_name: |
| 193 |
description: |
| 194 |
- The name of key pair which is used to access ECS instance in SSH. |
| 195 |
type: str |
| 196 |
aliases: ['keypair'] |
| 197 |
user_data: |
| 198 |
description: |
| 199 |
- User-defined data to customize the startup behaviors of an ECS instance and to pass data into an ECS instance. It |
| 200 |
only takes effect when launching the new ECS instances. |
| 201 |
type: str |
| 202 |
ram_role_name: |
| 203 |
description: |
| 204 |
- The name of the instance RAM role. |
| 205 |
type: str |
| 206 |
version_added: '0.2.0' |
| 207 |
spot_price_limit: |
| 208 |
description: |
| 209 |
- The maximum hourly price for the preemptible instance. This parameter supports a maximum of three decimal places and |
| 210 |
takes effect when the SpotStrategy parameter is set to SpotWithPriceLimit. |
| 211 |
type: float |
| 212 |
version_added: '0.2.0' |
| 213 |
spot_strategy: |
| 214 |
description: |
| 215 |
- The bidding mode of the pay-as-you-go instance. This parameter is valid when O(instance_charge_type=PostPaid). |
| 216 |
choices: ['NoSpot', 'SpotWithPriceLimit', 'SpotAsPriceGo'] |
| 217 |
default: 'NoSpot' |
| 218 |
type: str |
| 219 |
version_added: '0.2.0' |
| 220 |
period_unit: |
| 221 |
description: |
| 222 |
- The duration unit that you are buying the resource. It is valid when O(instance_charge_type=PrePaid). |
| 223 |
choices: ['Month', 'Week'] |
| 224 |
default: 'Month' |
| 225 |
type: str |
| 226 |
version_added: '0.2.0' |
| 227 |
dry_run: |
| 228 |
description: |
| 229 |
- Specifies whether to send a dry-run request. |
| 230 |
- If O(dry_run=true), Only a dry-run request is sent and no instance is created. The system checks whether the required |
| 231 |
parameters are set, and validates the request format, service permissions, and available ECS instances. If the validation |
| 232 |
fails, the corresponding error code is returned. If the validation succeeds, the DryRunOperation error code is returned. |
| 233 |
- If O(dry_run=false), a request is sent. If the validation succeeds, the instance is created. |
| 234 |
default: false |
| 235 |
type: bool |
| 236 |
version_added: '0.2.0' |
| 237 |
include_data_disks: |
| 238 |
description: |
| 239 |
- Whether to change instance disks charge type when changing instance charge type. |
| 240 |
default: true |
| 241 |
type: bool |
| 242 |
version_added: '0.2.0' |
| 243 |
author: |
| 244 |
- "He Guimin (@xiaozhu36)" |
| 245 |
requirements: |
| 246 |
- "Python >= 3.6" |
| 247 |
- "footmark >= 1.19.0" |
| 248 |
extends_documentation_fragment: |
| 249 |
- community.general._alicloud |
| 250 |
- community.general._attributes |
| 251 |
""" |
| 252 |
|
| 253 |
EXAMPLES = r""" |
| 254 |
# basic provisioning example vpc network |
| 255 |
- name: Basic provisioning example |
| 256 |
hosts: localhost |
| 257 |
vars: |
| 258 |
alicloud_access_key: <your-alicloud-access-key-id> |
| 259 |
alicloud_secret_key: <your-alicloud-access-secret-key> |
| 260 |
alicloud_region: cn-beijing |
| 261 |
image: ubuntu1404_64_40G_cloudinit_20160727.raw |
| 262 |
instance_type: ecs.n4.small |
| 263 |
vswitch_id: vsw-abcd1234 |
| 264 |
assign_public_ip: true |
| 265 |
max_bandwidth_out: 10 |
| 266 |
host_name: myhost |
| 267 |
password: mypassword |
| 268 |
system_disk_category: cloud_efficiency |
| 269 |
system_disk_size: 100 |
| 270 |
internet_charge_type: PayByBandwidth |
| 271 |
security_groups: ["sg-f2rwnfh23r"] |
| 272 |
|
| 273 |
instance_ids: ["i-abcd12346", "i-abcd12345"] |
| 274 |
force: true |
| 275 |
|
| 276 |
tasks: |
| 277 |
- name: Launch ECS instance in VPC network |
| 278 |
community.general.ali_instance: |
| 279 |
alicloud_access_key: '{{ alicloud_access_key }}' |
| 280 |
alicloud_secret_key: '{{ alicloud_secret_key }}' |
| 281 |
alicloud_region: '{{ alicloud_region }}' |
| 282 |
image: '{{ image }}' |
| 283 |
system_disk_category: '{{ system_disk_category }}' |
| 284 |
system_disk_size: '{{ system_disk_size }}' |
| 285 |
instance_type: '{{ instance_type }}' |
| 286 |
vswitch_id: '{{ vswitch_id }}' |
| 287 |
assign_public_ip: '{{ assign_public_ip }}' |
| 288 |
internet_charge_type: '{{ internet_charge_type }}' |
| 289 |
max_bandwidth_out: '{{ max_bandwidth_out }}' |
| 290 |
tags: |
| 291 |
Name: created_one |
| 292 |
host_name: '{{ host_name }}' |
| 293 |
password: '{{ password }}' |
| 294 |
|
| 295 |
- name: With count and count_tag to create a number of instances |
| 296 |
community.general.ali_instance: |
| 297 |
alicloud_access_key: '{{ alicloud_access_key }}' |
| 298 |
alicloud_secret_key: '{{ alicloud_secret_key }}' |
| 299 |
alicloud_region: '{{ alicloud_region }}' |
| 300 |
image: '{{ image }}' |
| 301 |
system_disk_category: '{{ system_disk_category }}' |
| 302 |
system_disk_size: '{{ system_disk_size }}' |
| 303 |
instance_type: '{{ instance_type }}' |
| 304 |
assign_public_ip: '{{ assign_public_ip }}' |
| 305 |
security_groups: '{{ security_groups }}' |
| 306 |
internet_charge_type: '{{ internet_charge_type }}' |
| 307 |
max_bandwidth_out: '{{ max_bandwidth_out }}' |
| 308 |
tags: |
| 309 |
Name: created_one |
| 310 |
Version: 0.1 |
| 311 |
count: 2 |
| 312 |
count_tag: |
| 313 |
Name: created_one |
| 314 |
host_name: '{{ host_name }}' |
| 315 |
password: '{{ password }}' |
| 316 |
|
| 317 |
- name: Start instance |
| 318 |
community.general.ali_instance: |
| 319 |
alicloud_access_key: '{{ alicloud_access_key }}' |
| 320 |
alicloud_secret_key: '{{ alicloud_secret_key }}' |
| 321 |
alicloud_region: '{{ alicloud_region }}' |
| 322 |
instance_ids: '{{ instance_ids }}' |
| 323 |
state: 'running' |
| 324 |
|
| 325 |
- name: Reboot instance forcibly |
| 326 |
ecs: |
| 327 |
alicloud_access_key: '{{ alicloud_access_key }}' |
| 328 |
alicloud_secret_key: '{{ alicloud_secret_key }}' |
| 329 |
alicloud_region: '{{ alicloud_region }}' |
| 330 |
instance_ids: '{{ instance_ids }}' |
| 331 |
state: 'restarted' |
| 332 |
force: '{{ force }}' |
| 333 |
|
| 334 |
- name: Add instances to an security group |
| 335 |
ecs: |
| 336 |
alicloud_access_key: '{{ alicloud_access_key }}' |
| 337 |
alicloud_secret_key: '{{ alicloud_secret_key }}' |
| 338 |
alicloud_region: '{{ alicloud_region }}' |
| 339 |
instance_ids: '{{ instance_ids }}' |
| 340 |
security_groups: '{{ security_groups }}' |
| 341 |
""" |
| 342 |
|
| 343 |
RETURN = r""" |
| 344 |
instances: |
| 345 |
description: List of ECS instances. |
| 346 |
returned: always |
| 347 |
type: list |
| 348 |
elements: dict |
| 349 |
contains: |
| 350 |
availability_zone: |
| 351 |
description: The availability zone of the instance is in. |
| 352 |
returned: always |
| 353 |
type: str |
| 354 |
sample: cn-beijing-a |
| 355 |
block_device_mappings: |
| 356 |
description: Any block device mapping entries for the instance. |
| 357 |
returned: always |
| 358 |
type: complex |
| 359 |
contains: |
| 360 |
device_name: |
| 361 |
description: The device name exposed to the instance. |
| 362 |
returned: always |
| 363 |
type: str |
| 364 |
sample: /dev/xvda |
| 365 |
attach_time: |
| 366 |
description: The time stamp when the attachment initiated. |
| 367 |
returned: always |
| 368 |
type: str |
| 369 |
sample: "2018-06-25T04:08:26Z" |
| 370 |
delete_on_termination: |
| 371 |
description: Indicates whether the volume is deleted on instance termination. |
| 372 |
returned: always |
| 373 |
type: bool |
| 374 |
sample: true |
| 375 |
status: |
| 376 |
description: The attachment state. |
| 377 |
returned: always |
| 378 |
type: str |
| 379 |
sample: in_use |
| 380 |
volume_id: |
| 381 |
description: The ID of the cloud disk. |
| 382 |
returned: always |
| 383 |
type: str |
| 384 |
sample: d-2zei53pjsi117y6gf9t6 |
| 385 |
cpu: |
| 386 |
description: The CPU core count of the instance. |
| 387 |
returned: always |
| 388 |
type: int |
| 389 |
sample: 4 |
| 390 |
creation_time: |
| 391 |
description: The time the instance was created. |
| 392 |
returned: always |
| 393 |
type: str |
| 394 |
sample: "2018-06-25T04:08Z" |
| 395 |
description: |
| 396 |
description: The instance description. |
| 397 |
returned: always |
| 398 |
type: str |
| 399 |
sample: "my ansible instance" |
| 400 |
eip: |
| 401 |
description: The attribution of EIP associated with the instance. |
| 402 |
returned: always |
| 403 |
type: complex |
| 404 |
contains: |
| 405 |
allocation_id: |
| 406 |
description: The ID of the EIP. |
| 407 |
returned: always |
| 408 |
type: str |
| 409 |
sample: eip-12345 |
| 410 |
internet_charge_type: |
| 411 |
description: The internet charge type of the EIP. |
| 412 |
returned: always |
| 413 |
type: str |
| 414 |
sample: "paybybandwidth" |
| 415 |
ip_address: |
| 416 |
description: EIP address. |
| 417 |
returned: always |
| 418 |
type: str |
| 419 |
sample: 42.10.2.2 |
| 420 |
expired_time: |
| 421 |
description: The time the instance expires. |
| 422 |
returned: always |
| 423 |
type: str |
| 424 |
sample: "2099-12-31T15:59Z" |
| 425 |
gpu: |
| 426 |
description: The attribution of instance GPU. |
| 427 |
returned: always |
| 428 |
type: complex |
| 429 |
contains: |
| 430 |
amount: |
| 431 |
description: The count of the GPU. |
| 432 |
returned: always |
| 433 |
type: int |
| 434 |
sample: 0 |
| 435 |
spec: |
| 436 |
description: The specification of the GPU. |
| 437 |
returned: always |
| 438 |
type: str |
| 439 |
sample: "" |
| 440 |
host_name: |
| 441 |
description: The host name of the instance. |
| 442 |
returned: always |
| 443 |
type: str |
| 444 |
sample: iZ2zewaoZ |
| 445 |
id: |
| 446 |
description: Alias of instance_id. |
| 447 |
returned: always |
| 448 |
type: str |
| 449 |
sample: i-abc12345 |
| 450 |
instance_id: |
| 451 |
description: ECS instance resource ID. |
| 452 |
returned: always |
| 453 |
type: str |
| 454 |
sample: i-abc12345 |
| 455 |
image_id: |
| 456 |
description: The ID of the image used to launch the instance. |
| 457 |
returned: always |
| 458 |
type: str |
| 459 |
sample: m-0011223344 |
| 460 |
inner_ip_address: |
| 461 |
description: The inner IPv4 address of the classic instance. |
| 462 |
returned: always |
| 463 |
type: str |
| 464 |
sample: 10.0.0.2 |
| 465 |
instance_charge_type: |
| 466 |
description: The instance charge type. |
| 467 |
returned: always |
| 468 |
type: str |
| 469 |
sample: PostPaid |
| 470 |
instance_name: |
| 471 |
description: The name of the instance. |
| 472 |
returned: always |
| 473 |
type: str |
| 474 |
sample: my-ecs |
| 475 |
instance_type: |
| 476 |
description: The instance type of the running instance. |
| 477 |
returned: always |
| 478 |
type: str |
| 479 |
sample: ecs.sn1ne.xlarge |
| 480 |
instance_type_family: |
| 481 |
description: The instance type family of the instance belongs. |
| 482 |
returned: always |
| 483 |
type: str |
| 484 |
sample: ecs.sn1ne |
| 485 |
internet_charge_type: |
| 486 |
description: The billing method of the network bandwidth. |
| 487 |
returned: always |
| 488 |
type: str |
| 489 |
sample: PayByBandwidth |
| 490 |
internet_max_bandwidth_in: |
| 491 |
description: Maximum incoming bandwidth from the internet network. |
| 492 |
returned: always |
| 493 |
type: int |
| 494 |
sample: 200 |
| 495 |
internet_max_bandwidth_out: |
| 496 |
description: Maximum incoming bandwidth from the internet network. |
| 497 |
returned: always |
| 498 |
type: int |
| 499 |
sample: 20 |
| 500 |
io_optimized: |
| 501 |
description: Indicates whether the instance is optimized for EBS I/O. |
| 502 |
returned: always |
| 503 |
type: bool |
| 504 |
sample: false |
| 505 |
memory: |
| 506 |
description: Memory size of the instance. |
| 507 |
returned: always |
| 508 |
type: int |
| 509 |
sample: 8192 |
| 510 |
network_interfaces: |
| 511 |
description: One or more network interfaces for the instance. |
| 512 |
returned: always |
| 513 |
type: complex |
| 514 |
contains: |
| 515 |
mac_address: |
| 516 |
description: The MAC address. |
| 517 |
returned: always |
| 518 |
type: str |
| 519 |
sample: "00:11:22:33:44:55" |
| 520 |
network_interface_id: |
| 521 |
description: The ID of the network interface. |
| 522 |
returned: always |
| 523 |
type: str |
| 524 |
sample: eni-01234567 |
| 525 |
primary_ip_address: |
| 526 |
description: The primary IPv4 address of the network interface within the vswitch. |
| 527 |
returned: always |
| 528 |
type: str |
| 529 |
sample: 10.0.0.1 |
| 530 |
osname: |
| 531 |
description: The operation system name of the instance owned. |
| 532 |
returned: always |
| 533 |
type: str |
| 534 |
sample: CentOS |
| 535 |
ostype: |
| 536 |
description: The operation system type of the instance owned. |
| 537 |
returned: always |
| 538 |
type: str |
| 539 |
sample: linux |
| 540 |
private_ip_address: |
| 541 |
description: The IPv4 address of the network interface within the subnet. |
| 542 |
returned: always |
| 543 |
type: str |
| 544 |
sample: 10.0.0.1 |
| 545 |
public_ip_address: |
| 546 |
description: The public IPv4 address assigned to the instance or eip address. |
| 547 |
returned: always |
| 548 |
type: str |
| 549 |
sample: 43.0.0.1 |
| 550 |
resource_group_id: |
| 551 |
description: The ID of the resource group to which the instance belongs. |
| 552 |
returned: always |
| 553 |
type: str |
| 554 |
sample: my-ecs-group |
| 555 |
security_groups: |
| 556 |
description: One or more security groups for the instance. |
| 557 |
returned: always |
| 558 |
type: list |
| 559 |
elements: dict |
| 560 |
contains: |
| 561 |
group_id: |
| 562 |
description: The ID of the security group. |
| 563 |
returned: always |
| 564 |
type: str |
| 565 |
sample: sg-0123456 |
| 566 |
group_name: |
| 567 |
description: The name of the security group. |
| 568 |
returned: always |
| 569 |
type: str |
| 570 |
sample: my-security-group |
| 571 |
status: |
| 572 |
description: The current status of the instance. |
| 573 |
returned: always |
| 574 |
type: str |
| 575 |
sample: running |
| 576 |
tags: |
| 577 |
description: Any tags assigned to the instance. |
| 578 |
returned: always |
| 579 |
type: dict |
| 580 |
sample: |
| 581 |
user_data: |
| 582 |
description: User-defined data. |
| 583 |
returned: always |
| 584 |
type: dict |
| 585 |
sample: |
| 586 |
vswitch_id: |
| 587 |
description: The ID of the vswitch in which the instance is running. |
| 588 |
returned: always |
| 589 |
type: str |
| 590 |
sample: vsw-dew00abcdef |
| 591 |
vpc_id: |
| 592 |
description: The ID of the VPC the instance is in. |
| 593 |
returned: always |
| 594 |
type: str |
| 595 |
sample: vpc-0011223344 |
| 596 |
spot_price_limit: |
| 597 |
description: |
| 598 |
- The maximum hourly price for the preemptible instance. |
| 599 |
returned: always |
| 600 |
type: float |
| 601 |
sample: 0.97 |
| 602 |
spot_strategy: |
| 603 |
description: |
| 604 |
- The bidding mode of the pay-as-you-go instance. |
| 605 |
returned: always |
| 606 |
type: str |
| 607 |
sample: NoSpot |
| 608 |
ids: |
| 609 |
description: List of ECS instance IDs. |
| 610 |
returned: always |
| 611 |
type: list |
| 612 |
sample: ["i-12345er", "i-3245fs"] |
| 613 |
""" |
| 614 |
|
| 615 |
import re |
| 616 |
import time |
| 617 |
|
| 618 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 619 |
|
| 620 |
from ansible_collections.community.general.plugins.module_utils._alicloud_ecs import ( |
| 621 |
FOOTMARK_IMP_ERR, |
| 622 |
HAS_FOOTMARK, |
| 623 |
ecs_argument_spec, |
| 624 |
ecs_connect, |
| 625 |
) |
| 626 |
|
| 627 |
|
| 628 |
def get_instances_info(connection, ids): |
| 629 |
result = [] |
| 630 |
instances = connection.describe_instances(instance_ids=ids) |
| 631 |
if len(instances) > 0: |
| 632 |
for inst in instances: |
| 633 |
volumes = connection.describe_disks(instance_id=inst.id) |
| 634 |
inst.block_device_mappings = volumes |
| 635 |
inst.user_data = inst.describe_user_data() |
| 636 |
result.append(inst.read()) |
| 637 |
return result |
| 638 |
|
| 639 |
|
| 640 |
def run_instance(module, ecs, exact_count): |
| 641 |
if exact_count <= 0: |
| 642 |
return None |
| 643 |
zone_id = module.params["availability_zone"] |
| 644 |
image_id = module.params["image_id"] |
| 645 |
instance_type = module.params["instance_type"] |
| 646 |
security_groups = module.params["security_groups"] |
| 647 |
vswitch_id = module.params["vswitch_id"] |
| 648 |
instance_name = module.params["instance_name"] |
| 649 |
description = module.params["description"] |
| 650 |
internet_charge_type = module.params["internet_charge_type"] |
| 651 |
max_bandwidth_out = module.params["max_bandwidth_out"] |
| 652 |
max_bandwidth_in = module.params["max_bandwidth_in"] |
| 653 |
host_name = module.params["host_name"] |
| 654 |
password = module.params["password"] |
| 655 |
system_disk_category = module.params["system_disk_category"] |
| 656 |
system_disk_size = module.params["system_disk_size"] |
| 657 |
system_disk_name = module.params["system_disk_name"] |
| 658 |
system_disk_description = module.params["system_disk_description"] |
| 659 |
# allocate_public_ip = module.params["allocate_public_ip"] TODO - this is unused! |
| 660 |
period = module.params["period"] |
| 661 |
auto_renew = module.params["auto_renew"] |
| 662 |
instance_charge_type = module.params["instance_charge_type"] |
| 663 |
auto_renew_period = module.params["auto_renew_period"] |
| 664 |
user_data = module.params["user_data"] |
| 665 |
key_name = module.params["key_name"] |
| 666 |
ram_role_name = module.params["ram_role_name"] |
| 667 |
spot_price_limit = module.params["spot_price_limit"] |
| 668 |
spot_strategy = module.params["spot_strategy"] |
| 669 |
unique_suffix = module.params["unique_suffix"] |
| 670 |
# check whether the required parameter passed or not |
| 671 |
if not image_id: |
| 672 |
module.fail_json(msg="image_id is required for new instance") |
| 673 |
if not instance_type: |
| 674 |
module.fail_json(msg="instance_type is required for new instance") |
| 675 |
if not isinstance(security_groups, list): |
| 676 |
module.fail_json(msg="The parameter security_groups should be a list, aborting") |
| 677 |
if len(security_groups) <= 0: |
| 678 |
module.fail_json( |
| 679 |
msg="Expected the parameter security_groups is non-empty when create new ECS instances, aborting" |
| 680 |
) |
| 681 |
|
| 682 |
client_token = f"Ansible-Alicloud-{hash(str(module.params))}-{time.time()}" |
| 683 |
|
| 684 |
try: |
| 685 |
# call to create_instance method from footmark |
| 686 |
instances = ecs.run_instances( |
| 687 |
image_id=image_id, |
| 688 |
instance_type=instance_type, |
| 689 |
security_group_id=security_groups[0], |
| 690 |
zone_id=zone_id, |
| 691 |
instance_name=instance_name, |
| 692 |
description=description, |
| 693 |
internet_charge_type=internet_charge_type, |
| 694 |
internet_max_bandwidth_out=max_bandwidth_out, |
| 695 |
internet_max_bandwidth_in=max_bandwidth_in, |
| 696 |
host_name=host_name, |
| 697 |
password=password, |
| 698 |
io_optimized="optimized", |
| 699 |
system_disk_category=system_disk_category, |
| 700 |
system_disk_size=system_disk_size, |
| 701 |
system_disk_disk_name=system_disk_name, |
| 702 |
system_disk_description=system_disk_description, |
| 703 |
vswitch_id=vswitch_id, |
| 704 |
amount=exact_count, |
| 705 |
instance_charge_type=instance_charge_type, |
| 706 |
period=period, |
| 707 |
period_unit="Month", |
| 708 |
auto_renew=auto_renew, |
| 709 |
auto_renew_period=auto_renew_period, |
| 710 |
key_pair_name=key_name, |
| 711 |
user_data=user_data, |
| 712 |
client_token=client_token, |
| 713 |
ram_role_name=ram_role_name, |
| 714 |
spot_price_limit=spot_price_limit, |
| 715 |
spot_strategy=spot_strategy, |
| 716 |
unique_suffix=unique_suffix, |
| 717 |
) |
| 718 |
|
| 719 |
except Exception as e: |
| 720 |
module.fail_json(msg=f"Unable to create instance, error: {e}") |
| 721 |
|
| 722 |
return instances |
| 723 |
|
| 724 |
|
| 725 |
def modify_instance(module, instance): |
| 726 |
# According to state to modify instance's some special attribute |
| 727 |
state = module.params["state"] |
| 728 |
name = module.params["instance_name"] |
| 729 |
unique_suffix = module.params["unique_suffix"] |
| 730 |
if not name: |
| 731 |
name = instance.name |
| 732 |
|
| 733 |
description = module.params["description"] |
| 734 |
if not description: |
| 735 |
description = instance.description |
| 736 |
|
| 737 |
host_name = module.params["host_name"] |
| 738 |
if unique_suffix and host_name: |
| 739 |
suffix = instance.host_name[-3:] |
| 740 |
host_name = host_name + suffix |
| 741 |
|
| 742 |
if not host_name: |
| 743 |
host_name = instance.host_name |
| 744 |
|
| 745 |
# password can be modified only when restart instance |
| 746 |
password = "" |
| 747 |
if state == "restarted": |
| 748 |
password = module.params["password"] |
| 749 |
|
| 750 |
# userdata can be modified only when instance is stopped |
| 751 |
instance.user_data = instance.describe_user_data() |
| 752 |
user_data = instance.user_data |
| 753 |
if state == "stopped": |
| 754 |
user_data = module.params["user_data"].encode() |
| 755 |
|
| 756 |
try: |
| 757 |
return instance.modify( |
| 758 |
name=name, description=description, host_name=host_name, password=password, user_data=user_data |
| 759 |
) |
| 760 |
except Exception as e: |
| 761 |
module.fail_json(msg=f"Modify instance {instance.id} attribute got an error: {e}") |
| 762 |
|
| 763 |
|
| 764 |
def wait_for_instance_modify_charge(ecs, instance_ids, charge_type, delay=10, timeout=300): |
| 765 |
""" |
| 766 |
To verify instance charge type has become expected after modify instance charge type |
| 767 |
""" |
| 768 |
try: |
| 769 |
while True: |
| 770 |
instances = ecs.describe_instances(instance_ids=instance_ids) |
| 771 |
flag = True |
| 772 |
for inst in instances: |
| 773 |
if inst and inst.instance_charge_type != charge_type: |
| 774 |
flag = False |
| 775 |
if flag: |
| 776 |
return |
| 777 |
timeout -= delay |
| 778 |
time.sleep(delay) |
| 779 |
if timeout <= 0: |
| 780 |
raise Exception(f"Timeout Error: Waiting for instance to {charge_type}. ") |
| 781 |
except Exception as e: |
| 782 |
raise e |
| 783 |
|
| 784 |
|
| 785 |
def main(): |
| 786 |
argument_spec = ecs_argument_spec() |
| 787 |
argument_spec.update( |
| 788 |
dict( |
| 789 |
security_groups=dict(type="list", elements="str", aliases=["group_ids"]), |
| 790 |
availability_zone=dict(type="str", aliases=["alicloud_zone", "zone_id"]), |
| 791 |
instance_type=dict(type="str", aliases=["type"]), |
| 792 |
image_id=dict(type="str", aliases=["image"]), |
| 793 |
count=dict(type="int", default=1), |
| 794 |
count_tag=dict(type="str"), |
| 795 |
vswitch_id=dict(type="str", aliases=["subnet_id"]), |
| 796 |
instance_name=dict(type="str", aliases=["name"]), |
| 797 |
host_name=dict(type="str"), |
| 798 |
password=dict(type="str", no_log=True), |
| 799 |
internet_charge_type=dict(type="str", default="PayByBandwidth", choices=["PayByBandwidth", "PayByTraffic"]), |
| 800 |
max_bandwidth_in=dict(type="int", default=200), |
| 801 |
max_bandwidth_out=dict(type="int", default=0), |
| 802 |
system_disk_category=dict( |
| 803 |
type="str", default="cloud_efficiency", choices=["cloud_efficiency", "cloud_ssd"] |
| 804 |
), |
| 805 |
system_disk_size=dict(type="int", default=40), |
| 806 |
system_disk_name=dict(type="str"), |
| 807 |
system_disk_description=dict(type="str"), |
| 808 |
force=dict(type="bool", default=False), |
| 809 |
tags=dict(type="dict", aliases=["instance_tags"]), |
| 810 |
purge_tags=dict(type="bool", default=False), |
| 811 |
state=dict(default="present", choices=["present", "running", "stopped", "restarted", "absent"]), |
| 812 |
description=dict(type="str"), |
| 813 |
allocate_public_ip=dict(type="bool", aliases=["assign_public_ip"], default=False), |
| 814 |
instance_charge_type=dict(type="str", default="PostPaid", choices=["PrePaid", "PostPaid"]), |
| 815 |
period=dict(type="int", default=1), |
| 816 |
auto_renew=dict(type="bool", default=False), |
| 817 |
instance_ids=dict(type="list", elements="str"), |
| 818 |
auto_renew_period=dict(type="int", choices=[1, 2, 3, 6, 12]), |
| 819 |
key_name=dict(type="str", aliases=["keypair"]), |
| 820 |
user_data=dict(type="str"), |
| 821 |
ram_role_name=dict(type="str"), |
| 822 |
spot_price_limit=dict(type="float"), |
| 823 |
spot_strategy=dict(type="str", default="NoSpot", choices=["NoSpot", "SpotWithPriceLimit", "SpotAsPriceGo"]), |
| 824 |
unique_suffix=dict(type="bool", default=False), |
| 825 |
period_unit=dict(type="str", default="Month", choices=["Month", "Week"]), |
| 826 |
dry_run=dict(type="bool", default=False), |
| 827 |
include_data_disks=dict(type="bool", default=True), |
| 828 |
) |
| 829 |
) |
| 830 |
module = AnsibleModule(argument_spec=argument_spec) |
| 831 |
|
| 832 |
if HAS_FOOTMARK is False: |
| 833 |
module.fail_json(msg=missing_required_lib("footmark"), exception=FOOTMARK_IMP_ERR) |
| 834 |
|
| 835 |
ecs = ecs_connect(module) |
| 836 |
host_name = module.params["host_name"] |
| 837 |
state = module.params["state"] |
| 838 |
instance_ids = module.params["instance_ids"] |
| 839 |
count_tag = module.params["count_tag"] |
| 840 |
count = module.params["count"] |
| 841 |
instance_name = module.params["instance_name"] |
| 842 |
force = module.params["force"] |
| 843 |
zone_id = module.params["availability_zone"] |
| 844 |
key_name = module.params["key_name"] |
| 845 |
tags = module.params["tags"] |
| 846 |
max_bandwidth_out = module.params["max_bandwidth_out"] |
| 847 |
instance_charge_type = module.params["instance_charge_type"] |
| 848 |
if instance_charge_type == "PrePaid": |
| 849 |
module.params["spot_strategy"] = "" |
| 850 |
changed = False |
| 851 |
|
| 852 |
instances = [] |
| 853 |
if instance_ids: |
| 854 |
if not isinstance(instance_ids, list): |
| 855 |
module.fail_json(msg="The parameter instance_ids should be a list, aborting") |
| 856 |
instances = ecs.describe_instances(zone_id=zone_id, instance_ids=instance_ids) |
| 857 |
if not instances: |
| 858 |
module.fail_json( |
| 859 |
msg=f"There are no instances in our record based on instance_ids {instance_ids}. Please check it and try again." |
| 860 |
) |
| 861 |
elif count_tag: |
| 862 |
instances = ecs.describe_instances(zone_id=zone_id, tags=eval(count_tag)) |
| 863 |
elif instance_name: |
| 864 |
instances = ecs.describe_instances(zone_id=zone_id, instance_name=instance_name) |
| 865 |
|
| 866 |
ids = [] |
| 867 |
if state == "absent": |
| 868 |
if len(instances) < 1: |
| 869 |
module.fail_json( |
| 870 |
msg="Please specify ECS instances that you want to operate by using " |
| 871 |
"parameters instance_ids, tags or instance_name, aborting" |
| 872 |
) |
| 873 |
try: |
| 874 |
targets = [] |
| 875 |
for inst in instances: |
| 876 |
if inst.status != "stopped" and not force: |
| 877 |
module.fail_json(msg="Instance is running, and please stop it or set 'force' as True.") |
| 878 |
targets.append(inst.id) |
| 879 |
if ecs.delete_instances(instance_ids=targets, force=force): |
| 880 |
changed = True |
| 881 |
ids.extend(targets) |
| 882 |
|
| 883 |
module.exit_json(changed=changed, ids=ids, instances=[]) |
| 884 |
except Exception as e: |
| 885 |
module.fail_json(msg=f"Delete instance got an error: {e}") |
| 886 |
|
| 887 |
if module.params["allocate_public_ip"] and max_bandwidth_out < 0: |
| 888 |
module.fail_json(msg="'max_bandwidth_out' should be greater than 0 when 'allocate_public_ip' is True.") |
| 889 |
if not module.params["allocate_public_ip"]: |
| 890 |
module.params["max_bandwidth_out"] = 0 |
| 891 |
|
| 892 |
if state == "present": |
| 893 |
if not instance_ids: |
| 894 |
if len(instances) > count: |
| 895 |
for _i in range(0, len(instances) - count): |
| 896 |
inst = instances[len(instances) - 1] |
| 897 |
if inst.status != "stopped" and not force: |
| 898 |
module.fail_json( |
| 899 |
msg=f"That to delete instance {inst.id} is failed results from it is running, " |
| 900 |
"and please stop it or set 'force' as True." |
| 901 |
) |
| 902 |
try: |
| 903 |
if inst.terminate(force=force): |
| 904 |
changed = True |
| 905 |
except Exception as e: |
| 906 |
module.fail_json(msg=f"Delete instance {inst.id} got an error: {e}") |
| 907 |
instances.pop(len(instances) - 1) |
| 908 |
else: |
| 909 |
try: |
| 910 |
if re.search(r"-\[\d+,\d+\]-", host_name): |
| 911 |
module.fail_json( |
| 912 |
msg="Ordered hostname is not supported, If you want to add an ordered " |
| 913 |
"suffix to the hostname, you can set unique_suffix to True" |
| 914 |
) |
| 915 |
new_instances = run_instance(module, ecs, count - len(instances)) |
| 916 |
if new_instances: |
| 917 |
changed = True |
| 918 |
instances.extend(new_instances) |
| 919 |
except Exception as e: |
| 920 |
module.fail_json(msg=f"Create new instances got an error: {e}") |
| 921 |
|
| 922 |
# Security Group join/leave begin |
| 923 |
security_groups = module.params["security_groups"] |
| 924 |
if security_groups: |
| 925 |
if not isinstance(security_groups, list): |
| 926 |
module.fail_json(msg="The parameter security_groups should be a list, aborting") |
| 927 |
for inst in instances: |
| 928 |
existing = inst.security_group_ids["security_group_id"] |
| 929 |
remove = list(set(existing).difference(set(security_groups))) |
| 930 |
add = list(set(security_groups).difference(set(existing))) |
| 931 |
for sg in remove: |
| 932 |
if inst.leave_security_group(sg): |
| 933 |
changed = True |
| 934 |
for sg in add: |
| 935 |
if inst.join_security_group(sg): |
| 936 |
changed = True |
| 937 |
# Security Group join/leave ends here |
| 938 |
|
| 939 |
# Attach/Detach key pair |
| 940 |
inst_ids = [] |
| 941 |
for inst in instances: |
| 942 |
if key_name is not None and key_name != inst.key_name: |
| 943 |
if key_name == "": |
| 944 |
if inst.detach_key_pair(): |
| 945 |
changed = True |
| 946 |
else: |
| 947 |
inst_ids.append(inst.id) |
| 948 |
if inst_ids: |
| 949 |
changed = ecs.attach_key_pair(instance_ids=inst_ids, key_pair_name=key_name) |
| 950 |
|
| 951 |
# Modify instance attribute |
| 952 |
for inst in instances: |
| 953 |
if modify_instance(module, inst): |
| 954 |
changed = True |
| 955 |
if inst.id not in ids: |
| 956 |
ids.append(inst.id) |
| 957 |
|
| 958 |
# Modify instance charge type |
| 959 |
ids = [] |
| 960 |
for inst in instances: |
| 961 |
if inst.instance_charge_type != instance_charge_type: |
| 962 |
ids.append(inst.id) |
| 963 |
if ids: |
| 964 |
params = { |
| 965 |
"instance_ids": ids, |
| 966 |
"instance_charge_type": instance_charge_type, |
| 967 |
"include_data_disks": module.params["include_data_disks"], |
| 968 |
"dry_run": module.params["dry_run"], |
| 969 |
"auto_pay": True, |
| 970 |
} |
| 971 |
if instance_charge_type == "PrePaid": |
| 972 |
params["period"] = module.params["period"] |
| 973 |
params["period_unit"] = module.params["period_unit"] |
| 974 |
|
| 975 |
if ecs.modify_instance_charge_type(**params): |
| 976 |
changed = True |
| 977 |
wait_for_instance_modify_charge(ecs, ids, instance_charge_type) |
| 978 |
|
| 979 |
else: |
| 980 |
if len(instances) < 1: |
| 981 |
module.fail_json( |
| 982 |
msg="Please specify ECS instances that you want to operate by using " |
| 983 |
"parameters instance_ids, tags or instance_name, aborting" |
| 984 |
) |
| 985 |
if state == "running": |
| 986 |
try: |
| 987 |
targets = [] |
| 988 |
for inst in instances: |
| 989 |
if modify_instance(module, inst): |
| 990 |
changed = True |
| 991 |
if inst.status != "running": |
| 992 |
targets.append(inst.id) |
| 993 |
ids.append(inst.id) |
| 994 |
if targets and ecs.start_instances(instance_ids=targets): |
| 995 |
changed = True |
| 996 |
ids.extend(targets) |
| 997 |
except Exception as e: |
| 998 |
module.fail_json(msg=f"Start instances got an error: {e}") |
| 999 |
elif state == "stopped": |
| 1000 |
try: |
| 1001 |
targets = [] |
| 1002 |
for inst in instances: |
| 1003 |
if inst.status != "stopped": |
| 1004 |
targets.append(inst.id) |
| 1005 |
if targets and ecs.stop_instances(instance_ids=targets, force_stop=force): |
| 1006 |
changed = True |
| 1007 |
ids.extend(targets) |
| 1008 |
for inst in instances: |
| 1009 |
if modify_instance(module, inst): |
| 1010 |
changed = True |
| 1011 |
except Exception as e: |
| 1012 |
module.fail_json(msg=f"Stop instances got an error: {e}") |
| 1013 |
elif state == "restarted": |
| 1014 |
try: |
| 1015 |
targets = [] |
| 1016 |
for inst in instances: |
| 1017 |
if modify_instance(module, inst): |
| 1018 |
changed = True |
| 1019 |
targets.append(inst.id) |
| 1020 |
if ecs.reboot_instances(instance_ids=targets, force_stop=module.params["force"]): |
| 1021 |
changed = True |
| 1022 |
ids.extend(targets) |
| 1023 |
except Exception as e: |
| 1024 |
module.fail_json(msg=f"Reboot instances got an error: {e}") |
| 1025 |
|
| 1026 |
tags = module.params["tags"] |
| 1027 |
if module.params["purge_tags"]: |
| 1028 |
for inst in instances: |
| 1029 |
if not tags: |
| 1030 |
tags = inst.tags |
| 1031 |
try: |
| 1032 |
if inst.remove_tags(tags): |
| 1033 |
changed = True |
| 1034 |
except Exception as e: |
| 1035 |
module.fail_json(msg=f"{e}") |
| 1036 |
module.exit_json(changed=changed, instances=get_instances_info(ecs, ids)) |
| 1037 |
|
| 1038 |
if tags: |
| 1039 |
for inst in instances: |
| 1040 |
try: |
| 1041 |
if inst.add_tags(tags): |
| 1042 |
changed = True |
| 1043 |
except Exception as e: |
| 1044 |
module.fail_json(msg=f"{e}") |
| 1045 |
module.exit_json(changed=changed, instances=get_instances_info(ecs, ids)) |
| 1046 |
|
| 1047 |
|
| 1048 |
if __name__ == "__main__": |
| 1049 |
main() |