| 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 |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Create or delete an EC2 Placement Group |
| 13 |
description: |
| 14 |
- Create an EC2 Placement Group; if the placement group already exists, |
| 15 |
nothing is done. Or, delete an existing placement group. If the placement |
| 16 |
group is absent, do nothing. See also |
| 17 |
U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html). |
| 18 |
author: |
| 19 |
- "Brad Macpherson (@iiibrad)" |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- The name for the placement group. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
partition_count: |
| 27 |
description: |
| 28 |
- The number of partitions. |
| 29 |
- Valid only when O(strategy) is set to V(partition). |
| 30 |
- Must be a value between V(1) and V(7). |
| 31 |
type: int |
| 32 |
version_added: 3.1.0 |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Create or delete placement group. |
| 36 |
default: present |
| 37 |
choices: [ 'present', 'absent' ] |
| 38 |
type: str |
| 39 |
strategy: |
| 40 |
description: |
| 41 |
- Placement group strategy. Cluster will cluster instances into a |
| 42 |
low-latency group in a single Availability Zone, while Spread spreads |
| 43 |
instances across underlying hardware. |
| 44 |
default: cluster |
| 45 |
choices: [ 'cluster', 'spread', 'partition' ] |
| 46 |
type: str |
| 47 |
tags: |
| 48 |
description: |
| 49 |
- A dict of key value pairs to associate with the placement group. |
| 50 |
type: dict |
| 51 |
version_added: 8.1.0 |
| 52 |
extends_documentation_fragment: |
| 53 |
- amazon.aws.common.modules |
| 54 |
- amazon.aws.region.modules |
| 55 |
- amazon.aws.boto3 |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
# Note: These examples do not set authentication details, see the AWS Guide |
| 60 |
# for details. |
| 61 |
|
| 62 |
- name: Create a placement group. |
| 63 |
amazon.aws.ec2_placement_group: |
| 64 |
name: my-cluster |
| 65 |
state: present |
| 66 |
|
| 67 |
- name: Create a Spread placement group. |
| 68 |
amazon.aws.ec2_placement_group: |
| 69 |
name: my-cluster |
| 70 |
state: present |
| 71 |
strategy: spread |
| 72 |
|
| 73 |
- name: Create a Partition strategy placement group. |
| 74 |
amazon.aws.ec2_placement_group: |
| 75 |
name: my-cluster |
| 76 |
state: present |
| 77 |
strategy: partition |
| 78 |
partition_count: 3 |
| 79 |
|
| 80 |
- name: Delete a placement group. |
| 81 |
amazon.aws.ec2_placement_group: |
| 82 |
name: my-cluster |
| 83 |
state: absent |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = r""" |
| 87 |
placement_group: |
| 88 |
description: Placement group attributes. |
| 89 |
returned: when state != absent |
| 90 |
type: dict |
| 91 |
contains: |
| 92 |
group_arn: |
| 93 |
description: Placement Group ARN. |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
sample: "arn:aws:ec2:us-east-1:123456789012:placement-group" |
| 97 |
group_id: |
| 98 |
description: Placement Group Id. |
| 99 |
type: str |
| 100 |
returned: always |
| 101 |
sample: "pg-123456789012" |
| 102 |
name: |
| 103 |
description: Placement Group name. |
| 104 |
type: str |
| 105 |
returned: always |
| 106 |
sample: "my-cluster" |
| 107 |
partition_count: |
| 108 |
description: Partition Count. |
| 109 |
type: str |
| 110 |
returned: If applicable |
| 111 |
sample: "my-cluster" |
| 112 |
state: |
| 113 |
description: Placement Groupt state. |
| 114 |
type: str |
| 115 |
returned: If applicable |
| 116 |
sample: "available" |
| 117 |
strategy: |
| 118 |
description: Placement Group strategy. |
| 119 |
type: str |
| 120 |
returned: If applicable |
| 121 |
sample: "cluster" |
| 122 |
tags: |
| 123 |
description: Tags associated with the placement group. |
| 124 |
type: dict |
| 125 |
returned: If applicable |
| 126 |
version_added: 8.1.0 |
| 127 |
sample: |
| 128 |
tags: |
| 129 |
some: value1 |
| 130 |
other: value2 |
| 131 |
""" |
| 132 |
|
| 133 |
from typing import Any |
| 134 |
from typing import Dict |
| 135 |
|
| 136 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 137 |
|
| 138 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_ec2_placement_group |
| 139 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_ec2_placement_group |
| 140 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_placement_groups |
| 141 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 142 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 143 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications |
| 144 |
|
| 145 |
|
| 146 |
def search_placement_group(connection, name: str) -> Dict[str, Any]: |
| 147 |
""" |
| 148 |
Check if a placement group exists. |
| 149 |
""" |
| 150 |
response = describe_ec2_placement_groups(connection, Filters=[{"Name": "group-name", "Values": [name]}]) |
| 151 |
|
| 152 |
if len(response) != 1: |
| 153 |
return None |
| 154 |
else: |
| 155 |
return format_placement_group_information(response[0]) |
| 156 |
|
| 157 |
|
| 158 |
def format_placement_group_information(response: Dict[str, Any]) -> Dict[str, Any]: |
| 159 |
""" |
| 160 |
Format placement group information |
| 161 |
""" |
| 162 |
|
| 163 |
response = camel_dict_to_snake_dict(response, ignore_list=["Tags"]) |
| 164 |
if "tags" in response: |
| 165 |
response["tags"] = boto3_tag_list_to_ansible_dict(response.get("tags", [])) |
| 166 |
response["name"] = response["group_name"] |
| 167 |
return response |
| 168 |
|
| 169 |
|
| 170 |
def create_placement_group(connection, module: AnsibleAWSModule) -> None: |
| 171 |
name = module.params.get("name") |
| 172 |
strategy = module.params.get("strategy") |
| 173 |
tags = module.params.get("tags") |
| 174 |
partition_count = module.params.get("partition_count") |
| 175 |
|
| 176 |
if strategy != "partition" and partition_count: |
| 177 |
module.fail_json(msg="'partition_count' can only be set when strategy is set to 'partition'.") |
| 178 |
|
| 179 |
params = {} |
| 180 |
params["GroupName"] = name |
| 181 |
params["Strategy"] = strategy |
| 182 |
if tags: |
| 183 |
params["TagSpecifications"] = boto3_tag_specifications(tags, types=["placement-group"]) |
| 184 |
if partition_count: |
| 185 |
params["PartitionCount"] = partition_count |
| 186 |
if module.check_mode: |
| 187 |
module.exit_json( |
| 188 |
changed=True, |
| 189 |
placement_group={ |
| 190 |
"name": name, |
| 191 |
"strategy": strategy, |
| 192 |
"tags": tags, |
| 193 |
}, |
| 194 |
msg="EC2 placement group would be created if not in check mode", |
| 195 |
) |
| 196 |
|
| 197 |
response = create_ec2_placement_group(connection, **params) |
| 198 |
module.exit_json(changed=True, placement_group=format_placement_group_information(response)) |
| 199 |
|
| 200 |
|
| 201 |
def delete_placement_group(connection, module: AnsibleAWSModule) -> None: |
| 202 |
if module.check_mode: |
| 203 |
module.exit_json(changed=True, msg="VPC would be deleted if not in check mode") |
| 204 |
name = module.params.get("name") |
| 205 |
delete_ec2_placement_group(connection, name) |
| 206 |
module.exit_json(changed=True) |
| 207 |
|
| 208 |
|
| 209 |
def main(): |
| 210 |
argument_spec = dict( |
| 211 |
name=dict(required=True, type="str"), |
| 212 |
partition_count=dict(type="int"), |
| 213 |
state=dict(default="present", choices=["present", "absent"]), |
| 214 |
strategy=dict(default="cluster", choices=["cluster", "spread", "partition"]), |
| 215 |
tags=dict(type="dict"), |
| 216 |
) |
| 217 |
|
| 218 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 219 |
|
| 220 |
connection = module.client("ec2") |
| 221 |
|
| 222 |
state = module.params.get("state") |
| 223 |
name = module.params.get("name") |
| 224 |
placement_group = search_placement_group(connection, name) |
| 225 |
|
| 226 |
if state == "present": |
| 227 |
if placement_group is None: |
| 228 |
create_placement_group(connection, module) |
| 229 |
else: |
| 230 |
strategy = module.params.get("strategy") |
| 231 |
if placement_group["strategy"] == strategy: |
| 232 |
module.exit_json(changed=False, placement_group=placement_group) |
| 233 |
else: |
| 234 |
name = module.params.get("name") |
| 235 |
module.fail_json( |
| 236 |
msg=f"Placement group '{name}' exists, can't change strategy from '{placement_group['strategy']}' to '{strategy}'" |
| 237 |
) |
| 238 |
|
| 239 |
elif state == "absent": |
| 240 |
if placement_group is None: |
| 241 |
module.exit_json(changed=False) |
| 242 |
else: |
| 243 |
delete_placement_group(connection, module) |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == "__main__": |
| 247 |
main() |