Darwick
Darwick

Reputation: 350

ansible loop over list with multiple items

I have a definition of an array in an ansible playbook, which looks like:

x_php_versions:
  - php71
  - php72
  - php73

And I would like to make a loop for all of them, so my task looks like this:

- name: Install PHP packages
  yum: 
    name: '{{ item }}'
    state: 'present'
  loop:
    - '{{ x_php_versions }}'
    - '{{ x_php_versions }}-bcmath'
    - '{{ x_php_versions }}-bz2'

This should install php71 php71-bcmath php71-bz2 php72 php72-bcmath ... and so on. But this kind of loop does not work. Am I made a typo or my loop is completly wrong for this scenario? If I try the loop without '' then it also gave me an error.

Upvotes: 0

Views: 888

Answers (1)

Baptiste Mille-Mathias
Baptiste Mille-Mathias

Reputation: 2179

you can use the filter product to combine all possibilities

- name: Install PHP packages
  yum: 
    name: '{{ item.0 }}{{ item.1 }}'
    state: 'present'
  loop: "{{ x_php_versions | product(['', '-bcmath', '-bz2']) | list }}"

Upvotes: 3

Related Questions