| 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_info |
| 26 |
short_description: Gather information on instances of Alibaba Cloud ECS |
| 27 |
description: |
| 28 |
- This module fetches data from the Open API in Alicloud. The module must be called from within the ECS instance itself. |
| 29 |
attributes: |
| 30 |
check_mode: |
| 31 |
version_added: 3.3.0 |
| 32 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 33 |
|
| 34 |
options: |
| 35 |
name_prefix: |
| 36 |
description: |
| 37 |
- Use a instance name prefix to filter ECS instances. |
| 38 |
type: str |
| 39 |
version_added: '0.2.0' |
| 40 |
tags: |
| 41 |
description: |
| 42 |
- A hash/dictionaries of instance tags. C({"key":"value"}). |
| 43 |
aliases: ["instance_tags"] |
| 44 |
type: dict |
| 45 |
filters: |
| 46 |
description: |
| 47 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. The filter keys can be all |
| 48 |
of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/25506.htm) for parameter details. Filter |
| 49 |
keys can be same as request parameter name or be lower case and use underscore (V("_")) or dash (V("-")) to connect |
| 50 |
different words in one parameter. C(InstanceIds) should be a list. C(Tag.n.Key) and C(Tag.n.Value) should be a dict |
| 51 |
and using O(tags) instead. |
| 52 |
type: dict |
| 53 |
version_added: '0.2.0' |
| 54 |
author: |
| 55 |
- "He Guimin (@xiaozhu36)" |
| 56 |
requirements: |
| 57 |
- "Python >= 3.6" |
| 58 |
- "footmark >= 1.13.0" |
| 59 |
extends_documentation_fragment: |
| 60 |
- community.general._alicloud |
| 61 |
- community.general._attributes |
| 62 |
- community.general._attributes.info_module |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
# Fetch instances details according to setting different filters |
| 67 |
|
| 68 |
- name: Find all instances in the specified region |
| 69 |
community.general.ali_instance_info: |
| 70 |
register: all_instances |
| 71 |
|
| 72 |
- name: Find all instances based on the specified ids |
| 73 |
community.general.ali_instance_info: |
| 74 |
instance_ids: |
| 75 |
- "i-35b333d9" |
| 76 |
- "i-ddav43kd" |
| 77 |
register: instances_by_ids |
| 78 |
|
| 79 |
- name: Find all instances based on the specified name_prefix |
| 80 |
community.general.ali_instance_info: |
| 81 |
name_prefix: "ecs_instance_" |
| 82 |
register: instances_by_name_prefix |
| 83 |
|
| 84 |
- name: Find instances based on tags |
| 85 |
community.general.ali_instance_info: |
| 86 |
tags: |
| 87 |
Test: "add" |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = r""" |
| 91 |
instances: |
| 92 |
description: List of ECS instances. |
| 93 |
returned: always |
| 94 |
type: list |
| 95 |
elements: dict |
| 96 |
contains: |
| 97 |
availability_zone: |
| 98 |
description: The availability zone of the instance is in. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: cn-beijing-a |
| 102 |
block_device_mappings: |
| 103 |
description: Any block device mapping entries for the instance. |
| 104 |
returned: always |
| 105 |
type: complex |
| 106 |
contains: |
| 107 |
device_name: |
| 108 |
description: The device name exposed to the instance (for example, /dev/xvda). |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: /dev/xvda |
| 112 |
attach_time: |
| 113 |
description: The time stamp when the attachment initiated. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: "2018-06-25T04:08:26Z" |
| 117 |
delete_on_termination: |
| 118 |
description: Indicates whether the volume is deleted on instance termination. |
| 119 |
returned: always |
| 120 |
type: bool |
| 121 |
sample: true |
| 122 |
status: |
| 123 |
description: The attachment state. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: in_use |
| 127 |
volume_id: |
| 128 |
description: The ID of the cloud disk. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: d-2zei53pjsi117y6gf9t6 |
| 132 |
cpu: |
| 133 |
description: The CPU core count of the instance. |
| 134 |
returned: always |
| 135 |
type: int |
| 136 |
sample: 4 |
| 137 |
creation_time: |
| 138 |
description: The time the instance was created. |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
sample: "2018-06-25T04:08Z" |
| 142 |
description: |
| 143 |
description: The instance description. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: "my ansible instance" |
| 147 |
eip: |
| 148 |
description: The attribution of EIP associated with the instance. |
| 149 |
returned: always |
| 150 |
type: complex |
| 151 |
contains: |
| 152 |
allocation_id: |
| 153 |
description: The ID of the EIP. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: eip-12345 |
| 157 |
internet_charge_type: |
| 158 |
description: The internet charge type of the EIP. |
| 159 |
returned: always |
| 160 |
type: str |
| 161 |
sample: "paybybandwidth" |
| 162 |
ip_address: |
| 163 |
description: EIP address. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: 42.10.2.2 |
| 167 |
expired_time: |
| 168 |
description: The time the instance expires. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
sample: "2099-12-31T15:59Z" |
| 172 |
gpu: |
| 173 |
description: The attribution of instance GPU. |
| 174 |
returned: always |
| 175 |
type: complex |
| 176 |
contains: |
| 177 |
amount: |
| 178 |
description: The count of the GPU. |
| 179 |
returned: always |
| 180 |
type: int |
| 181 |
sample: 0 |
| 182 |
spec: |
| 183 |
description: The specification of the GPU. |
| 184 |
returned: always |
| 185 |
type: str |
| 186 |
sample: "" |
| 187 |
host_name: |
| 188 |
description: The host name of the instance. |
| 189 |
returned: always |
| 190 |
type: str |
| 191 |
sample: iZ2zewaoZ |
| 192 |
id: |
| 193 |
description: Alias of instance_id. |
| 194 |
returned: always |
| 195 |
type: str |
| 196 |
sample: i-abc12345 |
| 197 |
instance_id: |
| 198 |
description: ECS instance resource ID. |
| 199 |
returned: always |
| 200 |
type: str |
| 201 |
sample: i-abc12345 |
| 202 |
image_id: |
| 203 |
description: The ID of the image used to launch the instance. |
| 204 |
returned: always |
| 205 |
type: str |
| 206 |
sample: m-0011223344 |
| 207 |
inner_ip_address: |
| 208 |
description: The inner IPv4 address of the classic instance. |
| 209 |
returned: always |
| 210 |
type: str |
| 211 |
sample: 10.0.0.2 |
| 212 |
instance_charge_type: |
| 213 |
description: The instance charge type. |
| 214 |
returned: always |
| 215 |
type: str |
| 216 |
sample: PostPaid |
| 217 |
instance_name: |
| 218 |
description: The name of the instance. |
| 219 |
returned: always |
| 220 |
type: str |
| 221 |
sample: my-ecs |
| 222 |
instance_type_family: |
| 223 |
description: The instance type family of the instance belongs. |
| 224 |
returned: always |
| 225 |
type: str |
| 226 |
sample: ecs.sn1ne |
| 227 |
instance_type: |
| 228 |
description: The instance type of the running instance. |
| 229 |
returned: always |
| 230 |
type: str |
| 231 |
sample: ecs.sn1ne.xlarge |
| 232 |
internet_charge_type: |
| 233 |
description: The billing method of the network bandwidth. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
sample: PayByBandwidth |
| 237 |
internet_max_bandwidth_in: |
| 238 |
description: Maximum incoming bandwidth from the internet network. |
| 239 |
returned: always |
| 240 |
type: int |
| 241 |
sample: 200 |
| 242 |
internet_max_bandwidth_out: |
| 243 |
description: Maximum incoming bandwidth from the internet network. |
| 244 |
returned: always |
| 245 |
type: int |
| 246 |
sample: 20 |
| 247 |
io_optimized: |
| 248 |
description: Indicates whether the instance is optimized for EBS I/O. |
| 249 |
returned: always |
| 250 |
type: bool |
| 251 |
sample: false |
| 252 |
memory: |
| 253 |
description: Memory size of the instance. |
| 254 |
returned: always |
| 255 |
type: int |
| 256 |
sample: 8192 |
| 257 |
network_interfaces: |
| 258 |
description: One or more network interfaces for the instance. |
| 259 |
returned: always |
| 260 |
type: complex |
| 261 |
contains: |
| 262 |
mac_address: |
| 263 |
description: The MAC address. |
| 264 |
returned: always |
| 265 |
type: str |
| 266 |
sample: "00:11:22:33:44:55" |
| 267 |
network_interface_id: |
| 268 |
description: The ID of the network interface. |
| 269 |
returned: always |
| 270 |
type: str |
| 271 |
sample: eni-01234567 |
| 272 |
primary_ip_address: |
| 273 |
description: The primary IPv4 address of the network interface within the vswitch. |
| 274 |
returned: always |
| 275 |
type: str |
| 276 |
sample: 10.0.0.1 |
| 277 |
osname: |
| 278 |
description: The operation system name of the instance owned. |
| 279 |
returned: always |
| 280 |
type: str |
| 281 |
sample: CentOS |
| 282 |
ostype: |
| 283 |
description: The operation system type of the instance owned. |
| 284 |
returned: always |
| 285 |
type: str |
| 286 |
sample: linux |
| 287 |
private_ip_address: |
| 288 |
description: The IPv4 address of the network interface within the subnet. |
| 289 |
returned: always |
| 290 |
type: str |
| 291 |
sample: 10.0.0.1 |
| 292 |
public_ip_address: |
| 293 |
description: The public IPv4 address assigned to the instance or EIP address. |
| 294 |
returned: always |
| 295 |
type: str |
| 296 |
sample: 43.0.0.1 |
| 297 |
resource_group_id: |
| 298 |
description: The ID of the resource group to which the instance belongs. |
| 299 |
returned: always |
| 300 |
type: str |
| 301 |
sample: my-ecs-group |
| 302 |
security_groups: |
| 303 |
description: One or more security groups for the instance. |
| 304 |
returned: always |
| 305 |
type: list |
| 306 |
elements: dict |
| 307 |
contains: |
| 308 |
group_id: |
| 309 |
description: The ID of the security group. |
| 310 |
returned: always |
| 311 |
type: str |
| 312 |
sample: sg-0123456 |
| 313 |
group_name: |
| 314 |
description: The name of the security group. |
| 315 |
returned: always |
| 316 |
type: str |
| 317 |
sample: my-security-group |
| 318 |
status: |
| 319 |
description: The current status of the instance. |
| 320 |
returned: always |
| 321 |
type: str |
| 322 |
sample: running |
| 323 |
tags: |
| 324 |
description: Any tags assigned to the instance. |
| 325 |
returned: always |
| 326 |
type: dict |
| 327 |
sample: |
| 328 |
vswitch_id: |
| 329 |
description: The ID of the vswitch in which the instance is running. |
| 330 |
returned: always |
| 331 |
type: str |
| 332 |
sample: vsw-dew00abcdef |
| 333 |
vpc_id: |
| 334 |
description: The ID of the VPC the instance is in. |
| 335 |
returned: always |
| 336 |
type: str |
| 337 |
sample: vpc-0011223344 |
| 338 |
ids: |
| 339 |
description: List of ECS instance IDs. |
| 340 |
returned: always |
| 341 |
type: list |
| 342 |
sample: ["i-12345er", "i-3245fs"] |
| 343 |
""" |
| 344 |
|
| 345 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 346 |
|
| 347 |
from ansible_collections.community.general.plugins.module_utils._alicloud_ecs import ( |
| 348 |
FOOTMARK_IMP_ERR, |
| 349 |
HAS_FOOTMARK, |
| 350 |
ecs_argument_spec, |
| 351 |
ecs_connect, |
| 352 |
) |
| 353 |
|
| 354 |
|
| 355 |
def main(): |
| 356 |
argument_spec = ecs_argument_spec() |
| 357 |
argument_spec.update( |
| 358 |
dict(name_prefix=dict(type="str"), tags=dict(type="dict", aliases=["instance_tags"]), filters=dict(type="dict")) |
| 359 |
) |
| 360 |
module = AnsibleModule( |
| 361 |
argument_spec=argument_spec, |
| 362 |
supports_check_mode=True, |
| 363 |
) |
| 364 |
|
| 365 |
if HAS_FOOTMARK is False: |
| 366 |
module.fail_json(msg=missing_required_lib("footmark"), exception=FOOTMARK_IMP_ERR) |
| 367 |
|
| 368 |
ecs = ecs_connect(module) |
| 369 |
|
| 370 |
instances = [] |
| 371 |
instance_ids = [] |
| 372 |
ids = [] |
| 373 |
name_prefix = module.params["name_prefix"] |
| 374 |
|
| 375 |
filters = module.params["filters"] |
| 376 |
if not filters: |
| 377 |
filters = {} |
| 378 |
for key, value in list(filters.items()): |
| 379 |
if key in ["InstanceIds", "instance_ids", "instance-ids"] and isinstance(ids, list): |
| 380 |
for id in value: |
| 381 |
if id not in ids: |
| 382 |
ids.append(value) |
| 383 |
if ids: |
| 384 |
filters["instance_ids"] = ids |
| 385 |
if module.params["tags"]: |
| 386 |
filters["tags"] = module.params["tags"] |
| 387 |
|
| 388 |
for inst in ecs.describe_instances(**filters): |
| 389 |
if name_prefix: |
| 390 |
if not str(inst.instance_name).startswith(name_prefix): |
| 391 |
continue |
| 392 |
volumes = ecs.describe_disks(instance_id=inst.id) |
| 393 |
inst.block_device_mappings = volumes |
| 394 |
inst.user_data = inst.describe_user_data() |
| 395 |
instances.append(inst.read()) |
| 396 |
instance_ids.append(inst.id) |
| 397 |
|
| 398 |
module.exit_json(changed=False, ids=instance_ids, instances=instances) |
| 399 |
|
| 400 |
|
| 401 |
if __name__ == "__main__": |
| 402 |
main() |