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

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:

- 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?

See Ansible 2.19.0 breaks `loop`-templates because of jinja native types. · Issue #85605 · ansible/ansible · GitHub

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.

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

FWIW, given the dictionary

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

Convert the git_config dictionaries to lists

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

gives

    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

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

gives (abridged)

        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
1 Like