daemonofchaos
daemonofchaos

Reputation: 795

How to combine json dictionary and lists in jinja template

This is a follow-up to the excellent answer that I received in Ansible, Boto, AWS - Invalid type for parameter containerDefinitions[0].memory.

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

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

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‌​(‌​'^')

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?

Upvotes: 1

Views: 2557

Answers (1)

2ps
2ps

Reputation: 15946

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

Upvotes: 2

Related Questions