Ansible: shell script output is always empty

I am trying to insert Linux shell output into a variable, but for some reason the variable is always empty.

Here is the Ansible code:

  - name: Check PHP version
    shell: php -v 2> /dev/null | awk '{print $2; exit}'
    register: php_version

  - debug: var=php_version

And here is the conclusion:

ok: [10.0.0.5] => {
    "php_version": {
        "changed": true, 
        "cmd": "php -v 2> /dev/null | awk '{print $2; exit}'", 
        "delta": "0:00:00.015180", 
        "end": "2017-01-08 18:41:00.323773", 
        "rc": 0, 
        "start": "2017-01-08 18:41:00.308593", 
        "stderr": "", 
        "stdout": "", 
        "stdout_lines": [], 
        "warnings": []
    }
}

When I run the command directly on the server, I get the correct result:

php -v 2> /dev/null | awk '{print $2; exit}'
7.0.14

What could be the problem?

+4
source share
1 answer

As for the possible reasons why you can run the command from the CLI but not Ansible, most likely the path to the executable file is phpnot defined in the variable PATHwhen starting the shell through non-interactive SSH session * (as Ansible does).

Use the full path, not just the phpmodule argument shell.


What could be the problem?

, (awk), , .

Awk , , stderr /dev/null, php.

, :

- name: Check PHP version
    shell: qwerty -v 2> /dev/null | awk '{print $2; exit}'
    register: php_version

- debug: var=php_version

:

"rc": 0,
"start": "2017-01-09 06:35:10.588258",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": []

* . " " " " Unix.SE.

+3

Source: https://habr.com/ru/post/1666156/


All Articles