Skip to content

Commit 30b57b1

Browse files
authored
Merge pull request #10 from punktDe/apt-third-party
Add a possibility to use Ondřej Surý's apt repo on Debian-based systems
2 parents 02e93fb + 993a928 commit 30b57b1

File tree

8 files changed

+105
-18
lines changed

8 files changed

+105
-18
lines changed

.github/workflows/test.yml

+22-11
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ jobs:
1616
- name: Checkout
1717
uses: actions/checkout@v3
1818

19+
- name: Install uv
20+
uses: astral-sh/setup-uv@v5
21+
1922
- name: Set up Python
20-
uses: actions/setup-python@v2
21-
with:
22-
python-version: '3.x'
23+
run: uv python install
2324

24-
- name: Install dependencies.
25-
run: pip install -r requirements.txt
25+
- name: Install dependencies
26+
run: |
27+
uv -q venv --allow-existing --seed .venv
28+
. .venv/bin/activate
29+
uv -q pip install -r requirements.txt
2630
2731
- name: Run ansible-lint
28-
run: "ansible-lint"
32+
run: |
33+
. .venv/bin/activate
34+
ansible-lint
2935
3036
molecule:
3137
strategy:
@@ -36,15 +42,20 @@ jobs:
3642
- name: Checkout
3743
uses: actions/checkout@v3
3844

45+
- name: Install uv
46+
uses: astral-sh/setup-uv@v5
47+
3948
- name: Set up Python
40-
uses: actions/setup-python@v2
41-
with:
42-
python-version: '3.x'
49+
run: uv python install
4350

4451
- name: Install dependencies.
4552
run: |
46-
pip install -r requirements.txt
53+
uv -q venv --allow-existing --seed .venv
54+
. .venv/bin/activate
55+
uv -q pip install -r requirements.txt
4756
ansible-galaxy install -r requirements.yml
4857
4958
- name: Run molecule
50-
run: molecule test -p ${{ matrix.os }}
59+
run: |
60+
. .venv/bin/activate
61+
molecule test -p ${{ matrix.os }}

defaults/main.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
---
22
php:
3+
repository:
4+
apt:
5+
enabled: no
6+
repository: >-
7+
{%- if ansible_distribution == 'Ubuntu' -%}
8+
https://ppa.launchpadcontent.net/ondrej/php/ubuntu
9+
{%- else -%}
10+
https://packages.sury.org/php/
11+
{%- endif -%}
12+
key_url: >-
13+
{%- if ansible_distribution == 'Ubuntu' -%}
14+
https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xb8dc7e53946656efbce4c1dd71daeaab4ad4cab6
15+
{%- else -%}
16+
https://packages.sury.org/apt.gpg
17+
{%- endif -%}
318
version: "{{ ansible_local.php.version }}"
419
prefix: "{{ ansible_local.php.prefix }}"
520
xdebug:

files/php.fact.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ def get_os_family(self) -> str:
3131
def get_php_version(self) -> str:
3232
os_family = self.get_os_family()
3333
if os_family == "debian":
34-
command='apt-cache search --names-only "^php[0-9].[0-9]$" | grep -o "[0-9]\\.[0-9]"'
34+
command='apt-cache search --names-only "^php[0-9].[0-9]$" | grep -o "[0-9]\\.[0-9]" | sort -V | tail -n 1'
3535
else:
3636
command="php -r 'echo phpversion();' | grep -o '[0-9]\\.[0-9]' | head -n1"
3737
command_output = subprocess.run(command, shell=True, capture_output=True)
38-
if len(command_output.stdout) > 0:
38+
clean_output = command_output.stdout.decode().strip()
39+
if re.match(r"[0-9]+\.[0-9]", clean_output):
3940
return command_output.stdout.decode().strip()
4041
else:
4142
raise OSError(f"Error detecting the PHP version: command_output.stderr.decode()")

molecule/default/converge.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
proserver_user: yes
1111
nginx:
1212
default_server: no
13+
php:
14+
repository:
15+
apt:
16+
enabled: yes
1317
tasks:
1418
- name: "Include ansible-proserver-php"
1519
ansible.builtin.include_role:

tasks/apt.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
- name: Install python3-debian package with apt
3+
ansible.builtin.apt:
4+
name: python3-debian
5+
update_cache: yes
6+
7+
- name: Remove the legacy apt repository
8+
when: php.repository.apt.enabled
9+
ansible.builtin.file:
10+
dest: /etc/apt/sources.list.d/php.list
11+
state: absent
12+
13+
- name: Ensure the third-party PHP repository is {{ 'present' if php.repository.apt.enabled else 'absent' }}
14+
changed_when: "'molecule-idempotence-notest' not in ansible_skip_tags"
15+
register: php_repository_added
16+
ansible.builtin.deb822_repository:
17+
name: php
18+
uris: "{{ php.repository.apt.repository }}"
19+
signed_by: "{{ php.repository.apt.key_url }}"
20+
types: [deb]
21+
components: [main]
22+
suites: "{{ ansible_distribution_release }}"
23+
state: "{{ 'present' if php.repository.apt.enabled else 'absent' }}"
24+
enabled: "{{ php.repository.apt.enabled }}"
25+
26+
- name: Update apt cache (Debian-based)
27+
when: php_repository_added.changed
28+
changed_when: no
29+
ansible.builtin.apt:
30+
update_cache: yes
31+
32+
- name: Reload facts
33+
when: php_repository_added.changed
34+
check_mode: no
35+
changed_when: no
36+
ansible.builtin.setup: {}

tasks/install.yaml

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33
ansible.builtin.user:
44
name: proserver
55

6-
- name: Install PHP and extensions
6+
- name: Install PHP {{ php.version }} and extensions
77
notify: Restart PHP-FPM
88
ansible.builtin.apt:
99
name: >-
1010
{{
11-
['php-fpm'] + php.install_extensions.items() |
11+
['php' + (php.version if php.repository.apt.enabled else '') + '-fpm'] + php.install_extensions.items() |
1212
selectattr('1', 'eq', true)|map(attribute='0') |
13-
map('regex_replace', '^', 'php-') |
13+
map('regex_replace', '^', 'php' + (php.version if php.repository.apt.enabled else '') + '-') |
1414
list + (['composer'] if php.install_composer else [])
1515
}}
16+
17+
- name: List all packages belonging to other PHP versions
18+
changed_when: no
19+
check_mode: no
20+
register: previous_php_packages
21+
failed_when: previous_php_packages.failed and previous_php_packages.stderr != ""
22+
ansible.builtin.shell:
23+
cmd: |
24+
dpkg -l php\* | awk '/^[hi]i/{print $2}' | grep "php[0-9]\\.[0-9]" | grep -v {{ php.version }}
25+
26+
- name: Ensure all non-{{ php.version }} PHP packages are uninstalled
27+
when: previous_php_packages.stdout != ""
28+
ansible.builtin.apt:
29+
package: "{{ previous_php_packages.stdout_lines }}"
30+
state: absent
31+
update_cache: yes
32+
autoremove: yes

tasks/main.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
- name: Set the PHP facts
33
ansible.builtin.include_tasks: php_fact.yaml
44

5+
- name: Manage apt repo (Ubuntu/Debian)
6+
when: ansible_os_family == 'Debian'
7+
ansible.builtin.include_tasks: apt.yaml
8+
59
- name: Install PHP and extensions (Ubuntu/Debian)
610
when: ansible_os_family == 'Debian'
711
ansible.builtin.include_tasks: install.yaml

tasks/php_fact.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@
2828

2929
- name: Reload facts
3030
check_mode: no
31-
when: php_template_fact_result.changed
32-
changed_when: yes
31+
changed_when: no
3332
ansible.builtin.setup: {}

0 commit comments

Comments
 (0)