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
tags: packages
- name: Install nginx first (required for web root)
- name: Install essential packages
apt:
name: nginx
name:
- gnupg
- software-properties-common
- nginx
state: present
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:
name: "{{ packages }}"
name: "{{ packages | reject('search', '^php') | list }}"
state: present
tags: packages
- name: Add PHP PPA for Ubuntu (needed for PHP 8.4)
apt_repository:
repo: ppa:ondrej/php
when: ansible_distribution == "Ubuntu"
- name: Display PHP version installed
debug:
msg: "Using PHP version {{ php_version }}"
tags: packages
- name: Install all required packages
package:
name: "{{ packages }}"
state: present
- name: Set PHP FPM service name dynamically
set_fact:
php_fpm_service: "php{{ php_version }}-fpm"
tags: packages
# Service Management

View File

@@ -108,40 +108,64 @@ tasks:
cache_valid_time: 3600
tags: packages
- name: Install gnupg for PPA (required for apt-add-repository)
package:
name: gnupg
state: present
tags: packages
- name: Install nginx first (required for web root)
- name: Install essential packages
apt:
name: nginx
name:
- gnupg
- software-properties-common
- nginx
state: present
tags: [packages, nginx]
- name: Install all required packages
package:
name: "{{ packages }}"
state: present
tags: [packages, nginx]
- 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: 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:
name: gnupg
name: "{{ packages | reject('search', '^php') | list }}"
state: present
tags: packages
- name: Add PHP PPA for Ubuntu (needed for PHP 8.4)
apt_repository:
repo: ppa:ondrej/php
when: ansible_distribution == "Ubuntu"
- name: Display PHP version installed
debug:
msg: "Using PHP version {{ php_version }}"
tags: packages
- name: Install all required packages
package:
name: "{{ packages }}"
state: present
- name: Set PHP FPM service name dynamically
set_fact:
php_fpm_service: "php{{ php_version }}-fpm"
tags: packages
# Service Management

View File

@@ -1,39 +1,26 @@
# Ubuntu/Debian family variables
---
# Package names for Ubuntu/Debian systems
# Package names for Ubuntu/Debian systems (PHP version is detected and installed dynamically)
packages:
- nginx
- 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
- unzip
- fail2ban
- ufw
- gnupg
- software-properties-common
# Service names
mysql_service: mariadb
nginx_service: nginx
php_fpm_service: php8.4-fpm
php_fpm_service: php{{ php_version }}-fpm
# Package manager
package_manager: apt
# PHP paths
php_fpm_config_path: /etc/php/8.4/fpm
php_cli_config_path: /etc/php/8.4/cli
php_fpm_pool_path: /etc/php/8.4/fpm/pool.d
php_fpm_socket: /run/php/php8.4-fpm.sock
# PHP paths (dynamic based on detected version)
php_fpm_config_path: /etc/php/{{ php_version }}/fpm
php_cli_config_path: /etc/php/{{ php_version }}/cli
php_fpm_pool_path: /etc/php/{{ php_version }}/fpm/pool.d
php_fpm_socket: /run/php/php{{ php_version }}-fpm.sock
# Nginx paths
nginx_config_path: /etc/nginx