<p>I've been trying to find a way to take a data collection that contains three fields where two of the fields can contain multiple items and convert/expand that data to a list where a row is added for every item in one of the fields.</p>
<p>The data is about kubernetes pods and the containers and images inside them.  I can get the raw data just fine I just can't transform it.</p>
<p>the json data is this:</p>
<pre><code>
  - name: condense the raw data
    set_fact:
      pod_data: &quot;{{ pod_data|default([]) + [{'pod_name':item.pod_name,'container_name':item.container_name,'image_path':item.image_path }] }}&quot;
    with_items: &quot;{{ podinfo | json_query('resources[*].{pod_name: metadata.name, container_name: spec.containers[*].name, image_path: spec.containers[*].image }') }}&quot;

  - debug: var=pod_data

&quot;pod_data&quot;: [
        {
            &quot;container_name&quot;: [
                &quot;container1&quot;,
                &quot;container2&quot;
            ],
            &quot;image_path&quot;: [
                &quot;path1&quot;,
                &quot;path2&quot;
            ],
            &quot;pod_name&quot;: &quot;pod1&quot;
        },
        {
            &quot;container_name&quot;: [
                &quot;container3&quot;,
                &quot;container4&quot;
            ],
            &quot;image_path&quot;: [
                &quot;path3&quot;,
                &quot;path4&quot;
            ],
            &quot;pod_name&quot;: &quot;pod2&quot;
        }
</code></pre>
<p>I want to output the data in a 1:1 format like this:</p>
<pre><code>&quot;pod_data&quot;: [
        {
            &quot;container_name&quot;:  &quot;container1&quot;
            ,
            &quot;image_path&quot;: &quot;path1&quot;
            ,
            &quot;pod_name&quot;: &quot;pod1&quot;
        },
        {
            &quot;container_name&quot;: &quot;container2&quot;
            ,
            &quot;image_path&quot;: &quot;path2&quot;
            ],
            &quot;pod_name&quot;: &quot;pod1&quot;
        },
        {
            &quot;container_name&quot;: &quot;container3&quot;
            ,
            &quot;image_path&quot;: &quot;path3&quot;
            ,
            &quot;pod_name&quot;: &quot;pod2&quot;
        }
</code></pre>
<p>I'm not good enough with loops to know how to do a nested style loop when there are multiple sub elements in ansible.  I could do it in powershell but that doesn't help me here.</p>
<p>Is my only choice some sort of include_tasks loop while passing each item and container/image count so I can split it that many times?</p>

VTXAD123
<p>Create the structure</p>
<pre class="lang-yaml prettyprint-override"><code>  pod_data_update: |
    [{% for i in pod_data %}
    {% for n,p in i.container_name|zip(i.image_path) %}
    {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
    {% endfor %}
    {% endfor %}]
</code></pre>
<p>, convert it from YAML, and update <em>pod_data</em></p>
<pre class="lang-yaml prettyprint-override"><code>    - set_fact:
        pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;
</code></pre>
<p>gives what you want</p>
<pre class="lang-yaml prettyprint-override"><code>  pod_data:
    - {container_name: container1, image_path: path1, pod_name: pod1}
    - {container_name: container2, image_path: path2, pod_name: pod1}
    - {container_name: container3, image_path: path3, pod_name: pod2}
    - {container_name: container4, image_path: path4, pod_name: pod2}
</code></pre>
<hr>
<sup>
<p>Example of a complete playbook for testing</p>
<pre class="lang-yaml prettyprint-override"><code>- hosts: localhost

  vars:

    pod_data:
    - container_name:
      - container1
      - container2
      image_path:
      - path1
      - path2
      pod_name: pod1
    - container_name:
      - container3
      - container4
      image_path:
      - path3
      - path4
      pod_name: pod2

    pod_data_update: |
      [{% for i in pod_data %}
      {% for n,p in i.container_name|zip(i.image_path) %}
      {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
      {% endfor %}
      {% endfor %}]

  tasks:

    - set_fact:
        pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;
    - debug:
        var: pod_data|to_yaml
</code></pre>
</sup>
