I have the output of a command:
---
data:
versions:
- alt-php53
- ea-php56
- ea-php72
metadata:
command: php_get_installed_versions
reason: OK
result: 1
version: 1
And I need to iterate through the values of the versions key.
I have this to assign the output to a variable:
- name: Get PHP versions
shell: "/usr/sbin/whmapi1 php_get_installed_versions"
register: eaphp_versions
But I do not know how exactly to iterate through those values. I tried various methods with dict2items, from_yaml, from_yaml_all using loop, I tried sub_elements but none worked.
The closest I got is the below:
- name: 'Apply stuff'
shell: "echo the item is {{ item }} >> report.txt"
loop: "{{ eaphp_versions.stdout | from_yaml_all | list }}"
But judging from the output it iterates through the whole output as one item:
the item is {udata: {uversions: [ualt-php53, uea-php56, uea-php72]}, umetadata: {ureason: uOK, uversion: 1, ucommand: uphp_get_installed_versions, uresult: 1}}
Please help.
Thanks in advance.
2
Answers
I found a solution to this myself. Here it is below how I did it.
Assume the data structure at top are the contents from eaphp_versions, then you can:
The {{ item }} is one of the 3 versions.