---
- name: Prep php... add php Remi repo
  dnf:
    name: https://rpms.remirepo.net/enterprise/remi-release-{{ ansible_distribution_major_version|int }}.rpm
    state: latest
    disable_gpg_check: yes
    validate_certs: no

- name: Prep php... disable all the php repositories
  shell: yum-config-manager --disable 'remi-php*'
  no_log: true

- name: Prep php... enable the repo php{{ php_version | replace(".","") }}
  shell: yum-config-manager --enable remi-php{{ php_version | replace(".","") }}
  no_log: true

- name: Prep php... update os
  dnf:
    name: '*'
    update_cache: true
    state: latest

- name: Prep php... install needed packages
  dnf:
    name:
      - php{{ php_version | replace(".","") }}-php 
      - php{{ php_version | replace(".","") }}-php-pecl-apcu 
      - php{{ php_version | replace(".","") }}-php-bcmath 
      - php{{ php_version | replace(".","") }}-php-dom 
      - php{{ php_version | replace(".","") }}-php-gmp 
      - php{{ php_version | replace(".","") }}-php-pecl-imagick 
      - php{{ php_version | replace(".","") }}-php-ldap 
      - php{{ php_version | replace(".","") }}-php-openssl 
      - php{{ php_version | replace(".","") }}-php-gd 
      - php{{ php_version | replace(".","") }}-php-json 
      - php{{ php_version | replace(".","") }}-php-mysql 
      - php{{ php_version | replace(".","") }}-php-curl 
      - php{{ php_version | replace(".","") }}-php-mbstring 
      - php{{ php_version | replace(".","") }}-php-intl 
      - php{{ php_version | replace(".","") }}-php-exif 
      - php{{ php_version | replace(".","") }}-php-zip 
      - php{{ php_version | replace(".","") }}-php-zlib 
      - php{{ php_version | replace(".","") }}-php-fileinfo 
      - php{{ php_version | replace(".","") }}-php-pcntl 
      - php{{ php_version | replace(".","") }}-php-posix 
      - php{{ php_version | replace(".","") }}-php-xmlreader 
      - php{{ php_version | replace(".","") }}-php-xmlwriter 
      - php{{ php_version | replace(".","") }}-php-ctype 
      - php{{ php_version | replace(".","") }}-php-bz2 
      - php{{ php_version | replace(".","") }}-php-ftp
      - php{{ php_version | replace(".","") }}-php-smbclient 
      - php{{ php_version | replace(".","") }}-php-memcached 
      - php{{ php_version | replace(".","") }}-php-redis 
      - php{{ php_version | replace(".","") }}-php-phar 
      - php{{ php_version | replace(".","") }}-php-opcache
    state: latest

- name: Prep php... Set php in web server conf 
  template:
    dest: /etc/httpd/conf.d/php{{ php_version | replace(".","") }}-php.conf
    src: CentOS/httpd_php.conf.j2
    mode: 0640
  when: websrv in ["apache", "apache2"]

- name: Prep php... Set php env for {{ ansible_facts['distribution'] }}
  set_fact: 
    php_bin: "php{{ php_version | replace('.','') }}"
    php_dir: "/etc/opt/remi/php{{ php_version | replace('.','') }}/php.d"

- name: Prep php... Read configuration for PHP
  set_fact:
    php_content: "{{ lookup('template', '{{ role_path }}/templates/CentOS/php.ini.j2') }}"

- name: Prep php... Integration configuration for PHP
  blockinfile:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php.ini
    content: '{{ php_content }}'
    state: present
    marker: "; {mark} ANSIBLE MANAGED BLOCK"

- name: Prep php... Read OPCACHE configuration for PHP 
  set_fact:
    php_content: "{{ lookup('template', '{{ role_path }}/templates/CentOS/10-opcache.ini.j2') }}"

- name: Prep php... Integration OPCACHE configuration for PHP 
  blockinfile:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php.d/10-opcache.ini
    content: '{{ php_content }}'
    state: present
    marker: "; {mark} ANSIBLE MANAGED BLOCK"

- name: Prep php... Install PHP-FPM
  dnf:
    name:
      - php{{ php_version | replace(".","") }}-php-fpm
    state: latest
  when: add_php_fpm

- name: Prep php... Configure PHP-FPM
  lineinfile:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php-fpm.d/www.conf
    regexp: "^{{ item.property | regex_escape() }}.*"
    line: "{{ item.value }}"
  with_items:
    - { property: 'listen = 127.0.0.1:9000', value: 'listen = /var/opt/remi/php81/run/php-fpm/www.sock' }
    - { property: 'pm = dynamic',            value: 'pm = {{ pm }}' }
    - { property: 'pm.max_children =',       value: 'pm.max_children = {{ pm_max_children }}' }
    - { property: 'pm.start_servers =',      value: 'pm.start_servers = {{ pm_start_servers }}' }
    - { property: 'pm.min_spare_servers =',  value: 'pm.min_spare_servers = {{ pm_min_spare_servers }}' }
    - { property: 'pm.max_spare_servers =',  value: 'pm.max_spare_servers = {{ pm_max_spare_servers }}' }
    - { property: ';pm.max_requests =',      value: 'pm.max_requests = {{ pm_max_requests }}' }
    - { property: 'php_value[session.save_handler]', value: ';php_value[session.save_handler] = files' }
    - { property: 'php_value[session.save_path]',    value: ';php_value[session.save_path]    = /var/opt/remi/php81/lib/php/session' }
    - { property: 'php_value[soap.wsdl_cache_dir]',  value: ';php_value[soap.wsdl_cache_dir]  = /var/opt/remi/php81/lib/php/wsdlcache' }
  when: add_php_fpm

- name: Prep php... Read PHP-FPM configuration
  set_fact:
    phpfpm_content: "{{ lookup('template', '{{ role_path }}/templates/CentOS/www.conf.j2') }}"

- name: Prep php... Integration PHP-FPM configuration
  blockinfile:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php-fpm.d/www.conf
    content: '{{ phpfpm_content }}'
    state: present
    marker: "; {mark} ANSIBLE MANAGED BLOCK"

- name: Prep php... Read APCU configuration for PHP 
  set_fact:
    php_content: "{{ lookup('template', '{{ role_path }}/templates/CentOS/apcu_nc_ini.j2') }}"
  
- name: Prep php... Integration APCU configuration for PHP 
  blockinfile:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php.d/40-apcu.ini
    content: '{{ php_content }}'
    state: present
    marker: "; {mark} ANSIBLE MANAGED BLOCK"

- name: Prep php... Integration Redis configuration for PHP
  template:
    dest: /etc/opt/remi/php{{ php_version | replace(".","") }}/php.d/50-redis.ini
    src: CentOS/50-redis.ini.j2
    mode: 0640
  when: (('%s.service' % (redis_daemon)) in ansible_facts.services.keys()) and (my_redis_service.status == "enabled")

- name: Prep php... Set alias php
  file:
    src: /opt/remi/php{{ php_version | replace(".","") }}/root/usr/bin/php
    dest: /usr/bin/php
    state: link

- name: PHP Installed Starting
  service:
    name: php{{ php_version | replace(".","") }}-php-fpm
    state: started
    enabled: yes

- name: Prep php... Set php-fpm instead of mod_php in web server conf 
  template:
    dest: /etc/httpd/conf.d/php{{ php_version | replace(".","") }}-php.conf
    src: CentOS/httpd_php_fpm.conf.j2
    mode: 0640
  when: (websrv in ["apache", "apache2"]) and (enable_php_fpm)

