Ansible 命令模块说“|"是非法字符
我正在使用 Ansible 来部署我的项目,我尝试检查是否安装了指定的包,但我遇到了问题,任务如下:
I am using Ansible to deploy my project and I trying to check if an specified package is installed, but I have a problem with it task, here is the task:
- name: Check if python-apt is installed
command: dpkg -l | grep python-apt
register: python_apt_installed
ignore_errors: True
问题来了:
$ ansible-playbook -i hosts idempotent.yml
PLAY [lxc-host] ***************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.3.240]
TASK: [idempotent | Check if python-apt is installed] *************************
failed: [10.0.3.240] => {"changed": true, "cmd": ["dpkg", "-l", "|", "grep", "python-apt"], "delta": "0:00:00.015524", "end": "2014-07-10 14:41:35.207971", "rc": 2, "start": "2014-07-10 14:41:35.192447"}
stderr: dpkg-query: error: package name in specifier '|' is illegal: must start with an alphanumeric character
...ignoring
PLAY RECAP ********************************************************************
10.0.3.240 : ok=2 changed=1 unreachable=0 failed=0
为什么这个字符是非法的'|'.
Why is illegal this character '|' .
来自文档:
command - Executes a command on a remote node
命令模块采用命令名称后跟一个列表以空格分隔的参数.给定的命令将在所有选定的节点.不会通过shell处理,所以像 $HOME 这样的变量和像<"、>"、|"和&"这样的操作将要不起作用(如果您需要这些功能,请使用 shell 模块).
The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work (use the shell module if you need these features).
shell - Executes a commands in nodes
shell 模块采用命令名称后跟以空格分隔的参数列表.它几乎与命令模块完全一样,但运行命令通过远程节点上的 shell (/bin/sh).
The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.
因此你必须使用 shell: dpkg -l |grep python-apt
.