Skip to content

Commit

Permalink
Added assignPublicIp param in network_configuration (ansible-collecti…
Browse files Browse the repository at this point in the history
…ons#395)

* added assign_public_ip feature
* fix sanity issues and added changelog

Co-authored-by: Mark Chappell <[email protected]>
  • Loading branch information
Sakar and tremble authored Mar 29, 2021
1 parent ef64bbd commit 5300ce1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/395_add_assign_public_ip.yaml
Original file line number Diff line number Diff line change
@@ -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).
39 changes: 37 additions & 2 deletions plugins/modules/ecs_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/targets/ecs_cluster/tasks/full_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5300ce1

Please sign in to comment.