Skip to content

Commit

Permalink
ec2_placement_group: Add partition strategy and partition count (#872) (
Browse files Browse the repository at this point in the history
#928)

[PR #872/716ae77f backport][stable-3] ec2_placement_group: Add partition strategy and partition count

This is a backport of PR #872 as merged into main (716ae77).
SUMMARY

Add partition as a strategy and an option, partition_count to choose the actual number of partitions for the community.aws.ec2_placement_group module.

Fixes #808
ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ec2_placement_group
ADDITIONAL INFO
Tested locally with
- name: Create a partition placement group with partition count 4.
    ec2_placement_group:
      name: my-cluster
      state: present
      strategy: partition
      partition_count: 4
  • Loading branch information
patchback[bot] committed Feb 7, 2022
1 parent 30ce931 commit d42b7e0
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_placement_group - add support for partition strategy and partition count (https://github.com/ansible-collections/community.aws/pull/872).
34 changes: 30 additions & 4 deletions plugins/modules/ec2_placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
- The name for the placement group.
required: true
type: str
partition_count:
description:
- The number of partitions.
- Valid only when I(Strategy) is set to C(partition).
- Must be a value between C(1) and C(7).
type: int
version_added: 3.1.0
state:
description:
- Create or delete placement group.
Expand All @@ -35,7 +42,7 @@
low-latency group in a single Availability Zone, while Spread spreads
instances across underlying hardware.
default: cluster
choices: [ 'cluster', 'spread' ]
choices: [ 'cluster', 'spread', 'partition' ]
type: str
extends_documentation_fragment:
- amazon.aws.aws
Expand All @@ -58,6 +65,13 @@
state: present
strategy: spread
- name: Create a Partition strategy placement group.
community.aws.ec2_placement_group:
name: my-cluster
state: present
strategy: partition
partition_count: 3
- name: Delete a placement group.
community.aws.ec2_placement_group:
name: my-cluster
Expand Down Expand Up @@ -126,10 +140,21 @@ def get_placement_group_details(connection, module):
def create_placement_group(connection, module):
name = module.params.get("name")
strategy = module.params.get("strategy")
partition_count = module.params.get("partition_count")

if strategy != 'partition' and partition_count:
module.fail_json(
msg="'partition_count' can only be set when strategy is set to 'partition'.")

params = {}
params['GroupName'] = name
params['Strategy'] = strategy
if partition_count:
params['PartitionCount'] = partition_count
params['DryRun'] = module.check_mode

try:
connection.create_placement_group(
GroupName=name, Strategy=strategy, DryRun=module.check_mode)
connection.create_placement_group(**params)
except is_boto3_error_code('DryRunOperation'):
module.exit_json(changed=True, placement_group={
"name": name,
Expand Down Expand Up @@ -165,8 +190,9 @@ def delete_placement_group(connection, module):
def main():
argument_spec = dict(
name=dict(required=True, type='str'),
partition_count=dict(type='int'),
state=dict(default='present', choices=['present', 'absent']),
strategy=dict(default='cluster', choices=['cluster', 'spread'])
strategy=dict(default='cluster', choices=['cluster', 'spread', 'partition'])
)

module = AnsibleAWSModule(
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/ec2_placement_group/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud/aws

ec2_placement_group_info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
- name: remove any instances in the test VPC
ec2_instance:
filters:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: Get ENIs
ec2_eni_info:
filters:
vpc-id: "{{ testing_vpc.vpc.id }}"
register: enis

- name: delete all ENIs
ec2_eni:
eni_id: "{{ item.id }}"
state: absent
until: removed is not failed
with_items: "{{ enis.network_interfaces }}"
ignore_errors: yes
retries: 10

- name: remove the security group
ec2_group:
name: "{{ resource_prefix }}-sg"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove routing rules
ec2_vpc_route_table:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove internet gateway
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove subnet A
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove subnet B
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove the VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
state: absent
tags:
Name: Ansible Testing VPC
tenancy: default
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
64 changes: 64 additions & 0 deletions tests/integration/targets/ec2_placement_group/tasks/env_setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- name: Create VPC for use in testing
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
tags:
Name: Ansible ec2_lc Testing VPC
tenancy: default
register: testing_vpc

- name: Create internet gateway for use in testing
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: present
tags:
Name: Ansible ec2_lc Testing gateway
register: igw

- name: Create default subnet in zone A
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
az: "{{ aws_region }}a"
resource_tags:
Name: "{{ resource_prefix }}-subnet-a"
register: testing_subnet_a

- name: Create secondary subnet in zone B
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
az: "{{ aws_region }}b"
resource_tags:
Name: "{{ resource_prefix }}-subnet-b"
register: testing_subnet_b

- name: create routing rules
ec2_vpc_route_table:
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"

- name: create a security group with the vpc
ec2_group:
name: "{{ resource_prefix }}-sg"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: sg
Loading

0 comments on commit d42b7e0

Please sign in to comment.