| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ec2_vol_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about EC2 volumes in AWS |
| 12 |
description: |
| 13 |
- Gather information about EC2 volumes in AWS. |
| 14 |
author: "Rob White (@wimnat)" |
| 15 |
options: |
| 16 |
filters: |
| 17 |
type: dict |
| 18 |
default: {} |
| 19 |
description: |
| 20 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 21 |
- See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVolumes.html) for possible filters. |
| 22 |
extends_documentation_fragment: |
| 23 |
- amazon.aws.common.modules |
| 24 |
- amazon.aws.region.modules |
| 25 |
- amazon.aws.boto3 |
| 26 |
""" |
| 27 |
|
| 28 |
EXAMPLES = r""" |
| 29 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 30 |
|
| 31 |
# Gather information about all volumes |
| 32 |
- amazon.aws.ec2_vol_info: |
| 33 |
|
| 34 |
# Gather information about a particular volume using volume ID |
| 35 |
- amazon.aws.ec2_vol_info: |
| 36 |
filters: |
| 37 |
volume-id: vol-00112233 |
| 38 |
|
| 39 |
# Gather information about any volume with a tag key Name and value Example |
| 40 |
- amazon.aws.ec2_vol_info: |
| 41 |
filters: |
| 42 |
"tag:Name": Example |
| 43 |
|
| 44 |
# Gather information about any volume that is attached |
| 45 |
- amazon.aws.ec2_vol_info: |
| 46 |
filters: |
| 47 |
attachment.status: attached |
| 48 |
|
| 49 |
# Gather information about all volumes related to an EC2 Instance |
| 50 |
# register information to `volumes` variable |
| 51 |
# Replaces functionality of `amazon.aws.ec2_vol` - `state: list` |
| 52 |
- name: get volume(s) info from EC2 Instance |
| 53 |
amazon.aws.ec2_vol_info: |
| 54 |
filters: |
| 55 |
attachment.instance-id: "i-000111222333" |
| 56 |
register: volumes |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
volumes: |
| 61 |
description: Volumes that match the provided filters. Each element consists of a dict with all the information related to that volume. |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
returned: always |
| 65 |
contains: |
| 66 |
attachment_set: |
| 67 |
description: |
| 68 |
- Information about the volume attachments. |
| 69 |
- This was changed in version 2.0.0 from a dictionary to a list of dictionaries. |
| 70 |
type: list |
| 71 |
elements: dict |
| 72 |
sample: [{ |
| 73 |
"attach_time": "2015-10-23T00:22:29.000Z", |
| 74 |
"deleteOnTermination": "false", |
| 75 |
"device": "/dev/sdf", |
| 76 |
"instance_id": "i-8356263c", |
| 77 |
"status": "attached" |
| 78 |
}] |
| 79 |
create_time: |
| 80 |
description: The time stamp when volume creation was initiated. |
| 81 |
type: str |
| 82 |
sample: "2015-10-21T14:36:08.870Z" |
| 83 |
encrypted: |
| 84 |
description: Indicates whether the volume is encrypted. |
| 85 |
type: bool |
| 86 |
sample: False |
| 87 |
id: |
| 88 |
description: The ID of the volume. |
| 89 |
type: str |
| 90 |
sample: "vol-35b333d9" |
| 91 |
iops: |
| 92 |
description: The number of I/O operations per second (IOPS) that the volume supports. |
| 93 |
type: int |
| 94 |
sample: null |
| 95 |
size: |
| 96 |
description: The size of the volume, in GiBs. |
| 97 |
type: int |
| 98 |
sample: 1 |
| 99 |
snapshot_id: |
| 100 |
description: The snapshot from which the volume was created, if applicable. |
| 101 |
type: str |
| 102 |
sample: "" |
| 103 |
status: |
| 104 |
description: The volume state. |
| 105 |
type: str |
| 106 |
sample: "in-use" |
| 107 |
tags: |
| 108 |
description: Any tags assigned to the volume. |
| 109 |
type: dict |
| 110 |
sample: { |
| 111 |
env: "dev" |
| 112 |
} |
| 113 |
type: |
| 114 |
description: The volume type. This can be gp2, io1, st1, sc1, or standard. |
| 115 |
type: str |
| 116 |
sample: "standard" |
| 117 |
zone: |
| 118 |
description: The Availability Zone of the volume. |
| 119 |
type: str |
| 120 |
sample: "us-east-1b" |
| 121 |
throughput: |
| 122 |
description: The throughput that the volume supports, in MiB/s. |
| 123 |
type: int |
| 124 |
sample: 131 |
| 125 |
""" |
| 126 |
|
| 127 |
from typing import Any |
| 128 |
from typing import Dict |
| 129 |
|
| 130 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 131 |
|
| 132 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 133 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_volumes |
| 134 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 135 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 136 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import sanitize_filters_to_boto3_filter_list |
| 137 |
|
| 138 |
|
| 139 |
def get_volume_info(volume: Dict[str, Any], region: str) -> Dict[str, Any]: |
| 140 |
attachment_data = [] |
| 141 |
for data in volume["attachments"]: |
| 142 |
attachment_data.append( |
| 143 |
{ |
| 144 |
"attach_time": data.get("attach_time", None), |
| 145 |
"device": data.get("device", None), |
| 146 |
"instance_id": data.get("instance_id", None), |
| 147 |
"status": data.get("state", None), |
| 148 |
"delete_on_termination": data.get("delete_on_termination", None), |
| 149 |
} |
| 150 |
) |
| 151 |
|
| 152 |
volume_info = { |
| 153 |
"create_time": volume["create_time"], |
| 154 |
"id": volume["volume_id"], |
| 155 |
"encrypted": volume["encrypted"], |
| 156 |
"iops": volume["iops"] if "iops" in volume else None, |
| 157 |
"size": volume["size"], |
| 158 |
"snapshot_id": volume["snapshot_id"], |
| 159 |
"status": volume["state"], |
| 160 |
"type": volume["volume_type"], |
| 161 |
"zone": volume["availability_zone"], |
| 162 |
"region": region, |
| 163 |
"attachment_set": attachment_data, |
| 164 |
"tags": boto3_tag_list_to_ansible_dict(volume["tags"]) if "tags" in volume else None, |
| 165 |
} |
| 166 |
|
| 167 |
if "throughput" in volume: |
| 168 |
volume_info["throughput"] = volume["throughput"] |
| 169 |
|
| 170 |
return volume_info |
| 171 |
|
| 172 |
|
| 173 |
def list_ec2_volumes(connection, module: AnsibleAWSModule) -> None: |
| 174 |
# Sanitize filters |
| 175 |
sanitized_filters = sanitize_filters_to_boto3_filter_list(module.params.get("filters"), ignore_keys=["tag:"]) |
| 176 |
volume_dict_array = [] |
| 177 |
|
| 178 |
try: |
| 179 |
all_volumes = describe_volumes(connection, Filters=sanitized_filters) |
| 180 |
except AnsibleEC2Error as e: |
| 181 |
module.fail_json_aws(e, msg="Failed to describe volumes.") |
| 182 |
|
| 183 |
for volume in all_volumes: |
| 184 |
volume = camel_dict_to_snake_dict(volume, ignore_list=["Tags"]) |
| 185 |
volume_dict_array.append(get_volume_info(volume, module.region)) |
| 186 |
module.exit_json(volumes=volume_dict_array) |
| 187 |
|
| 188 |
|
| 189 |
def main(): |
| 190 |
argument_spec = dict(filters=dict(default={}, type="dict")) |
| 191 |
|
| 192 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 193 |
|
| 194 |
connection = module.client("ec2") |
| 195 |
|
| 196 |
list_ec2_volumes(connection, module) |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |