最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Ansible - flatten keysvalues muti-dimension dictionary - Stack Overflow

matteradmin8PV0评论

I have a multi-dimensional dictionary to configure where to grab images for multiple distributions.

- name: Test Playbook
  hosts: localhost
  gather_facts: false

  vars:

    distributions_images:
      Debian:
        12:
          name: bookworm
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
        11:
          name: bullseye
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
      Ubuntu:
        24:
          name: noble
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
            desktop: <url>
        22:
          name: jammy
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
            desktop: <url>
  tasks:

    - name: Get all images
      ansible.builtin.debug:
        var: distributions_images | <flatten_dict dim1='distribution', dim2='version'>

I would like to flatten all keys and value in order to have this kind of result:

- distribution: Debian
  version: 12
  name: bookworm
  cloud: <url>
  lxc: <url>
  classic: <url>
...
- distribution: Ubuntu
  version: 22
  name: jammy
  cloud: <url>
  lxc: <url>
  classic: <url>
  desktop: <url>

I would like a generic plugin in this purpose. Are there a generic plugin for that or to write a own one for this purpose?

Thanks

I have a multi-dimensional dictionary to configure where to grab images for multiple distributions.

- name: Test Playbook
  hosts: localhost
  gather_facts: false

  vars:

    distributions_images:
      Debian:
        12:
          name: bookworm
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
        11:
          name: bullseye
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
      Ubuntu:
        24:
          name: noble
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
            desktop: <url>
        22:
          name: jammy
          images:
            cloud: <url>
            lxc: <url>
            classic: <url>
            desktop: <url>
  tasks:

    - name: Get all images
      ansible.builtin.debug:
        var: distributions_images | <flatten_dict dim1='distribution', dim2='version'>

I would like to flatten all keys and value in order to have this kind of result:

- distribution: Debian
  version: 12
  name: bookworm
  cloud: <url>
  lxc: <url>
  classic: <url>
...
- distribution: Ubuntu
  version: 22
  name: jammy
  cloud: <url>
  lxc: <url>
  classic: <url>
  desktop: <url>

I would like a generic plugin in this purpose. Are there a generic plugin for that or to write a own one for this purpose?

Thanks

Share Improve this question asked Nov 17, 2024 at 14:56 Yves GuduszeitYves Guduszeit 11 silver badge 1
  • In JavaScript, you can use a neat trick to do this: 1. Do a join('') on the array to be flattened. 2. Do a split(',') on the returned string from last step. – Joe Johnson Commented Nov 17, 2024 at 19:05
Add a comment  | 

1 Answer 1

Reset to default 1

The easiest way is probably to recreate the data in the desired structure and then interpret it so that you can work with it in Ansible.

To do this, you can create valid YAML data using Jinja2 and then interpret it using the from_yaml filter.

Here is an example task to create the YAML data and store the interpreted data as fact distributions_images_new:

- name: Transform data into new datastructure
  set_fact:
    distributions_images_new: "{{ new_data | from_yaml }}"
  vars:
    new_data: >
      [
      {% for dist, dist_dict in distributions_images.items() %}
      {% for ver, ver_dict in dist_dict.items() %}
      {
      distribution: '{{ dist }}',
      version: '{{ ver }}',
      name: '{{ ver_dict.name }}',
      {% for k, v in ver_dict.images.items() %}
      {{ k }}: '{{ v }}',
      {% endfor %}
      },
      {% endfor %}
      {% endfor %}
      ]

The result would look like:

TASK [debug] ***************************
ok: [localhost] => {
    "distributions_images_new": [
        {
            "classic": "<url>",
            "cloud": "<url>",
            "distribution": "Debian",
            "lxc": "<url>",
            "name": "bookworm",
            "version": "12"
        },
        {
            "classic": "<url>",
            "cloud": "<url>",
            "distribution": "Debian",
            "lxc": "<url>",
            "name": "bullseye",
            "version": "11"
        },
        {
            "classic": "<url>",
            "cloud": "<url>",
            "desktop": "<url>",
            "distribution": "Ubuntu",
            "lxc": "<url>",
            "name": "noble",
            "version": "24"
        },
        {
            "classic": "<url>",
            "cloud": "<url>",
            "desktop": "<url>",
            "distribution": "Ubuntu",
            "lxc": "<url>",
            "name": "jammy",
            "version": "22"
        }
    ]
}

Possibility of expansion

In this code, you can also easily add default(...) in case an entry does not exist.

Example for the sub-key images:
{% for k, v in (ver_dict.images | default({})).items() %}

If images does not exist, you can define an empty dict ({}) as the default so that no error occurs.

Post a comment

comment list (0)

  1. No comments so far