Ansible,Boto,AWS-参数containerDefinitions [0] .memory的类型无效 [英] Ansible, Boto, AWS - Invalid type for parameter containerDefinitions[0].memory
问题描述
我在Amazon ECS的docker容器中运行了几项服务,并正在为每种服务分配总系统内存的百分比.
I have several services running in docker containers on Amazon ECS and am assigning percentages of the total system memory for each service.
在我的ansible/roles/ecs_cluster_init/defaults/main.yaml文件中,我具有以下条目:
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应该获得系统总内存的17%,而Service 4仅获得10%.这是为了轻松适应实例大小的更改.
Service1 should get 17% of the total system memory while service 4 only gets 10%. This is to easily accommodate changes in instance sizes.
在我的ansible/roles/ecs_cluster_init/vars/main.yaml文件中,我有(部分):
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 }}"
为确保清晰起见,将服务内存值转换为百分比(除以100),然后确定总系统内存的那一部分.
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.
在我的ansible/roles/ecs_cluster_init/tasks/main.yaml文件中,我有:
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?
推荐答案
这是Ansible/Jinja的一个已知问题,您无法在模板化后保留int
类型.
换句话说,|int
是双括号内的整数,但是当您关闭括号时变为字符串.
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.
但是Ansible在模板化后会保留诸如dicts/list之类的复杂类型,因此您可以执行以下操作:
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 }}"
我已经简化了示例.
通过这种方式,我们构造了一个字符串,该字符串是JSON列表,在模板化后ansible会进行转换.
This way we construct a string that is a JSON-list which ansible converts after templating.
这篇关于Ansible,Boto,AWS-参数containerDefinitions [0] .memory的类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!