phpcheck: fall back to sudo for docker automatically
This commit is contained in:
1 parent
f4042b27c0
commit
d3a70eca24
2 files changed
+33
-16
No files matched your search
@@ -20,18 +20,12 @@ run a playbook with sudo:
|
|||||||
ansible-playbook playbooks/<playbook_name>.yml -K
|
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
|
```bash
|
||||||
ansible-playbook playbooks/phpcheck.yml
|
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
|
## WSL Setup Instructions
|
||||||
|
|
||||||
To use Ansible on Windows, you need to set up Windows Subsystem for Linux (WSL).
|
To use Ansible on Windows, you need to set up Windows Subsystem for Linux (WSL).
|
||||||
|
|||||||
+32
-9
@@ -2,9 +2,8 @@
|
|||||||
- name: Check PHP versions
|
- name: Check PHP versions
|
||||||
hosts: all
|
hosts: all
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
become: "{{ phpcheck_become }}"
|
become: false
|
||||||
vars:
|
vars:
|
||||||
phpcheck_become: false
|
|
||||||
phpcheck_oldest_branch: "8.2"
|
phpcheck_oldest_branch: "8.2"
|
||||||
phpcheck_min_versions:
|
phpcheck_min_versions:
|
||||||
"8.2": "8.2.35"
|
"8.2": "8.2.35"
|
||||||
@@ -32,14 +31,30 @@
|
|||||||
failed_when: false
|
failed_when: false
|
||||||
when: phpcheck_docker.rc == 0
|
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
|
- name: Get container PHP versions
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv: [docker, exec, "{{ item }}", php, -r, "echo PHP_VERSION;"]
|
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
|
register: phpcheck_container_php
|
||||||
changed_when: false
|
changed_when: false
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when: phpcheck_containers.rc | default(1) == 0
|
|
||||||
|
|
||||||
- name: Evaluate PHP versions
|
- name: Evaluate PHP versions
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
@@ -61,8 +76,16 @@
|
|||||||
|
|
||||||
- name: Print PHP versions
|
- name: Print PHP versions
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: >-
|
msg: "{{ phpcheck_report + ([phpcheck_docker_line] if phpcheck_docker_line else []) }}"
|
||||||
{{ phpcheck_report
|
vars:
|
||||||
+ (['docker: not installed'] if phpcheck_docker.rc != 0 else [])
|
phpcheck_sudo_msg: "{{ phpcheck_list.msg | default('') if phpcheck_list is failed else '' }}"
|
||||||
+ (['docker: not accessible (' ~ phpcheck_containers.stderr | default('') | trim ~ ')']
|
phpcheck_need_k: "{{ phpcheck_sudo_msg is search('password is required|terminal is required|missing sudo password', ignorecase=true) }}"
|
||||||
if phpcheck_docker.rc == 0 and phpcheck_containers.rc | default(0) != 0 else []) }}
|
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 '' }}
|
||||||
Reference in new issue
Block a user