0

Expected output is the version of nginx, but it outputs a blank string instead:

- hosts: localhost
 tasks:
     - name: check if nginx exists
       stat:
           path: /usr/sbin/nginx
       register: result
     - name: check nginx version
       command: nginx -v
       register: version
     - debug: msg="{{version.stdout}}"

I am getting the below output instead:

TASK [debug] *********************************************************************************************************************
ok: [localhost] => {
    "msg": ""
}
  • 2
    Please take note that [some software packages like Java, Python, Nginx are reporting his version to `stderr`](https://stackoverflow.com/questions/13483443/). You may also take advantage from [Whats the best way in Ansible to check if a command is available?](https://stackoverflow.com/a/70812235/6771046). – U880D Dec 24 '22 at 07:35

1 Answers1

3

It's because nginx -v sends its output to stderr, as one can see with

$ nginx -v >/dev/null
nginx version: nginx/1.23.3
$ nginx -v 2>/dev/null
$

so your output is actually in version.stderr, as you would have seen for yourself had you increased the ansible verbosity or used

- debug: var=version
mdaniel
  • 31,240
  • 5
  • 55
  • 58