title: Multiple ports and mount points in AWS ECS Fargate Task Definition using Ansible
body: <p>I went through the documentation provided here</p>
  <p><a href="https://docs.ansible.com/ansible/latest/collections/community/aws/ecs_taskdefinition_module.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/collections/community/aws/ecs_taskdefinition_module.html</a></p>
  <p>It gives me nice examples of setting of Fargate task definition. However it showcases example with only one port mapping and  there is no mount point shown  here.
  I want to dynamically add port mappings ( depending on my app) and volume/mount points</p>
  <p>For that I am defining my host_var for app as below ( there can be many such apps with different mount points and ports)</p>
  <pre><code>---
  task_count: 4
  task_cpu: 1028
  task_memory: 2056
  app_port: 8080
  
  </code></pre>
  <p>My Task definition yaml file looks like below</p>
  <pre><code>    - name: Create/Update Task Definition
        ecs_taskdefinition:
          aws_access_key: &quot;{{....}}&quot;
          aws_secret_key: &quot;{{....}}&quot;
          security_token: &quot;{{....}}&quot;
          region: &quot;{{....}}&quot;
          launch_type: FARGATE
          network_mode: awsvpc
          execution_role_arn: &quot;{{ ... }}&quot;
          task_role_arn: &quot;{{ ...}}&quot;
          containers:
            - name: &quot;{{...}}&quot;
              environment: &quot;{{...}}&quot;
              essential: true
              image: &quot;{{ ....}}&quot;
              logConfiguration: &quot;{{....}}&quot;
              portMappings:
                - containerPort: &quot;{{app_port}}&quot;
                  hostPort: &quot;{{app_port}}&quot;
  
          cpu: &quot;{{task_cpu}}&quot;
          memory: &quot;{{task_memory}}&quot;
          state: present
  
  </code></pre>
  <p>I am able to create/update the task definition.</p>
  <p><strong>New requirements are that</strong></p>
  <ol>
  <li>Instead of one port, now we can have multiple(or none) port mappings.</li>
  <li>We will have multiple (or none)  mount points  and volumes as well</li>
  </ol>
  <p>Here is what I think the modified ansible host_var should look like below for ports
  [container_port1:host_port1, container_port2:host_port2, container_port3:host_port3]</p>
  <pre><code>task_count: 4
  task_cpu: 1028
  task_memory: 2056
  #[container_port1:host_port1, container_port2:host_port2, container_port3:host_port3]
  app_ports: [8080:80, 8081:8081, 5703:5703]
   
  </code></pre>
  <p>I am  not sure what to do in ansible playbook to run through this list of ports.</p>
  <p>Another part of the problem is that, although I was able to achieve creating volume and mouting in container thorough aws console, I was not able to do same using ansible.</p>
  <p>here is the snippet of json for the AWS fargate looks like ( for volume part). There can be many such mounts depending on the application. I want to achieve that dynamically by defining mount points and volumes in host_vars</p>
  <pre><code>-
  -
  -
  
        &quot;mountPoints&quot;: [
          {
            &quot;readOnly&quot;: null,
            &quot;containerPath&quot;: &quot;/mnt/downloads&quot;,
            &quot;sourceVolume&quot;: &quot;downloads&quot;
          }
  -
  -
  -
  -
  -
  -
  &quot;volumes&quot;: [
      {
        &quot;efsVolumeConfiguration&quot;: {
          &quot;transitEncryptionPort&quot;: ENABLED,
          &quot;fileSystemId&quot;: &quot;fs-ecdg222d&quot;,
          &quot;authorizationConfig&quot;: {
            &quot;iam&quot;: &quot;ENABLED&quot;,
            &quot;accessPointId&quot;: null
          },
          &quot;transitEncryption&quot;: &quot;ENABLED&quot;,
          &quot;rootDirectory&quot;: &quot;/vol/downloads&quot;
        },
        &quot;name&quot;: &quot;downloads&quot;,
        &quot;host&quot;: null,
        &quot;dockerVolumeConfiguration&quot;: null
      }
  
  </code></pre>
  <p>I am not sure how to do that.
  Official documentation offers very little help.</p>