Ansible Command模块说"|"是非法字符 [英] Ansible Command module says that '|' is illegal character

查看:167
本文介绍了Ansible Command模块说"|"是非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ansible部署我的项目,并且尝试检查是否安装了指定的软件包,但是我的任务有问题,这是任务:

- 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 

为什么该字符'|'是非法的.

解决方案

来自文档:

命令-在远程节点上执行命令

命令模块采用命令名称,后跟列表 空格分隔的参数.给定的命令将在所有 选定的节点.它不会通过外壳处理,因此 像$ HOME这样的变量,以及像<",>," |和"&这样的操作将要 不起作用(如果需要这些功能,请使用Shell模块).

shell -在节点中执行命令

shell模块采用命令名称,后跟由空格分隔的参数列表. 它几乎完全像命令模块一样,但是运行命令 通过远程节点上的外壳程序(/bin/sh).

因此,您必须使用shell: dpkg -l | grep python-apt.

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

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

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

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.

Therefore you have to use shell: dpkg -l | grep python-apt.

这篇关于Ansible Command模块说"|"是非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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