1

I'm trying to find the most efficient way to install every PHP version and packages for each version without duplication.

I currently have this:

- name: Install PHP
  apt:
    name: "{{ packages.php_version }}-{{ packages.packages }}"
  vars:
    packages:
      - { php_version: 5.6, packages: ['common', 'cli', 'opcache', 'mysql', 'mbstring', 'simplexml', 'curl', 'bcmath', 'ldap', 'gd', 'dom', 'zip', 'xml', 'readline', 'json', 'fpm', 'imagick']}
      - { php_version: 7.0, packages: ['common', 'cli', 'opcache', 'mysql', 'mbstring', 'simplexml', 'curl', 'bcmath', 'ldap', 'gd', 'dom', 'zip', 'xml', 'readline', 'json', 'fpm', 'imagick']}

However, i cannot workout the selector to itterate over the packages.

I would like to have something where it basically loops over each php version and the packages for each, as they may be different.

Thanks

EDIT TO ANSWER Pretty much there for me! Thanks so much. I wanted to install all the PHP versions just just a selected one. So i did the following:

- name: Install PHP
  apt:
    name: "php{{ item.0.key }}-{{ item.1 }}"
  loop: "{{ packages_dict | dict2items | subelements('value') }}"

Not sure if thats the best way? seems inefficient really but it works!

1 Answer 1

1

The most efficient way would be converting the list to a dictionary first. For example

  packages_dict: "{{ packages|items2dict(key_name='php_version',
                                         value_name='packages') }}"

gives (abridged)

  packages_dict:
    '5.6':
    - common
    - cli
    - opcache
    '7.0':
    - common
    - cli
    - opcache

Then the selection and iteration is trivial, e.g.

- hosts: localhost
  vars:
    packages:
      - php_version: '5.6'
        packages: ['common', 'cli', 'opcache']
      - php_version: '7.0'
        packages: ['common', 'cli', 'opcache']
    packages_dict: "{{ packages|items2dict(key_name='php_version',
                                           value_name='packages') }}"
    php_version: "{{ my_php_version|d('5.6') }}"
  tasks:
    - name: Install PHP
      debug:
        msg: "name: {{ php_version }}-{{ item }}"
      loop: "{{ packages_dict[php_version] }}"

gives

  msg: 'name: 5.6-common'
  msg: 'name: 5.6-cli'
  msg: 'name: 5.6-opcache'

Q: "I wanted to install ALL php versions."

A: No conversion is needed in this case. You can iterate the list packages with subelements, .e.g

    - name: Install PHP
      debug:
        msg: "name: {{ item.0.php_version }}-{{ item.1 }}"
      with_subelements:
        - "{{ packages }}"
        - packages

gives

  msg: 'name: 5.6-common'
  msg: 'name: 5.6-cli'
  msg: 'name: 5.6-opcache'
  msg: 'name: 7.0-common'
  msg: 'name: 7.0-cli'
  msg: 'name: 7.0-opcache'
Sign up to request clarification or add additional context in comments.

1 Comment

See edit on original post, markdown wasn't working in this comment. Thanks so much for pointing me in the right direction. However, i wanted to install ALL php versions not just a selected one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.