From 8a1afc7aeba43fcf41b76bd595b8bdc446d02361 Mon Sep 17 00:00:00 2001 From: Daniel Riek Date: Sat, 12 Nov 2022 18:38:56 -0500 Subject: [PATCH 1/3] Adding ability to set and change PlacementGroup and PartitionNumber of instance. --- plugins/modules/ec2_instance.py | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 2158bc8c39e..8264a22b56c 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -322,6 +322,9 @@ description: - The placement group that needs to be assigned to the instance. type: str + partition_number: + description: + - The number of the partition within the placement group - only valid with partition typle placement groups. metadata_options: description: - Modify the metadata options for the instance. @@ -1259,6 +1262,8 @@ def build_top_level_options(params): spec['Placement']['GroupName'] = str(params.get('placement_group')) else: spec.setdefault('Placement', {'GroupName': str(params.get('placement_group'))}) + if params.get('partition_number'): + spec['Placement']['PartitionNumber'] = int(params.get('partition_number')) if params.get('ebs_optimized') is not None: spec['EbsOptimized'] = params.get('ebs_optimized') if params.get('instance_initiated_shutdown_behavior'): @@ -1468,7 +1473,7 @@ def value_wrapper(v): InstanceId=instance['InstanceId'], SourceDestCheck={'Value': check}, )) - + return changes_to_apply @@ -1497,6 +1502,33 @@ def change_network_attachments(instance, params): return False +def change_placement(instance, params): + current_placement = instance['Placement'] + desired_placement = dict() + if params.get('placement_group'): + desired_placement.update({'GroupName':params.get('placement_group')}) + else: + desired_placement.update({'GroupName':current_placement['GroupName']}) + + if params.get('partition_number'): + desired_placement.update({'PartitionNumber':params.get('partition_number')}) + + if (( desired_placement['GroupName'] != current_placement['GroupName']) or + ( desired_placement['PartitionNumber'] and ( desired_placement['PartitionNumber'] != current_placement['PartitionNumber']))): + + try: + client.modify_instance_placement( + aws_retry=True, + InstanceId=instance['InstanceId'], + **desired_placement + ) + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: + module.fail_json_aws(e, msg="Could not change placement instance {0} to {1} - {2}".format(instance['InstanceId'], + desired_placement_group,desired_partition_number)) + return True + return False + + def find_instances(ids=None, filters=None): sanitized_filters = dict() @@ -1796,6 +1828,7 @@ def handle_existing(existing_matches, state, filters): changed |= bool(changes) changed |= add_or_update_instance_profile(existing_matches[0], module.params.get('iam_instance_profile')) changed |= change_network_attachments(existing_matches[0], module.params) + changed |= change_placement(existing_matches[0], module.params) altered = find_instances(ids=[i['InstanceId'] for i in existing_matches]) alter_config_result = dict( @@ -2016,6 +2049,7 @@ def main(): )), tenancy=dict(type='str', choices=['dedicated', 'default']), placement_group=dict(type='str'), + partition_number=dict(type='int', choices=[1,2,3,4,5,6,7]), instance_initiated_shutdown_behavior=dict(type='str', choices=['stop', 'terminate']), termination_protection=dict(type='bool'), hibernation_options=dict(type='bool', default=False), From 06a254481c8c605b1c0875567fb47f5c8752485b Mon Sep 17 00:00:00 2001 From: Daniel Riek Date: Sat, 12 Nov 2022 18:49:59 -0500 Subject: [PATCH 2/3] Fixing syntax. --- plugins/modules/ec2_instance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 8264a22b56c..05f413e933f 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1514,7 +1514,7 @@ def change_placement(instance, params): desired_placement.update({'PartitionNumber':params.get('partition_number')}) if (( desired_placement['GroupName'] != current_placement['GroupName']) or - ( desired_placement['PartitionNumber'] and ( desired_placement['PartitionNumber'] != current_placement['PartitionNumber']))): + ( 'PartitionNumber' in desired_placement and ( desired_placement['PartitionNumber'] != current_placement['PartitionNumber']))): try: client.modify_instance_placement( @@ -1523,7 +1523,7 @@ def change_placement(instance, params): **desired_placement ) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: - module.fail_json_aws(e, msg="Could not change placement instance {0} to {1} - {2}".format(instance['InstanceId'], + module.fail_json_aws(e, msg="Could not change placement instance {0} to {1}".format(instance['InstanceId'], desired_placement_group,desired_partition_number)) return True return False From e2747151bf32444e211e97b48caa68105b6c2eae Mon Sep 17 00:00:00 2001 From: Daniel Riek Date: Fri, 9 Dec 2022 06:05:19 -0500 Subject: [PATCH 3/3] the versioning scheme with -dev0 may be breaking the galaxy dependency resolution? Error was: Provided candidate =4.0.0 ,,, --- galaxy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galaxy.yml b/galaxy.yml index 79d0f879984..bbcd45362d6 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: amazon name: aws -version: 6.0.0-dev0 +version: 6.0.1 readme: README.md authors: - Ansible (https://github.com/ansible)