1

Here is the output.

"result.containers":[
   {
      "Image":"ca.docker/webproxy:1.0.0",
      "Names":[
         "/customer1"
      ]
   },
   {
      "Image":"docker.local/egacustomer:1.0.1",
      "Names":[
         "/webproxy"
      ]
   }
]

I'm trying to create a nested dictionary using jinja2. i'm trying to achieve the below using results.container and setfact.

"containerlist": "[webproxy:
                     name: customer1,
                   egacustomer:
                     name: webproxy]"

Here is my jinja2 code.

- set_fact:
      containerlist: |
           [
           {% for item in result.containers %}
           {{ item.Image | regex_replace('.*?/(.*?):.*', '\\1') }}:
                     'name': {{ item.Names | regex_replace("^/", "") }},
           {% endfor %}
         

Which throws the error. Could someone help me with the right Jinja2 Code.Any help would be greatly appreciated

"containerlist": "[\n\\1:\n          'name': ['/customer'],\n\\1:\n          'name': ['/webproxy'],\n,\n]\n"

1 Answer 1

3

For what you're trying to do I would normalize use an Ansible looping task rather than trying to use a Jinja {% for ... %} loop. For example, these tasks...

    - set_fact:
        container_list: >-
          {{
          container_list + [{
            item.Image.split('/')[-1].split(':')[0]:
              item.Names[0][1:]
          }]
          }}
      loop: "{{ result.containers }}"
      vars:
        container_list: []

    - debug:
        var: container_list

In the set_fact task, we're looping over result.containers, and for each iteration of the loop we redefine container_list as "the current contents of container_list + a new dictionary".

The above produces the following output:

TASK [set_fact] ****************************************************************
ok: [localhost] => (item={'Image': 'ca.docker/webproxy:1.0.0', 'Names': ['/customer1']})
ok: [localhost] => (item={'Image': 'docker.local/egacustomer:1.0.1', 'Names': ['/webproxy']})

TASK [debug] *******************************************************************
ok: [localhost] => {
    "container_list": [
        {
            "webproxy": "customer1"
        },
        {
            "egacustomer": "webproxy"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please help me change it to a dictionary instead of a list.
If you update your question to show the desired format, I can give it a shot.
Thanks,larsks. I could populate dictionary with the code. set_fact: containeruplist: "{{ containeruplist|default({}) | combine( {item.Image.split('/')[-1].split(':')[0]:item.Names[0][1:]} ) }}" loop: "{{ resultup | json_query('containers[*]') }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.