0

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

lhf
  • 70,581
  • 9
  • 108
  • 149
  • Does [Whats the difference between ansible `raw`, `shell` and `command`?](https://stackoverflow.com/questions/57804071/) and [`command` Synopsis](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html#synopsis) answer your question? In other words, replace `command:` with `shell:`. – U880D Aug 03 '23 at 10:06
  • One can reproduce the issue easily on CLI by using `ps aux \|` resulting in an output of `error: garbage option`. – U880D Aug 03 '23 at 10:25
  • 1
    `schell:` and switching on the output-side to `owner: "{{ httpd_user.stdout }}"` solved my problem. THX – Arny80Hexa Aug 03 '23 at 12:08
  • 1
    `uniq | tail -1` doesn't make sense since whatever value was at the end of the input before calling `uniq` will be the same value if you just don't call `uniq`. So the output of `anything | uniq | tail -1` will be the same as the output of `anything | tail -1`. – Ed Morton Aug 03 '23 at 13:12
  • 1
    You don't need all of those commands and pipes when you're using awk. `ps aux | egrep '([a|A]pache|[h|H]ttpd)' | awk '{ print $1}' | uniq | tail -1` (with or without the `| uniq`) is equivalent to `ps aux | awk '/[a|A]pache|[h|H]ttpd/{x=$1} END{if (x!="") print x}'`. Also, `egrep` has been deprecated for about 20 years in favor of `grep -E`. – Ed Morton Aug 03 '23 at 13:15
  • 1
    Also, the bracket expressions `[a|A]` and `[h|H]` should almost certainly be `[aA]` and `[hH]` as I suspect you just want to make the match on `a` and `h` case-insensitive, not also match on `|pache` and `|ttpd`. `[aA]` and `(a|A)` both mean `a or A` but `[a|A]` means `a or | or A`. So `awk '/[aA]pache|[hH]ttpd/{x=$1} END{if (x!="") print x}'` is probably what you really want. – Ed Morton Aug 03 '23 at 13:21

0 Answers0