ansible-php/tasks/main.yml

129 lines
4.2 KiB
YAML

---
- name: SHELL | Check if we are in multiple PHP distribution
ansible.builtin.shell: set -o pipefail && apt-cache search php xdebug | grep 'php[[:digit:]].[[:digit:]]'
args:
executable: /bin/bash
failed_when: false
changed_when: false
register: multiple_php
when: ansible_os_family == 'Debian'
- name: INCLUDE_VARS | Related to OS family
ansible.builtin.include_vars: "OS_Family_{{ ansible_os_family }}.yml"
- name: INCLUDE_VARS | Related to OS version
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}-{{ ansible_distribution_release }}.yml"
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml"
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- name: SET_FACT | Transform data
ansible.builtin.set_fact:
__php_fpm_full_pool: |
[
{% for p in php_fpm_poold %}
{
name: "{{ p.name | default(p.pool_name) }}",
listen: "{{ p.listen | default(php_version | php_socket(p.name | default(p.pool_name))) }}",
user: "{{ p.user | default(php_default_user_group) }}",
group: "{% if p.user is defined %}{{ p.group | default(p.user) }}{% else %}{{ p.group | default(php_default_user_group) }}{% endif %}",
php_value: {% if p.php_value is defined %}{{ p.php_value | to_nice_json }}{% else %}{}{% endif %},
php_admin_value: {% if p.php_admin_value is defined %}{{ p.php_admin_value | to_nice_json }}{% else %}{}{% endif %},
{% for k, v in p.items() | list %}
{% if k not in ['name', 'pool_name', 'listen', 'user', 'group', 'php_value', 'php_admin_value'] %}
{{ k }}: "{{ v }}"{% if not loop.last %},{% endif %}
{% endif %}
{% endfor %}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
- name: SET_FACT | To YAML
ansible.builtin.set_fact:
php_fpm_full_pool: "{{ __php_fpm_full_pool | from_yaml }}"
- name: FILE | Creates ansible facts.d
ansible.builtin.file:
path: /etc/ansible/facts.d
state: directory
owner: root
group: root
mode: 0755
- name: COPY | Manage facts
ansible.builtin.copy:
content: "{ \"fpm_pool\": {{ php_fpm_full_pool | to_nice_json }} }"
dest: /etc/ansible/facts.d/hanxhx_php.fact
owner: root
group: root
mode: 0644
register: f
tags:
- skip_ansible_lint
- name: SETUP | Gathers new facts
ansible.builtin.setup:
when: f.changed
tags:
- skip_ansible_lint
- name: APT | Install PHP packages
ansible.builtin.apt:
pkg: "{{ pkgs }}"
state: present
update_cache: true
cache_valid_time: 3600
install_recommends: false
vars:
pkgs: "{{ php_packages + php_extra_packages | flatten }}"
notify: restart php-fpm
when: ansible_os_family == 'Debian'
- name: PKGNG | Install PHP packages
community.general.pkgng:
name: "{{ php_packages + php_extra_packages | flatten | join(',') }}"
notify: restart php-fpm
when: ansible_os_family == 'FreeBSD'
- name: IMPORT_TASKS | PHP-FPM
ansible.builtin.import_tasks: fpm.yml
- name: LINEINFILE | PHP CLI configuration
ansible.builtin.lineinfile:
dest: '{{ php_cli_ini }}'
regexp: '^;?{{ item.key }}'
line: '{{ item.key }} = {{ item.value }}'
loop: "{{ php_ini | combine(php_ini_cli) | dict2items }}"
- name: IMPORT_TASKS | Xdebug
ansible.builtin.import_tasks: xdebug.yml
- name: APT | Install and configure opcache
ansible.builtin.import_tasks: opcache.yml
- name: SERVICE | Ensure PHP-FPM is started
ansible.builtin.service:
name: '{{ php_fpm_service }}'
state: started
when: php_install_fpm and ansible_virtualization_type != 'docker'
- name: BLOCK | Ensure PHP-FPM is started if running on Docker
when: php_install_fpm and ansible_virtualization_type == 'docker'
block:
- name: COMMAND | Check if PHP-FPM is started (Docker)
ansible.builtin.command: 'service {{ php_fpm_service }} status'
args:
warn: false
register: dps
changed_when: false
failed_when: false
- name: COMMAND | Ensure PHP-FPM is started (Docker)
ansible.builtin.command: 'service {{ php_fpm_service }} start'
args:
warn: false
when: dps.stdout.find('is not running') != -1