Ansible:shell 脚本输出始终为空 [英] Ansible: shell script output always empty

查看:57
本文介绍了Ansible:shell 脚本输出始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Linux shell 的输出插入到一个变量中,但由于某种原因,该变量始终为空.

这是 Ansible 代码:

 - 名称:检查 PHP 版本外壳: php -v 2>/开发/空|awk '{打印 $2;出口}'注册: php_version- 调试:var=php_version

这是输出:

ok: [10.0.0.5] =>{php_version":{改变":真的,"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,"开始": "2017-01-08 18:41:00.308593","stderr": "",标准输出":","stdout_lines": [],警告":[]}}

当我直接在服务器上运行命令时,我得到一个有效的结果:

php -v 2>/开发/空|awk '{打印 $2;出口}'7.0.14

可能是什么问题?

解决方案

至于可能的原因,为什么你可以从 CLI 运行命令,而不是 Ansible,很可能是 php 可执行文件的路径当您通过非交互式 SSH 会话* 运行 shell 时,PATH 变量中未定义(如 Ansible).

shell 模块参数中使用完整路径,而不仅仅是 php.

<小时><块引用>

可能是什么问题?

当您在 shell 调用中使用管道时,返回码将是最后一个命令 (awk) 的返回码,尽管第一个命令失败,但您不会收到通知.

Awk 没有得到任何处理输入,它优雅地退出,并且因为您将 stderr 重定向到 /dev/null,您看不到 php 的任何错误> 命令.

例如,如果您明确运行一个不存在的命令:

- 名称:检查 PHP 版本外壳:qwerty -v 2>/开发/空|awk '{打印 $2;出口}'注册: php_version- 调试:var=php_version

您还将获得:

"rc": 0,"开始": "2017-01-09 06:35:10.588258","stderr": "",标准输出":","stdout_lines": [],警告":[]

<小时>

* 查看区别登录 Shell 和非登录 Shell 之间的区别? Unix.SE 上的问题.

I'm trying to insert an output of Linux shell to a variable, but for some reason, the variable 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 output:

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 a valid result:

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

What could be the issue?

解决方案

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

Use a full path instead of just php in the shell module argument.


What could be the issue?

As you are using a pipeline in the shell call, the return code will be that of the last command (awk) and although the first one fails, you won't be notified.

Awk does not get any input to process and it quits gracefully, and because you are redirecting stderr to /dev/null you don't see any error from the php command.

For example if you explicitly run a non-existent command:

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

- debug: var=php_version

you will also get:

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


* See the Difference between Login Shell and Non-Login Shell? question on Unix.SE.

这篇关于Ansible:shell 脚本输出始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆