Try to find a smart way to get the user and group of the apache-service on any Linux machine.
Trying with the following ansible task run into an error:
- name: "create documentroot under /var/www/html/vhosts/{{ apache_ssl_server_hostname }}"
command: ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1
register: httpd_user
- file:
path: "/var/www/html/vhosts/{{ apache_ssl_server_hostname }}"
state: directory
mode: 0755
owner: "{{ httpd_user }}"
group: "{{ httpd_user }}"
The Error:
fatal: [nextcloud01]: FAILED! => {"changed": true, "cmd": ["ps", "aux", "|", "egrep", "([a|A]pache|[h|H]ttpd)", "|", "awk", "{ print $1}", "|", "uniq", "|", "tail", "-1"], "delta": "0:00:00.003675", "end": "2023-08-03 11:39:14.901966", "msg": "non-zero return code", "rc": 1, "start": "2023-08-03 11:39:14.898291", "stderr": "error: garbage option\n\nUsage:\n ps [options]\n\n Try 'ps --help <simple|list|output|threads|misc|all>'\n or 'ps --help <s|l|o|t|m|a>'\n for additional help text.\n\nFor more details see ps(1).", "stderr_lines": ["error: garbage option", "", "Usage:", " ps [options]", "", " Try 'ps --help <simple|list|output|threads|misc|all>'", " or 'ps --help <s|l|o|t|m|a>'", " for additional help text.", "", "For more details see ps(1)."], "stdout": "", "stdout_lines": []}
The same command directly executed on the destination machines (01 - debian / 02 - almalinux) works fine:
root@nextcloud01/home/sysadmin# ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1
www-data
root@nextcloud02/home/sysadmin# ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1
apache
I've already put the command string ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1
into a Variable to see if anything needs to be escaped, but the the `debug: msg="{{ string}}" output seems to be fine.
TASK [ansible-role-apache2 : debug] ******************************************************************************************
task path: /home/sysadmin/git/ansible_linux/roles/ansible-role-apache2/tasks/main.yml:34
ok: [nextcloud01] => {
"msg": "ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1"
}
Any suggestions? Thx and regards