| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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_placement_group_info |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: List EC2 Placement Group(s) details |
| 13 |
description: |
| 14 |
- List details of EC2 Placement Group(s). |
| 15 |
author: |
| 16 |
- "Brad Macpherson (@iiibrad)" |
| 17 |
options: |
| 18 |
names: |
| 19 |
description: |
| 20 |
- A list of names to filter on. If a listed group does not exist, there |
| 21 |
will be no corresponding entry in the result; no error will be raised. |
| 22 |
type: list |
| 23 |
elements: str |
| 24 |
required: false |
| 25 |
default: [] |
| 26 |
extends_documentation_fragment: |
| 27 |
- amazon.aws.common.modules |
| 28 |
- amazon.aws.region.modules |
| 29 |
- amazon.aws.boto3 |
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
# Note: These examples do not set authentication details or the AWS region, |
| 34 |
# see the AWS Guide for details. |
| 35 |
|
| 36 |
- name: List all placement groups. |
| 37 |
amazon.aws.ec2_placement_group_info: |
| 38 |
register: all_ec2_placement_groups |
| 39 |
|
| 40 |
- name: List two placement groups. |
| 41 |
amazon.aws.ec2_placement_group_info: |
| 42 |
names: |
| 43 |
- my-cluster |
| 44 |
- my-other-cluster |
| 45 |
register: specific_ec2_placement_groups |
| 46 |
|
| 47 |
- ansible.builtin.debug: |
| 48 |
msg: > |
| 49 |
{{ specific_ec2_placement_groups | json_query("[?name=='my-cluster']") }} |
| 50 |
""" |
| 51 |
|
| 52 |
|
| 53 |
RETURN = r""" |
| 54 |
placement_groups: |
| 55 |
description: Placement group attributes. |
| 56 |
returned: always |
| 57 |
type: complex |
| 58 |
contains: |
| 59 |
name: |
| 60 |
description: PG name. |
| 61 |
type: str |
| 62 |
sample: "my-cluster" |
| 63 |
state: |
| 64 |
description: PG state. |
| 65 |
type: str |
| 66 |
sample: "available" |
| 67 |
strategy: |
| 68 |
description: PG strategy. |
| 69 |
type: str |
| 70 |
sample: "cluster" |
| 71 |
tags: |
| 72 |
description: Tags associated with the placement group. |
| 73 |
type: dict |
| 74 |
version_added: 8.1.0 |
| 75 |
sample: |
| 76 |
tags: |
| 77 |
some: value1 |
| 78 |
other: value2 |
| 79 |
""" |
| 80 |
|
| 81 |
from typing import Any |
| 82 |
from typing import Dict |
| 83 |
from typing import List |
| 84 |
|
| 85 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_placement_groups |
| 86 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 87 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 88 |
|
| 89 |
|
| 90 |
def get_placement_groups_details(connection, names: List) -> Dict[str, Any]: |
| 91 |
params = {} |
| 92 |
if len(names) > 0: |
| 93 |
params["Filters"] = [ |
| 94 |
{ |
| 95 |
"Name": "group-name", |
| 96 |
"Values": names, |
| 97 |
} |
| 98 |
] |
| 99 |
response = describe_ec2_placement_groups(connection, **params) |
| 100 |
|
| 101 |
results = [] |
| 102 |
for placement_group in response: |
| 103 |
results.append( |
| 104 |
{ |
| 105 |
"name": placement_group["GroupName"], |
| 106 |
"state": placement_group["State"], |
| 107 |
"strategy": placement_group["Strategy"], |
| 108 |
"tags": boto3_tag_list_to_ansible_dict(placement_group.get("Tags")), |
| 109 |
} |
| 110 |
) |
| 111 |
return results |
| 112 |
|
| 113 |
|
| 114 |
def main(): |
| 115 |
argument_spec = dict( |
| 116 |
names=dict(type="list", default=[], elements="str"), |
| 117 |
) |
| 118 |
|
| 119 |
module = AnsibleAWSModule( |
| 120 |
argument_spec=argument_spec, |
| 121 |
supports_check_mode=True, |
| 122 |
) |
| 123 |
|
| 124 |
connection = module.client("ec2") |
| 125 |
names = module.params.get("names") |
| 126 |
|
| 127 |
placement_groups = get_placement_groups_details(connection, names) |
| 128 |
module.exit_json(changed=False, placement_groups=placement_groups) |
| 129 |
|
| 130 |
|
| 131 |
if __name__ == "__main__": |
| 132 |
main() |