Ansible, Boto, AWS - Invalid type for parameter co

2019-09-15 02:45发布

问题:

I have several services running in docker containers on Amazon ECS and am assigning percentages of the total system memory for each service.

In my ansible/roles/ecs_cluster_init/defaults/main.yaml file I have the following entries:

docker_memory_limit_service1: 17
docker_memory_limit_service2: 12
docker_memory_limit_service3: 16
docker_memory_limit_service4: 10
docker_memory_limit_service5: 16
docker_memory_limit_service6: 10
total_system_memory : 2048

Service1 should get 17% of the total system memory while service 4 only gets 10%. This is to easily accommodate changes in instance sizes.

In my ansible/roles/ecs_cluster_init/vars/main.yaml file I have (partial portion):

ecs_task_definitions:
  - name: "service1"
    elb: "{{ elb_creation_output.results[0].elb.dns_name }}"
    image: "{{ service1_docker_image }}"
    port: "{{ ports.service1 }}"
    memory: "{{ ( total_system_memory * ( docker_memory_limit_service1|int / 100 ))|int|abs }}"

To ensure clarity, the service memory value is turned into a percentage (by dividing by 100) and then determining that portion of the total system memory.

In my ansible/roles/ecs_cluster_init/tasks/main.yaml file I have:

## ECS Task and Service Definitions
- block:
    - name: Create ECS Service1 Task Definitions
      ecs_taskdefinition:
        region: "{{ region }}"
        containers:
          - name: "{{ item.name }}"
            cpu: 0
            essential: true
            image: "{{ item.image }}"
            memory: {{ item.memory|int|abs }}
            mountPoints: "{{ item.mounts }}"
            environment: "{{ item.env_vars }}"
            portMappings: "{{ item.portmap }}"
            entryPoint:
              - "java"
              - "-Xms{{ java_heap_size_initial }}"
              - "-Xmx{{ java_heap_size_max }}"
              - "-DlogDir=/host"
              - "-Dcom.sun.net.ssl.checkRevocation=false"
              - "-jar"
              - "/app.jar"
            logConfiguration:
              logDriver: "{{ ecs_task_log_configuration.logDriver }}"
              options:
                max-size: "{{ ecs_task_log_configuration.options.max_size }}"
                max-file: "{{ ecs_task_log_configuration.options.max_file }}"
        family: "{{ service_prefix }}-{{ item.name }}-{{ env_name }}"
        state: present
        increment_revision: true
        volumes: "{{ item.volumes }}"
      register: service1_task_definition
      with_items: "{{ ecs_task_definitions }}"

When I run the playbook for the role I receive the following error:

TASK [ecs_cluster_create : Create ECS Service1 Task Definitions] ****************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: Invalid type for parameter containerDefinitions[0].memory, value: 204, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>

Any idea what I'm doing wrong or where I'm missing specifying the memory value should be an int?

回答1:

It is a known issue with Ansible/Jinja that you can't preserve int type after templating.
In other words |int is integer inside double braces, but become string when you close braces.

But Ansible preserves complex types like dicts/list after templating, so you may do this trick:

- name: Create ECS Service1 Task Definitions
  ecs_taskdefinition:
    region: "{{ region }}"
    containers: "{{'['+dict(name=item.name, cpu=0, image=item.image, memory=item.memory|int)|to_json+']'}}"
  with_items: "{{ ecs_task_definitions }}"

I've shortened the example.

This way we construct a string that is a JSON-list which ansible converts after templating.