Reputation: 31
I do a debug from a lookup file (a dict):
- Name: debugging
debug:
msg:"{{ linux_file_list | dict2items }}"
and I receive the following:
TASK [reposync : debugging] *******************************************************************************************************************************************************
ok: [] => {
"msg": [
[
{
"key": "list_of_linux",
"value": [
{
"description": "linux SLES",
"enabled": 1,
"version": 15,
"name": "sles"
},
{
"description": "linux RHEL",
"enabled": 1,
"version": 8.4,
"name": "rhel"
}
]
}
]
]
}
First of all, I'd like to know what I think is correct: It is a dict and the value of dict is two lists and each element of these lists are a dict. It is correct? Second, I need to be able to iterate on each element to get the data for each linux type defined by 'name'. So something like that:
"name: {{ item['name'] }}"
"description: {{ item['description'] }}"
"version: {{ item['version'] }}"
loop: "{{ list_of_linux }}"
I don't know if my approach is right but I thought to create a dict and define name as a key, like that:
- set_fact:
list_of_linux: "{{ dict(_keys(linux_file_list['value'])) }}"
vars:
_keys: "{{ item['value']|map(attribute='name')|list }}"
loop: "{{ linux_file_list | dict2items }}"
But it doesn't run and I don't see how to manipulate these data... Could someone help me to understand how to retrieve dict value from list
Here're additional information:
Here's my original file:
---
List_of_linux
- name: sles
description: "Linux SLES"
enabled: 1
version: 15
- name: rhel
description: "Linux RHEL"
enabled: 1
version: 8.4
- …
This file is extern and I received it through a lookup and put it into a variable named : linux_file_list. This file becomes a dict. I've check that through type_debug and here's the simple output of variable, like: msg: "{{ linux_file_list }}"
Commands check:
- debug:
msg: "{{ linux_file_list | type_debug }}"
- debug:
msg: "{{ linux_file_list }}"
Results:
ok: [<server>] => {
"msg": ": dict"
ok: [<server>] => {
"msg": {'list_of_linux': [{'name': 'sles', 'description': 'Linux SLES', 'enabled': 1, 'version': 15}, {'name': 'rhel', 'description': 'Linux RHEL', 'enabled': 1, 'version': 8.4}]}"
}
Thank you in advance for your help H
Upvotes: 1
Views: 351
Reputation: 17007
Following your starting structure:
- hosts: localhost
gather_facts: no
vars:
linux_file_list: "{{ lookup('file', './variables.yml') | from_yaml }}"
tasks:
- debug:
msg: |
description: {{ item.description }}
name: {{ item.name }}
version: {{ item.version }}
loop: "{{ linux_file_list.List_of_linux }}"
result:
ok: [localhost] => (item={'name': 'sles', 'description': 'Linux SLES', 'enabled': 1, 'version': 15}) => {
"msg": "description: Linux SLES\nname: sles\nversion: 15\n"
}
ok: [localhost] => (item={'name': 'rhel', 'description': 'Linux RHEL', 'enabled': 1, 'version': 8.4}) => {
"msg": "description: Linux RHEL\nname: rhel\nversion: 8.4\n"
}
Upvotes: 1