Ansible,Boto,AWS - 参数containerDefinitions [0] .memory

时间:2016-12-01 16:28:10

标签: ansible boto3 amazon-ecs

我在Amazon ECS上的docker容器中运行了多个服务,并为每个服务分配了系统总内存的百分比。

在我的ansible / roles / ecs_cluster_init / defaults / main.yaml文件中,我有以下条目:

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应占总系统内存的17%,而服务4仅占10%。这可以轻松适应实例大小的变化。

在我的ansible / roles / ecs_cluster_init / vars / main.yaml文件中,我有(部分):

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 }}"

为确保清晰,将服务内存值转换为百分比(除以100),然后确定整个系统内存的那一部分。

在我的ansible / roles / ecs_cluster_init / tasks / main.yaml文件中,我有:

## 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 }}"

当我为角色运行playbook时,我收到以下错误:

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'>

知道我做错了什么或者我缺少指定内存值应该是一个int吗?

1 个答案:

答案 0 :(得分: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.