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).
使用完整路径而不仅仅是php
in the shell
模块参数。
可能是什么问题?
当您在 shell 调用中使用管道时,返回代码将是最后一个命令的返回代码(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": []
* See the Difference between Login Shell and Non-Login Shell? https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell question on Unix.SE.