From 152c8db8106b232c14b8c3557a9cfeb51c701095 Mon Sep 17 00:00:00 2001 From: Sakar Date: Mon, 29 Mar 2021 16:26:04 +0530 Subject: [PATCH] Added assignPublicIp param in network_configuration (#395) * added assign_public_ip feature * fix sanity issues and added changelog Co-authored-by: Mark Chappell --- .../fragments/395_add_assign_public_ip.yaml | 3 ++ plugins/modules/ecs_task.py | 39 ++++++++++++++++++- .../targets/ecs_cluster/tasks/full_test.yml | 15 +++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/395_add_assign_public_ip.yaml diff --git a/changelogs/fragments/395_add_assign_public_ip.yaml b/changelogs/fragments/395_add_assign_public_ip.yaml new file mode 100644 index 00000000000..29540bc7b30 --- /dev/null +++ b/changelogs/fragments/395_add_assign_public_ip.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- ecs_task - added ``assign_public_ip`` option for network_configuration (https://github.com/ansible-collections/community.aws/pull/395). diff --git a/plugins/modules/ecs_task.py b/plugins/modules/ecs_task.py index 90f9df43f01..03295c16eac 100644 --- a/plugins/modules/ecs_task.py +++ b/plugins/modules/ecs_task.py @@ -64,8 +64,13 @@ network_configuration: description: - Network configuration of the service. Only applicable for task definitions created with I(network_mode=awsvpc). + - I(assign_public_ip) requires botocore >= 1.8.4 type: dict suboptions: + assign_public_ip: + description: Whether the task's elastic network interface receives a public IP address. + type: bool + version_added: 1.5.0 subnets: description: A list of subnet IDs to which the task is attached. type: list @@ -142,6 +147,21 @@ - my_security_group register: task_output +- name: RUN a task on Fargate with public ip assigned + community.aws.ecs_task: + operation: run + count: 2 + cluster: console-sample-app-static-cluster + task_definition: console-sample-app-static-taskdef + task: "arn:aws:ecs:us-west-2:172139249013:task/3f8353d1-29a8-4689-bbf6-ad79937ffe8a" + started_by: ansible_user + launch_type: FARGATE + network_configuration: + assign_public_ip: yes + subnets: + - subnet-abcd1234 + register: task_output + - name: Stop a task community.aws.ecs_task: operation: stop @@ -248,6 +268,12 @@ def format_network_configuration(self, network_config): except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: self.module.fail_json_aws(e, msg="Couldn't look up security groups") result['securityGroups'] = groups + if 'assign_public_ip' in network_config: + if network_config['assign_public_ip'] is True: + result['assignPublicIp'] = "ENABLED" + else: + result['assignPublicIp'] = "DISABLED" + return dict(awsvpcConfiguration=result) def list_tasks(self, cluster_name, service_name, status): @@ -331,6 +357,12 @@ def ecs_api_handles_network_configuration(self): # to e.g. ecs.run_task, it's just passed as a keyword argument) return self.module.botocore_at_least('1.7.44') + def ecs_api_handles_network_configuration_assignIp(self): + # There doesn't seem to be a nice way to inspect botocore to look + # for attributes (and networkConfiguration is not an explicit argument + # to e.g. ecs.run_task, it's just passed as a keyword argument) + return self.module.botocore_at_least('1.8.4') + def main(): argument_spec = dict( @@ -373,8 +405,11 @@ def main(): service_mgr = EcsExecManager(module) - if module.params['network_configuration'] and not service_mgr.ecs_api_handles_network_configuration(): - module.fail_json(msg='botocore needs to be version 1.7.44 or higher to use network configuration') + if module.params['network_configuration']: + if 'assignPublicIp' in module.params['network_configuration'] and not service_mgr.ecs_api_handles_network_configuration_assignIp(): + module.fail_json(msg='botocore needs to be version 1.8.4 or higher to use assign_public_ip in network_configuration') + elif not service_mgr.ecs_api_handles_network_configuration(): + module.fail_json(msg='botocore needs to be version 1.7.44 or higher to use network configuration') if module.params['launch_type'] and not service_mgr.ecs_api_handles_launch_type(): module.fail_json(msg='botocore needs to be version 1.8.4 or higher to use launch type') diff --git a/tests/integration/targets/ecs_cluster/tasks/full_test.yml b/tests/integration/targets/ecs_cluster/tasks/full_test.yml index c874053aedc..a463fa5de0d 100644 --- a/tests/integration/targets/ecs_cluster/tasks/full_test.yml +++ b/tests/integration/targets/ecs_cluster/tasks/full_test.yml @@ -759,6 +759,21 @@ started_by: ansible_user register: fargate_run_task_output_with_tags + - name: create fargate ECS task with run task and assign public ip disable + ecs_task: + operation: run + cluster: "{{ ecs_cluster_name }}" + task_definition: "{{ ecs_task_name }}-vpc" + launch_type: FARGATE + count: 1 + network_configuration: + subnets: "{{ setup_subnet.results | community.general.json_query('[].subnet.id') }}" + security_groups: + - '{{ setup_sg.group_id }}' + assign_public_ip: false + started_by: ansible_user + register: fargate_run_task_output_with_assign_ip + # ============================================================ # End tests for Fargate