Shell Ansible命令模块说'|';这是非法字符
我正在使用Ansible部署我的项目,我试图检查是否安装了指定的软件包,但我的it任务有问题,任务如下:Shell Ansible命令模块说'|';这是非法字符,shell,ansible,dpkg,ansible-playbook,Shell,Ansible,Dpkg,Ansible Playbook,我正在使用Ansible部署我的项目,我试图检查是否安装了指定的软件包,但我的it任务有问题,任务如下: - 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
- 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
阅读以下内容:
它不会通过外壳进行处理,因此。。“”和“&”等操作将不起作用
根据建议,请使用:
您可以使用以下方法在debian环境中检查/确认安装:
顺便说一句,根据
python,当您使用apt
模块时,应该自动安装apt
,因此您不需要手动引导它。如果您想使用pipe
我的错误是使用命令而不是shell,请使用shell
模块。我还必须在第一次忽略错误,然后在安装python apt包时,检查任务不会失败。谢谢。如果我想使用apt模块,必须先安装。我只在第一次进行确认,之后我使用apt模块安装软件包。谢谢您的回答。我认为这是一个更好的方法,使用直接与包交互的模块。@rob3等等,什么确认?您当然可以使用apt
安装模块并验证它们是否已安装。debian和ubuntu都已经安装了apt,python apt
没有任何手动安装步骤。是的,但是从文档中第一个注释是:需要python-apt。然后第一个任务是安装python-apt,然后我可以用它来安装其他包。@Rob3这看起来很奇怪,但是你可以使用apt来安装python-apt。
- name: Check if python-apt is installed
shell: dpkg -l | grep python-apt
register: python_apt_installed
ignore_errors: True
- name: ensure python-apt is installed
apt: name=python-apt state=present