| 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: efs_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Get information about Amazon EFS file systems |
| 12 |
description: |
| 13 |
- This module can be used to search Amazon EFS file systems. |
| 14 |
Note that the M(community.aws.efs_info) module no longer returns C(ansible_facts)! |
| 15 |
author: |
| 16 |
- "Ryan Sydnor (@ryansydnor)" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Creation Token of Amazon EFS file system. |
| 21 |
aliases: [ creation_token ] |
| 22 |
type: str |
| 23 |
id: |
| 24 |
description: |
| 25 |
- ID of Amazon EFS. |
| 26 |
type: str |
| 27 |
tags: |
| 28 |
description: |
| 29 |
- List of tags of Amazon EFS. Should be defined as dictionary. |
| 30 |
type: dict |
| 31 |
default: {} |
| 32 |
targets: |
| 33 |
description: |
| 34 |
- List of targets on which to filter the returned results. |
| 35 |
- Result must match all of the specified targets, each of which can be a security group ID, a subnet ID or an IP address. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
default: [] |
| 39 |
extends_documentation_fragment: |
| 40 |
- amazon.aws.common.modules |
| 41 |
- amazon.aws.region.modules |
| 42 |
- amazon.aws.boto3 |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
- name: Find all existing efs |
| 47 |
community.aws.efs_info: |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: Find efs using id |
| 51 |
community.aws.efs_info: |
| 52 |
id: fs-1234abcd |
| 53 |
register: result |
| 54 |
|
| 55 |
- name: Searching all EFS instances with tag Name = 'myTestNameTag', in subnet 'subnet-1a2b3c4d' and with security group 'sg-4d3c2b1a' |
| 56 |
community.aws.efs_info: |
| 57 |
tags: |
| 58 |
Name: myTestNameTag |
| 59 |
targets: |
| 60 |
- subnet-1a2b3c4d |
| 61 |
- sg-4d3c2b1a |
| 62 |
register: result |
| 63 |
|
| 64 |
- ansible.builtin.debug: |
| 65 |
msg: "{{ result['efs'] }}" |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = r""" |
| 69 |
creation_time: |
| 70 |
description: timestamp of creation date |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: "2015-11-16 07:30:57-05:00" |
| 74 |
creation_token: |
| 75 |
description: EFS creation token |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: console-88609e04-9a0e-4a2e-912c-feaa99509961 |
| 79 |
file_system_id: |
| 80 |
description: ID of the file system |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: fs-xxxxxxxx |
| 84 |
life_cycle_state: |
| 85 |
description: state of the EFS file system |
| 86 |
returned: always |
| 87 |
type: str |
| 88 |
sample: creating, available, deleting, deleted |
| 89 |
mount_point: |
| 90 |
description: url of file system with leading dot from the time AWS EFS required to add network suffix to EFS address |
| 91 |
returned: always |
| 92 |
type: str |
| 93 |
sample: .fs-xxxxxxxx.efs.us-west-2.amazonaws.com:/ |
| 94 |
filesystem_address: |
| 95 |
description: url of file system |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: fs-xxxxxxxx.efs.us-west-2.amazonaws.com:/ |
| 99 |
mount_targets: |
| 100 |
description: list of mount targets |
| 101 |
returned: always |
| 102 |
type: list |
| 103 |
sample: |
| 104 |
[ |
| 105 |
{ |
| 106 |
"file_system_id": "fs-a7ad440e", |
| 107 |
"ip_address": "172.31.17.173", |
| 108 |
"life_cycle_state": "available", |
| 109 |
"mount_target_id": "fsmt-d8907871", |
| 110 |
"network_interface_id": "eni-6e387e26", |
| 111 |
"owner_id": "123456789012", |
| 112 |
"security_groups": [ |
| 113 |
"sg-a30b22c6" |
| 114 |
], |
| 115 |
"subnet_id": "subnet-e265c895" |
| 116 |
}, |
| 117 |
... |
| 118 |
] |
| 119 |
name: |
| 120 |
description: name of the file system |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: my-efs |
| 124 |
number_of_mount_targets: |
| 125 |
description: the number of targets mounted |
| 126 |
returned: always |
| 127 |
type: int |
| 128 |
sample: 3 |
| 129 |
owner_id: |
| 130 |
description: AWS account ID of EFS owner |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: XXXXXXXXXXXX |
| 134 |
size_in_bytes: |
| 135 |
description: size of the file system in bytes as of a timestamp |
| 136 |
returned: always |
| 137 |
type: dict |
| 138 |
sample: |
| 139 |
{ |
| 140 |
"timestamp": "2015-12-21 13:59:59-05:00", |
| 141 |
"value": 12288 |
| 142 |
} |
| 143 |
performance_mode: |
| 144 |
description: performance mode of the file system |
| 145 |
returned: always |
| 146 |
type: str |
| 147 |
sample: "generalPurpose" |
| 148 |
throughput_mode: |
| 149 |
description: mode of throughput for the file system |
| 150 |
returned: always |
| 151 |
type: str |
| 152 |
sample: "bursting" |
| 153 |
provisioned_throughput_in_mibps: |
| 154 |
description: throughput provisioned in Mibps |
| 155 |
returned: when throughput_mode is set to "provisioned" |
| 156 |
type: float |
| 157 |
sample: 15.0 |
| 158 |
tags: |
| 159 |
description: tags on the efs instance |
| 160 |
returned: always |
| 161 |
type: dict |
| 162 |
sample: |
| 163 |
{ |
| 164 |
"name": "my-efs", |
| 165 |
"key": "Value" |
| 166 |
} |
| 167 |
""" |
| 168 |
|
| 169 |
|
| 170 |
from collections import defaultdict |
| 171 |
|
| 172 |
try: |
| 173 |
import botocore |
| 174 |
except ImportError: |
| 175 |
pass # caught by AnsibleAWSModule |
| 176 |
|
| 177 |
from ansible.module_utils._text import to_native |
| 178 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 179 |
|
| 180 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 181 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 182 |
|
| 183 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 184 |
|
| 185 |
|
| 186 |
class EFSConnection(object): |
| 187 |
STATE_CREATING = "creating" |
| 188 |
STATE_AVAILABLE = "available" |
| 189 |
STATE_DELETING = "deleting" |
| 190 |
STATE_DELETED = "deleted" |
| 191 |
|
| 192 |
def __init__(self, module): |
| 193 |
try: |
| 194 |
self.connection = module.client("efs") |
| 195 |
self.module = module |
| 196 |
except Exception as e: |
| 197 |
module.fail_json(msg=f"Failed to connect to AWS: {to_native(e)}") |
| 198 |
|
| 199 |
self.region = module.region |
| 200 |
|
| 201 |
@AWSRetry.exponential_backoff(catch_extra_error_codes=["ThrottlingException"]) |
| 202 |
def list_file_systems(self, **kwargs): |
| 203 |
""" |
| 204 |
Returns generator of file systems including all attributes of FS |
| 205 |
""" |
| 206 |
paginator = self.connection.get_paginator("describe_file_systems") |
| 207 |
return paginator.paginate(**kwargs).build_full_result()["FileSystems"] |
| 208 |
|
| 209 |
@AWSRetry.exponential_backoff(catch_extra_error_codes=["ThrottlingException"]) |
| 210 |
def get_tags(self, file_system_id): |
| 211 |
""" |
| 212 |
Returns tag list for selected instance of EFS |
| 213 |
""" |
| 214 |
paginator = self.connection.get_paginator("describe_tags") |
| 215 |
return boto3_tag_list_to_ansible_dict( |
| 216 |
paginator.paginate(FileSystemId=file_system_id).build_full_result()["Tags"] |
| 217 |
) |
| 218 |
|
| 219 |
@AWSRetry.exponential_backoff(catch_extra_error_codes=["ThrottlingException"]) |
| 220 |
def get_mount_targets(self, file_system_id): |
| 221 |
""" |
| 222 |
Returns mount targets for selected instance of EFS |
| 223 |
""" |
| 224 |
paginator = self.connection.get_paginator("describe_mount_targets") |
| 225 |
return paginator.paginate(FileSystemId=file_system_id).build_full_result()["MountTargets"] |
| 226 |
|
| 227 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["ThrottlingException"]) |
| 228 |
def get_security_groups(self, mount_target_id): |
| 229 |
""" |
| 230 |
Returns security groups for selected instance of EFS |
| 231 |
""" |
| 232 |
return self.connection.describe_mount_target_security_groups(MountTargetId=mount_target_id)["SecurityGroups"] |
| 233 |
|
| 234 |
def get_mount_targets_data(self, file_systems): |
| 235 |
for item in file_systems: |
| 236 |
if item["life_cycle_state"] == self.STATE_AVAILABLE: |
| 237 |
try: |
| 238 |
mount_targets = self.get_mount_targets(item["file_system_id"]) |
| 239 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 240 |
self.module.fail_json_aws(e, msg="Couldn't get EFS targets") |
| 241 |
for mt in mount_targets: |
| 242 |
item["mount_targets"].append(camel_dict_to_snake_dict(mt)) |
| 243 |
return file_systems |
| 244 |
|
| 245 |
def get_security_groups_data(self, file_systems): |
| 246 |
for item in file_systems: |
| 247 |
if item["life_cycle_state"] == self.STATE_AVAILABLE: |
| 248 |
for target in item["mount_targets"]: |
| 249 |
if target["life_cycle_state"] == self.STATE_AVAILABLE: |
| 250 |
try: |
| 251 |
target["security_groups"] = self.get_security_groups(target["mount_target_id"]) |
| 252 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 253 |
self.module.fail_json_aws(e, msg="Couldn't get EFS security groups") |
| 254 |
else: |
| 255 |
target["security_groups"] = [] |
| 256 |
else: |
| 257 |
item["tags"] = {} |
| 258 |
item["mount_targets"] = [] |
| 259 |
return file_systems |
| 260 |
|
| 261 |
def get_file_systems(self, file_system_id=None, creation_token=None): |
| 262 |
kwargs = dict() |
| 263 |
if file_system_id: |
| 264 |
kwargs["FileSystemId"] = file_system_id |
| 265 |
if creation_token: |
| 266 |
kwargs["CreationToken"] = creation_token |
| 267 |
try: |
| 268 |
file_systems = self.list_file_systems(**kwargs) |
| 269 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 270 |
self.module.fail_json_aws(e, msg="Couldn't get EFS file systems") |
| 271 |
|
| 272 |
results = list() |
| 273 |
for item in file_systems: |
| 274 |
item["CreationTime"] = str(item["CreationTime"]) |
| 275 |
|
| 276 |
# In the time when MountPoint was introduced there was a need to add a suffix of network path before one could use it |
| 277 |
# AWS updated it and now there is no need to add a suffix. MountPoint is left for back-compatibility purpose |
| 278 |
# And new FilesystemAddress variable is introduced for direct use with other modules (e.g. mount) |
| 279 |
# AWS documentation is available here: |
| 280 |
# U(https://docs.aws.amazon.com/efs/latest/ug/gs-step-three-connect-to-ec2-instance.html) |
| 281 |
|
| 282 |
item["MountPoint"] = f".{item['FileSystemId']}.efs.{self.region}.amazonaws.com:/" |
| 283 |
item["FilesystemAddress"] = f"{item['FileSystemId']}.efs.{self.region}.amazonaws.com:/" |
| 284 |
|
| 285 |
if "Timestamp" in item["SizeInBytes"]: |
| 286 |
item["SizeInBytes"]["Timestamp"] = str(item["SizeInBytes"]["Timestamp"]) |
| 287 |
result = camel_dict_to_snake_dict(item) |
| 288 |
result["tags"] = {} |
| 289 |
result["mount_targets"] = [] |
| 290 |
# Set tags *after* doing camel to snake |
| 291 |
if result["life_cycle_state"] == self.STATE_AVAILABLE: |
| 292 |
try: |
| 293 |
result["tags"] = self.get_tags(result["file_system_id"]) |
| 294 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 295 |
self.module.fail_json_aws(e, msg="Couldn't get EFS tags") |
| 296 |
results.append(result) |
| 297 |
return results |
| 298 |
|
| 299 |
|
| 300 |
def prefix_to_attr(attr_id): |
| 301 |
""" |
| 302 |
Helper method to convert ID prefix to mount target attribute |
| 303 |
""" |
| 304 |
attr_by_prefix = { |
| 305 |
"fsmt-": "mount_target_id", |
| 306 |
"subnet-": "subnet_id", |
| 307 |
"eni-": "network_interface_id", |
| 308 |
"sg-": "security_groups", |
| 309 |
} |
| 310 |
return first_or_default( |
| 311 |
[attr_name for (prefix, attr_name) in attr_by_prefix.items() if str(attr_id).startswith(prefix)], "ip_address" |
| 312 |
) |
| 313 |
|
| 314 |
|
| 315 |
def first_or_default(items, default=None): |
| 316 |
""" |
| 317 |
Helper method to fetch first element of list (if exists) |
| 318 |
""" |
| 319 |
for item in items: |
| 320 |
return item |
| 321 |
return default |
| 322 |
|
| 323 |
|
| 324 |
def has_tags(available, required): |
| 325 |
""" |
| 326 |
Helper method to determine if tag requested already exists |
| 327 |
""" |
| 328 |
for key, value in required.items(): |
| 329 |
if key not in available or value != available[key]: |
| 330 |
return False |
| 331 |
return True |
| 332 |
|
| 333 |
|
| 334 |
def has_targets(available, required): |
| 335 |
""" |
| 336 |
Helper method to determine if mount target requested already exists |
| 337 |
""" |
| 338 |
grouped = group_list_of_dict(available) |
| 339 |
for value, field in required: |
| 340 |
if field not in grouped or value not in grouped[field]: |
| 341 |
return False |
| 342 |
return True |
| 343 |
|
| 344 |
|
| 345 |
def group_list_of_dict(array): |
| 346 |
""" |
| 347 |
Helper method to group list of dict to dict with all possible values |
| 348 |
""" |
| 349 |
result = defaultdict(list) |
| 350 |
for item in array: |
| 351 |
for key, value in item.items(): |
| 352 |
result[key] += value if isinstance(value, list) else [value] |
| 353 |
return result |
| 354 |
|
| 355 |
|
| 356 |
def main(): |
| 357 |
""" |
| 358 |
Module action handler |
| 359 |
""" |
| 360 |
argument_spec = dict( |
| 361 |
id=dict(), |
| 362 |
name=dict(aliases=["creation_token"]), |
| 363 |
tags=dict(type="dict", default={}), |
| 364 |
targets=dict(type="list", default=[], elements="str"), |
| 365 |
) |
| 366 |
|
| 367 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 368 |
|
| 369 |
connection = EFSConnection(module) |
| 370 |
|
| 371 |
name = module.params.get("name") |
| 372 |
fs_id = module.params.get("id") |
| 373 |
tags = module.params.get("tags") |
| 374 |
targets = module.params.get("targets") |
| 375 |
|
| 376 |
file_systems_info = connection.get_file_systems(fs_id, name) |
| 377 |
|
| 378 |
if tags: |
| 379 |
file_systems_info = [item for item in file_systems_info if has_tags(item["tags"], tags)] |
| 380 |
|
| 381 |
file_systems_info = connection.get_mount_targets_data(file_systems_info) |
| 382 |
file_systems_info = connection.get_security_groups_data(file_systems_info) |
| 383 |
|
| 384 |
if targets: |
| 385 |
targets = [(item, prefix_to_attr(item)) for item in targets] |
| 386 |
file_systems_info = [item for item in file_systems_info if has_targets(item["mount_targets"], targets)] |
| 387 |
|
| 388 |
module.exit_json(changed=False, efs=file_systems_info) |
| 389 |
|
| 390 |
|
| 391 |
if __name__ == "__main__": |
| 392 |
main() |