简体   繁体   English

Ansible 命令模块说“|” 是非法字符

[英]Ansible Command module says that '|' is illegal character

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:我正在使用 Ansible 来部署我的项目,我试图检查是否安装了指定的包,但我遇到了它的问题,这是任务:

- name: Check if python-apt is installed
  command: dpkg -l | grep python-apt
  register: python_apt_installed
  ignore_errors: True

And here is the problem:这是问题所在:

$ 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 '|'为什么这个字符'|'是非法. .

From the doc:从文档:

command - Executes a command on a remote node command - 在远程节点上执行命令

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 进行处理,因此诸如 $HOME 之类的变量和诸如“<”、“>”、“|”和“&”之类的操作将不起作用(如果需要这些功能,请使用 shell 模块)。

shell - Executes a commands in nodes shell - 在节点中执行命令

The shell module takes the command name followed by a list of space-delimited arguments. shell 模块采用命令名称后跟以空格分隔的参数列表。 It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.它几乎与命令模块完全一样,但通过远程节点上的 shell (/bin/sh) 运行命令。

Therefore you have to use shell: dpkg -l | grep python-apt因此你必须使用shell: dpkg -l | grep python-apt shell: dpkg -l | grep python-apt . shell: dpkg -l | grep python-apt

read about the command module in the Ansible documentation :阅读Ansible 文档中命令模块

It will not be processed through the shell, so .. operations like "<", ">", "|", and "&" will not work它不会通过 shell 进行处理,因此 .. 诸如“<”、“>”、“|”和“&”之类的操作将不起作用

As it recommends, use the shell module :按照它的建议,使用shell 模块

- name: Check if python-apt is installed
  shell: dpkg -l | grep python-apt
  register: python_apt_installed
  ignore_errors: True

For what it's worth, you can check/confirm the installation in a debian environment using the apt command :对于它的价值,您可以使用apt命令在 debian 环境中检查/确认安装:

- name: ensure python-apt is installed
  apt: name=python-apt state=present

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM