如何在jinja模板

时间:2016-12-05 23:26:38

标签: json ansible jinja2

这是我在Ansible, Boto, AWS - Invalid type for parameter containerDefinitions[0].memory收到的优秀答案的后续内容。

我不理解如何正确组合提供的答案。

在我最初的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 }}"

适用于没有entryPoint的服务的初始答案是:

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

以及如何处理entryPoint键的答案是:

entryPoint=('jav‌​a^-Xms'+java_heap_size_initial+'^-Xmx'+jav‌​a_heap_size_max+'^-D‌​logDir=/host^-Djava.‌​security.egd=file:/d‌​ev/./urandom^-D‌​com‌​.sun.net.ssl.chec‌​k‌​Revocation=false^-j‌‌​​ar^/app.jar').split‌​(‌​'^')

我没有得到的是如何在我的ansible / roles / ecs_cluster_init / tasks / main.yaml文件中将两者结合起来。如何将entryPoint和后续logConfiguration列表放入容器字典?

1 个答案:

答案 0 :(得分:2)

如果你的ansible≥1.8.4或者可以升级到该版本(https://github.com/ansible/ansible/issues/5865),你可以在memory附近添加引号:

memory: "{{ item.memory|int|abs|int }}"

但是如果你想这么做的话:

- name: Create ECS Service1 Task Definitions
  ecs_taskdefinition:
    region: "{{ region }}"
    containers: "{{'['+dict(name=item.name, cpu=0, image=item.image, memory=item.memory, entryPoint=[ 'java', '-Xms'+java_heap_size_initial, '-Xmx'+java_heap_size_max, '-DlogDir=/host', '-Djava.security.egd=file:/dev/./urandom', '-Dcom.sun.net.ssl.checkRevocation=false', '-jar', '/app.jar' ], logConfiguration=dict(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}))|to_json+']'}}"
  with_items: "{{ ecs_task_definitions }}"