# Help with converting a dictionary into a list (jinja2)

**URL:** <https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [July 28, 2024, 9:08pm UTC](https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521 "2024-07-28T21:08:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Riha](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jose_riha/32/4328_2.png) [@Jose\_Riha](https://forum.ansible.com/u/Jose_Riha)\
**Post date:** [July 28, 2024, 9:08pm UTC](https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521/1 "2024-07-28T21:08:46Z")

</div>

Hello,

seeking help with the following issue. So far I’ve only dealt with flat structures in ansible but this time I have the following:

```auto
firewalld_zones:
  xxx:
    services:
      - sshd
      - chromecast
    sources:
      - 1.1.1.1/32
  yyy:
    services:
      - sshd
      - h5ai
      - apache
    sources:
      - 1.2.3.4/32
      - 5.6.7.8/32

```

What I need to do is create a task for every service inside every zone. I am able to achieve this using jinja2:

```auto
- name: Show zone -> service mapping
  debug:
    msg: "{{ item[0] }} => {{ item[1] }}"
  loop: >
    {%- set data = [] -%}
    {%- for zone in firewalld_zones -%}
    {%- for service in firewalld_zones.get(zone).services -%}
    {{- data.append([zone, service]) -}}
    {%- endfor -%}
    {%- endfor -%}
    {{ data }}

```

But I highly doubt this is a way how this problem should be treated (too much lines, difficult to read). Could someone please point me at the right direction?

Thanks, jose

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 29, 2024, 2:24am UTC](https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521/2 "2024-07-29T02:24:24Z")

</div>

This works.

```yml
---
# jose_riha-subelements.yml
- name: Testing subelements
  hosts: localhost
  gather_facts: false
  vars:
    firewalld_zones:
      xxx:
        services:
          - sshd
          - chromecast
        sources:
          - 1.1.1.1/32
      yyy:
        services:
          - sshd
          - h5ai
          - apache
        sources:
          - 1.2.3.4/32
          - 5.6.7.8/32
  tasks:
    - name: Loop over firewall_zones pairing subelement services
      ansible.builtin.debug:
        msg: "{{ item.0.key }} -> {{ item.1 }}"
      loop: "{{ firewalld_zones | dict2items | subelements('value.services') }}"

```

which produces:

```yml
TASK [Loop over firewall_zones pairing subelement services] ************************************************************************************************************
ok: [localhost] => (item=[{'key': 'xxx', 'value': {'services': ['sshd', 'chromecast'], 'sources': ['1.1.1.1/32']}}, 'sshd']) => {
    "msg": "xxx -> sshd"
}
ok: [localhost] => (item=[{'key': 'xxx', 'value': {'services': ['sshd', 'chromecast'], 'sources': ['1.1.1.1/32']}}, 'chromecast']) => {
    "msg": "xxx -> chromecast"
}
ok: [localhost] => (item=[{'key': 'yyy', 'value': {'services': ['sshd', 'h5ai', 'apache'], 'sources': ['1.2.3.4/32', '5.6.7.8/32']}}, 'sshd']) => {
    "msg": "yyy -> sshd"
}
ok: [localhost] => (item=[{'key': 'yyy', 'value': {'services': ['sshd', 'h5ai', 'apache'], 'sources': ['1.2.3.4/32', '5.6.7.8/32']}}, 'h5ai']) => {
    "msg": "yyy -> h5ai"
}
ok: [localhost] => (item=[{'key': 'yyy', 'value': {'services': ['sshd', 'h5ai', 'apache'], 'sources': ['1.2.3.4/32', '5.6.7.8/32']}}, 'apache']) => {
    "msg": "yyy -> apache"
}

```

---

<div class="post-metadata">

**Author:** ![Jose\_Riha](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jose_riha/32/4328_2.png) [@Jose\_Riha](https://forum.ansible.com/u/Jose_Riha)\
**Post date:** [July 29, 2024, 8:09pm UTC](https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521/3 "2024-07-29T20:09:54Z")

</div>

Thank you very much. That’s exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 28, 2024, 8:10pm UTC](https://forum.ansible.com/t/help-with-converting-a-dictionary-into-a-list-jinja2/7521/4 "2024-08-28T20:10:16Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
