Build string from product list in Ansible

Viewed 52

My need is to install list of php extensions for many defined php versions.

Basicly I have a list with php versions and another with set of extensions :

php_defaults_modules:
  - curl
  - mbstring
  - gd
  - json
  - imap
  - readline
  - xml
  - zip
  - mysql
  - pgsql

php_version:
 - 7.4
 - 7.2

I wrote the following tasks for packet installation :

- name: "Install base modules"
  apt:
    name: "php{{ item[0] }}-{{ item[1] }}"
    state: present
  notify:
    - Reload FPM
  loop: "{{  php_version |product( php_defaults_modules)|list }}"

But it takes too much time, as the check operation is done for each packets. That's why I would prefer generate a single line to transmit to apt. The equivalent of :

apt install php7.2-curl php7.2-mb [...] php7.4-mysql php 7.4-pgsql

I tried something like

- name: Define base modules
  set_fact:
    php_extensions: "{{ php_extensions }} + '[ php{{ item[0] }}-{{ item[1] }} ] '"
  loop: "{{  php_version |product( php_defaults_modules) | list }}"

But obviously it's not the solution.

Is there a way to make installation and checks quicker?

Thanks

1 Answers

Here is my solution, not building a list, but a string and then split it. Maybe not the best solution, but it works :

- name: Define base modules
  set_fact:
    php_extensions: ""

- name: Define base modules
  set_fact:
    php_extensions: "{{ php_extensions }} php{{ item[0] }}-{{ item[1] }}"
  loop: "{{ php_version |product( php_defaults_modules ) | list }}"

- name: Install php packages
  apt:
    name: "{{ php_extensions.split() }}"
  notify:
    - Reload FPM
Related