diff --git a/changelogs/fragments/488-ecs_service-support_exec.yml b/changelogs/fragments/488-ecs_service-support_exec.yml new file mode 100644 index 00000000000..02b76b03b62 --- /dev/null +++ b/changelogs/fragments/488-ecs_service-support_exec.yml @@ -0,0 +1,2 @@ +minor_changes: +- ecs_service - added new parameter ``enable_execute_command`` (https://github.com/ansible-collections/community.aws/pull/488). diff --git a/plugins/modules/ecs_service.py b/plugins/modules/ecs_service.py index 4907187f3ab..4096cee1486 100644 --- a/plugins/modules/ecs_service.py +++ b/plugins/modules/ecs_service.py @@ -273,6 +273,13 @@ type: dict required: false version_added: 4.1.0 + enable_execute_command: + description: + - Whether or not to enable the execute command functionality for the containers in the ECS task. + - If I(enable_execute_command=true) execute command functionality is enabled on all containers in the ECS task. + required: false + type: bool + version_added: 5.3.0 extends_documentation_fragment: - amazon.aws.aws - amazon.aws.ec2 @@ -758,6 +765,9 @@ def is_matching_service(self, expected, existing): if boto3_tag_list_to_ansible_dict(existing.get('tags', [])) != (expected['tags'] or {}): return False + if (expected['enable_execute_command'] or False) != existing.get('enableExecuteCommand', False): + return False + # expected is params. DAEMON scheduling strategy returns desired count equal to # number of instances running; don't check desired count if scheduling strat is daemon if (expected['scheduling_strategy'] != 'DAEMON'): @@ -766,11 +776,14 @@ def is_matching_service(self, expected, existing): return True - def create_service(self, service_name, cluster_name, task_definition, load_balancers, - desired_count, client_token, role, deployment_controller, deployment_configuration, - placement_constraints, placement_strategy, health_check_grace_period_seconds, - network_configuration, service_registries, launch_type, platform_version, - scheduling_strategy, capacity_provider_strategy, tags, propagate_tags): + def create_service( + self, service_name, cluster_name, task_definition, load_balancers, + desired_count, client_token, role, deployment_controller, deployment_configuration, + placement_constraints, placement_strategy, health_check_grace_period_seconds, + network_configuration, service_registries, launch_type, platform_version, + scheduling_strategy, capacity_provider_strategy, tags, propagate_tags, + enable_execute_command, + ): params = dict( cluster=cluster_name, @@ -816,13 +829,19 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan if scheduling_strategy: params['schedulingStrategy'] = scheduling_strategy + if enable_execute_command: + params['enableExecuteCommand'] = enable_execute_command + response = self.ecs.create_service(**params) return self.jsonize(response['service']) - def update_service(self, service_name, cluster_name, task_definition, desired_count, - deployment_configuration, placement_constraints, placement_strategy, - network_configuration, health_check_grace_period_seconds, - force_new_deployment, capacity_provider_strategy, load_balancers): + def update_service( + self, service_name, cluster_name, task_definition, desired_count, + deployment_configuration, placement_constraints, placement_strategy, + network_configuration, health_check_grace_period_seconds, + force_new_deployment, capacity_provider_strategy, load_balancers, + enable_execute_command, + ): params = dict( cluster=cluster_name, service=service_name, @@ -848,6 +867,8 @@ def update_service(self, service_name, cluster_name, task_definition, desired_co # desired count is not required if scheduling strategy is daemon if desired_count is not None: params['desiredCount'] = desired_count + if enable_execute_command is not None: + params['enableExecuteCommand'] = enable_execute_command if load_balancers: params['loadBalancers'] = load_balancers @@ -940,6 +961,7 @@ def main(): ), propagate_tags=dict(required=False, choices=['TASK_DEFINITION', 'SERVICE']), tags=dict(required=False, type='dict'), + enable_execute_command=dict(required=False, type='bool'), ) module = AnsibleAWSModule(argument_spec=argument_spec, @@ -1049,43 +1071,46 @@ def main(): updatedLoadBalancers = loadBalancers if existing['deploymentController']['type'] == 'ECS' else [] # update required - response = service_mgr.update_service(module.params['name'], - module.params['cluster'], - task_definition, - module.params['desired_count'], - deploymentConfiguration, - module.params['placement_constraints'], - module.params['placement_strategy'], - network_configuration, - module.params['health_check_grace_period_seconds'], - module.params['force_new_deployment'], - capacityProviders, - updatedLoadBalancers, - ) - + response = service_mgr.update_service( + module.params['name'], + module.params['cluster'], + task_definition, + module.params['desired_count'], + deploymentConfiguration, + module.params['placement_constraints'], + module.params['placement_strategy'], + network_configuration, + module.params['health_check_grace_period_seconds'], + module.params['force_new_deployment'], + capacityProviders, + updatedLoadBalancers, + module.params['enable_execute_command'], + ) else: try: - response = service_mgr.create_service(module.params['name'], - module.params['cluster'], - module.params['task_definition'], - loadBalancers, - module.params['desired_count'], - clientToken, - role, - deploymentController, - deploymentConfiguration, - module.params['placement_constraints'], - module.params['placement_strategy'], - module.params['health_check_grace_period_seconds'], - network_configuration, - serviceRegistries, - module.params['launch_type'], - module.params['platform_version'], - module.params['scheduling_strategy'], - capacityProviders, - module.params['tags'], - module.params['propagate_tags'], - ) + response = service_mgr.create_service( + module.params['name'], + module.params['cluster'], + module.params['task_definition'], + loadBalancers, + module.params['desired_count'], + clientToken, + role, + deploymentController, + deploymentConfiguration, + module.params['placement_constraints'], + module.params['placement_strategy'], + module.params['health_check_grace_period_seconds'], + network_configuration, + serviceRegistries, + module.params['launch_type'], + module.params['platform_version'], + module.params['scheduling_strategy'], + capacityProviders, + module.params['tags'], + module.params['propagate_tags'], + module.params['enable_execute_command'], + ) except botocore.exceptions.ClientError as e: module.fail_json_aws(e, msg="Couldn't create service") diff --git a/tests/integration/targets/ecs_cluster/tasks/main.yml b/tests/integration/targets/ecs_cluster/tasks/main.yml index a86ecdde3a8..3ca08252093 100644 --- a/tests/integration/targets/ecs_cluster/tasks/main.yml +++ b/tests/integration/targets/ecs_cluster/tasks/main.yml @@ -299,6 +299,31 @@ that: - not ecs_service_again.changed + - name: Enable ExecuteCommand + ecs_service: + state: present + name: "{{ ecs_service_name }}" + cluster: "{{ ecs_cluster_name }}" + task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}" + desired_count: 1 + deployment_configuration: "{{ ecs_service_deployment_configuration }}" + placement_strategy: "{{ ecs_service_placement_strategy }}" + placement_constraints: + - type: distinctInstance + health_check_grace_period_seconds: "{{ ecs_service_health_check_grace_period }}" + load_balancers: + - targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}" + containerName: "{{ ecs_task_name }}" + containerPort: "{{ ecs_task_container_port }}" + role: "ecsServiceRole" + enable_execute_command: True + register: ecs_service_again + + - name: check that ECS service recreation changed nothing + assert: + that: + - ecs_service_again.changed + - name: create same ECS service definition via force_new_deployment ecs_service: state: present