# How a custom list can be created a using templates with ansible 12?

**URL:** <https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497>\
**Category:** Get Help\
**Tags:** templating\
**Created:** [September 16, 2025, 11:14am UTC](https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497 "2025-09-16T11:14:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roberto\_Majadas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/roberto_majadas/32/9433_2.png) [@Roberto\_Majadas](https://forum.ansible.com/u/Roberto_Majadas)\
**Post date:** [September 16, 2025, 11:14am UTC](https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497/1 "2025-09-16T11:14:14Z")

</div>

In my playbooks I sometimes render a list of dicts in order to be used in a variable or in a loop.  
Something like this:

```auto
- name: Configure repository's git_config
  git_config:
    name: "{{ item.config_name }}"
    value: "{{ item.config_value }}"
    scope: local
    repo: "{{ item.path }}"
  loop: >
    [
      {% for repo_name, repo_data in src_repositories_to_deploy.items() %}
        {% for git_config_key, git_config_value in repo_data.git_config.items() %}
          {
            "repo_name": "{{repo_name}}",
            "path": "{{ src_home }}/{{repo_name}}",
            "config_name": "{{ git_config_key }}",
            "config_value": "{{ git_config_value }}",
          },
        {% endfor %}
      {% endfor %}
    ]

```

This behaviour seems to be changed in ansible 12 and it returns a string instead a list.

What would be the correct way in ansible12? Could it be compatible with the previous ansible version?

---

<div class="post-metadata">

**Author:** ![mkrizek](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mkrizek/32/2055_2.png) [@mkrizek](https://forum.ansible.com/u/mkrizek)\
**Post date:** [September 16, 2025, 11:31am UTC](https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497/2 "2025-09-16T11:31:24Z")

</div>

See [Ansible 2.19.0 breaks `loop`-templates because of jinja native types. · Issue #85605 · ansible/ansible · GitHub](https://github.com/ansible/ansible/issues/85605)

---

<div class="post-metadata">

**Author:** ![Roberto\_Majadas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/roberto_majadas/32/9433_2.png) [@Roberto\_Majadas](https://forum.ansible.com/u/Roberto_Majadas)\
**Post date:** [September 16, 2025, 1:01pm UTC](https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497/3 "2025-09-16T13:01:47Z")

</div>

Thanks @mkrizek! Following the link that he posted I rewrote the example and it works now. I’m going to post here the code that I rewrote just in case someone has a similar issue.

```auto
- name: Configure repository's git_config
  git_config:
    name: "{{ item.config_name }}"
    value: "{{ item.config_value }}"
    scope: local
    repo: "{{ item.path }}"
  loop: |
    {%- set result = [] -%}
    {%- for repo_name, repo_data in src_repositories_to_deploy.items() -%}
      {%- for git_config_key, git_config_value in repo_data.git_config.items() -%}
        {%- set _ = result.append({
              "repo_name": repo_name,
              "path": src_home + "/" + repo_name,
              "config_name": git_config_key,
              "config_value": git_config_value,
            })
        -%}
      {%- endfor -%}
    {%- endfor -%}
    {{ result }}

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 16, 2025, 4:11pm UTC](https://forum.ansible.com/t/how-a-custom-list-can-be-created-a-using-templates-with-ansible-12/44497/4 "2025-09-16T16:11:07Z")

</div>

FWIW, given the dictionary

```yaml
    deploy:
      repo_A:
        git_config:
          {key1: v1, key2: v2}
      repo_B:
        git_config:
          {key3: v3, key4: v4}

```

Convert the _git\_config_ dictionaries to lists

```yaml
names: "{{ deploy | json_query('keys(@)') }}"
config: "{{ deploy | json_query('*.git_config') | map('dict2items') }}"
names_config: "{{ dict(names | zip(config)) }}"

```

gives

```yaml
    names_config:
        repo_A:
        - key: key1
            value: v1
        - key: key2
            value: v2
        repo_B:
        - key: key3
            value: v3
        - key: key4
            value: v4

```

Then, iterate _subelements_

```yaml
    - debug:
        msg: |
          repo: {{ item.0.key }} name: {{ item.1.key }} value: {{ item.1.value }}
      loop: "{{ names_config | dict2items | subelements('value') }}"

```

gives (abridged)

```yaml
        repo: repo_A name: key1 value: v1
        repo: repo_A name: key2 value: v2
        repo: repo_B name: key3 value: v3
        repo: repo_B name: key4 value: v4

```
