admin管理员组文章数量:1026191
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
1 Answer
Reset to default 1The 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.
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
1 Answer
Reset to default 1The 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.
本文标签: Ansibleflatten keysvalues mutidimension dictionaryStack Overflow
版权声明:本文标题:Ansible - flatten keysvalues muti-dimension dictionary - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745635214a2160405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论