如何在 jinja 模板中结合 json 字典和列表 [英] How to combine json dictionary and lists in jinja template

查看:26
本文介绍了如何在 jinja 模板中结合 json 字典和列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Ansible, Boto, AWS - 参数 containerDefinitions[0].memory 的无效类型.

我不明白如何正确组合提供的答案.

I am not understanding how to combine the provided answer(s) properly.

在我原来的 ansible/roles/ecs_cluster_init/tasks/main.yaml 文件中:

In my original 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 }}"

适用于没有入口点的服务的最初答案是:

The initial answer that works for the services without an entryPoint was to do:

- 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 键的答案是:

and the answer for how to handle the entryPoint key was:

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 列表放入容器字典中?

What I'm not getting is how to combine the two in my ansible/roles/ecs_cluster_init/tasks/main.yaml file. How do I get the entryPoint and subsequent logConfiguration lists into the container dictionary?

推荐答案

如果您使用的是 ansible ≥ 1.8.4 或可以升级到该版本 (https://github.com/ansible/ansible/issues/5865),你可以在memory周围添加引号:

If you are on ansible ≥ 1.8.4 or can upgrade to that version (https://github.com/ansible/ansible/issues/5865), you can just add quotes around memory:

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

但如果你想以艰难的方式做到这一点:

But if you want to do this the hard way:

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

这篇关于如何在 jinja 模板中结合 json 字典和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆