phpcheck: fall back to sudo for docker automatically

This commit is contained in:
Lino committed 2026-10-21 19:17:23 +02:00
1 parent f4042b27c0
commit d3a70eca24
2 files changed
+33 -16

No files matched your search

+1 -7
View File
@@ -20,18 +20,12 @@ run a playbook with sudo:
ansible-playbook playbooks/<playbook_name>.yml -K
```
check PHP versions on hosts and in running Docker containers:
check PHP versions on hosts and in running Docker containers (falls back to sudo for Docker if needed, add `-K` if sudo requires a password):
```bash
ansible-playbook playbooks/phpcheck.yml
```
with sudo (e.g. if the user is not in the docker group):
```bash
ansible-playbook playbooks/phpcheck.yml -K -e phpcheck_become=true
```
## WSL Setup Instructions
To use Ansible on Windows, you need to set up Windows Subsystem for Linux (WSL).
+32 -9
View File
@@ -2,9 +2,8 @@
- name: Check PHP versions
hosts: all
gather_facts: false
become: "{{ phpcheck_become }}"
become: false
vars:
phpcheck_become: false
phpcheck_oldest_branch: "8.2"
phpcheck_min_versions:
"8.2": "8.2.35"
@@ -32,14 +31,30 @@
failed_when: false
when: phpcheck_docker.rc == 0
- name: List running containers with sudo
ansible.builtin.command: docker ps --format '{% raw %}{{.Names}}{% endraw %}'
become: true
register: phpcheck_containers_sudo
changed_when: false
ignore_errors: true
when:
- phpcheck_docker.rc == 0
- phpcheck_containers.rc != 0
- phpcheck_containers.stderr is search('permission denied|docker\.sock|docker daemon', ignorecase=true)
- name: Select container list
ansible.builtin.set_fact:
phpcheck_use_become: "{{ phpcheck_containers_sudo is not skipped }}"
phpcheck_list: "{{ phpcheck_containers_sudo if phpcheck_containers_sudo is not skipped else phpcheck_containers }}"
- name: Get container PHP versions
ansible.builtin.command:
argv: [docker, exec, "{{ item }}", php, -r, "echo PHP_VERSION;"]
loop: "{{ phpcheck_containers.stdout_lines | default([]) }}"
become: "{{ phpcheck_use_become }}"
loop: "{{ phpcheck_list.stdout_lines | default([]) if phpcheck_list.rc | default(1) == 0 else [] }}"
register: phpcheck_container_php
changed_when: false
failed_when: false
when: phpcheck_containers.rc | default(1) == 0
- name: Evaluate PHP versions
ansible.builtin.set_fact:
@@ -61,8 +76,16 @@
- name: Print PHP versions
ansible.builtin.debug:
msg: >-
{{ phpcheck_report
+ (['docker: not installed'] if phpcheck_docker.rc != 0 else [])
+ (['docker: not accessible (' ~ phpcheck_containers.stderr | default('') | trim ~ ')']
if phpcheck_docker.rc == 0 and phpcheck_containers.rc | default(0) != 0 else []) }}
msg: "{{ phpcheck_report + ([phpcheck_docker_line] if phpcheck_docker_line else []) }}"
vars:
phpcheck_sudo_msg: "{{ phpcheck_list.msg | default('') if phpcheck_list is failed else '' }}"
phpcheck_need_k: "{{ phpcheck_sudo_msg is search('password is required|terminal is required|missing sudo password', ignorecase=true) }}"
phpcheck_wrong_pw: "{{ phpcheck_sudo_msg is search('incorrect|sorry, try again|duplicate become password', ignorecase=true) }}"
phpcheck_docker_line: >-
{{ 'docker: not installed' if phpcheck_docker.rc != 0
else 'docker: no access, run with -K' if phpcheck_need_k
else 'docker: no access, wrong sudo password' if phpcheck_wrong_pw
else 'docker: error (' ~ (phpcheck_list.stderr | default('', true) or phpcheck_sudo_msg) | trim ~ ')'
if phpcheck_list.rc | default(1) != 0
else 'docker: no running containers' if phpcheck_list.stdout_lines | default([]) | length == 0
else '' }}