skip to Main Content

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


  1. Chosen as BEST ANSWER

    I found a solution to this myself. Here it is below how I did it.

    - name: Set fact
      set_fact:
        versions: "{{ versions|default({})|combine(item) }}"
      loop: "{{ eaphp_versions.stdout | from_yaml_all|list }}"
    
    - name: 'Set PHP memory_limit to 512M (all PHP versions)'
      #40
      shell: "/usr/sbin/whmapi1 php_ini_set_directives directive-1=memory_limit%3A512M version={{ item }}"
      with_items: "{{ versions.data.versions }}"
    

  2. Assume the data structure at top are the contents from eaphp_versions, then you can:

    - any-your-module:
        any-module-params: use of {{ item }}
      with_items: "{{ eaphp_versions.data.versions }}"
    

    The {{ item }} is one of the 3 versions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search