feat: add PHP version detection with 8.4→8.3 fallback

- Detect available PHP version at runtime
- Try PHP 8.4 first, fallback to 8.3
- Dynamically set PHP service names and paths
- Remove hardcoded PHP 8.4 references from vars
This commit is contained in:
Sebastian Palencsar
2026-05-06 13:38:29 +02:00
parent 38241f82e3
commit 58ffb82378
3 changed files with 104 additions and 55 deletions

View File

@@ -105,28 +105,66 @@ tasks:
cache_valid_time: 3600 cache_valid_time: 3600
tags: packages tags: packages
- name: Install nginx first (required for web root) - name: Install essential packages
apt: apt:
name: nginx name:
- gnupg
- software-properties-common
- nginx
state: present state: present
tags: [packages, nginx] tags: [packages, nginx]
- name: Install all required packages - name: Detect available PHP version and set fallback
shell: |
if apt-cache show php8.4-fpm >/dev/null 2>&1; then
echo "8.4"
elif apt-cache show php8.3-fpm >/dev/null 2>&1; then
echo "8.3"
else
echo "none"
fi
register: php_version_detect
changed_when: false
tags: packages
- name: Set PHP version fact
set_fact:
php_version: "{{ php_version_detect.stdout }}"
tags: packages
- name: Install PHP with fallback (try 8.4, fallback to 8.3)
apt:
name:
- "php{{ php_version }}-fpm"
- "php{{ php_version }}-mysql"
- "php{{ php_version }}-curl"
- "php{{ php_version }}-gd"
- "php{{ php_version }}-intl"
- "php{{ php_version }}-mbstring"
- "php{{ php_version }}-soap"
- "php{{ php_version }}-xml"
- "php{{ php_version }}-xmlrpc"
- "php{{ php_version }}-zip"
- "php{{ php_version }}-opcache"
- "php{{ php_version }}-redis"
state: present
register: php_install_result
tags: packages
- name: Install remaining packages
package: package:
name: "{{ packages }}" name: "{{ packages | reject('search', '^php') | list }}"
state: present state: present
tags: packages tags: packages
- name: Add PHP PPA for Ubuntu (needed for PHP 8.4) - name: Display PHP version installed
apt_repository: debug:
repo: ppa:ondrej/php msg: "Using PHP version {{ php_version }}"
when: ansible_distribution == "Ubuntu"
tags: packages tags: packages
- name: Install all required packages - name: Set PHP FPM service name dynamically
package: set_fact:
name: "{{ packages }}" php_fpm_service: "php{{ php_version }}-fpm"
state: present
tags: packages tags: packages
# Service Management # Service Management

View File

@@ -108,40 +108,64 @@ tasks:
cache_valid_time: 3600 cache_valid_time: 3600
tags: packages tags: packages
- name: Install gnupg for PPA (required for apt-add-repository) - name: Install essential packages
package:
name: gnupg
state: present
tags: packages
- name: Install nginx first (required for web root)
apt: apt:
name: nginx name:
- gnupg
- software-properties-common
- nginx
state: present state: present
tags: [packages, nginx] tags: [packages, nginx]
- name: Install all required packages - name: Detect available PHP version and set fallback
package: shell: |
name: "{{ packages }}" if apt-cache show php8.4-fpm >/dev/null 2>&1; then
state: present echo "8.4"
tags: [packages, nginx] elif apt-cache show php8.3-fpm >/dev/null 2>&1; then
echo "8.3"
else
echo "none"
fi
register: php_version_detect
changed_when: false
tags: packages
- name: Install gnupg for PPA (required for apt-add-repository) - name: Set PHP version fact
set_fact:
php_version: "{{ php_version_detect.stdout }}"
tags: packages
- name: Install PHP with fallback (try 8.4, fallback to 8.3)
apt:
name:
- "php{{ php_version }}-fpm"
- "php{{ php_version }}-mysql"
- "php{{ php_version }}-curl"
- "php{{ php_version }}-gd"
- "php{{ php_version }}-intl"
- "php{{ php_version }}-mbstring"
- "php{{ php_version }}-soap"
- "php{{ php_version }}-xml"
- "php{{ php_version }}-xmlrpc"
- "php{{ php_version }}-zip"
state: present
register: php_install_result
tags: packages
- name: Install remaining packages
package: package:
name: gnupg name: "{{ packages | reject('search', '^php') | list }}"
state: present state: present
tags: packages tags: packages
- name: Add PHP PPA for Ubuntu (needed for PHP 8.4) - name: Display PHP version installed
apt_repository: debug:
repo: ppa:ondrej/php msg: "Using PHP version {{ php_version }}"
when: ansible_distribution == "Ubuntu"
tags: packages tags: packages
- name: Install all required packages - name: Set PHP FPM service name dynamically
package: set_fact:
name: "{{ packages }}" php_fpm_service: "php{{ php_version }}-fpm"
state: present
tags: packages tags: packages
# Service Management # Service Management

View File

@@ -1,39 +1,26 @@
# Ubuntu/Debian family variables # Ubuntu/Debian family variables
--- ---
# Package names for Ubuntu/Debian systems # Package names for Ubuntu/Debian systems (PHP version is detected and installed dynamically)
packages: packages:
- nginx
- mariadb-server - mariadb-server
- php8.4-fpm
- php8.4-mysql
- php8.4-curl
- php8.4-gd
- php8.4-intl
- php8.4-mbstring
- php8.4-soap
- php8.4-xml
- php8.4-xmlrpc
- php8.4-zip
- python3-pymysql - python3-pymysql
- unzip - unzip
- fail2ban - fail2ban
- ufw - ufw
- gnupg
- software-properties-common
# Service names # Service names
mysql_service: mariadb mysql_service: mariadb
nginx_service: nginx nginx_service: nginx
php_fpm_service: php8.4-fpm php_fpm_service: php{{ php_version }}-fpm
# Package manager # Package manager
package_manager: apt package_manager: apt
# PHP paths # PHP paths (dynamic based on detected version)
php_fpm_config_path: /etc/php/8.4/fpm php_fpm_config_path: /etc/php/{{ php_version }}/fpm
php_cli_config_path: /etc/php/8.4/cli php_cli_config_path: /etc/php/{{ php_version }}/cli
php_fpm_pool_path: /etc/php/8.4/fpm/pool.d php_fpm_pool_path: /etc/php/{{ php_version }}/fpm/pool.d
php_fpm_socket: /run/php/php8.4-fpm.sock php_fpm_socket: /run/php/php{{ php_version }}-fpm.sock
# Nginx paths # Nginx paths
nginx_config_path: /etc/nginx nginx_config_path: /etc/nginx