gpt4 book ai didi

shell - Ansible 命令模块显示 '|' 是非法字符

转载 作者:行者123 更新时间:2023-12-02 05:31:53 25 4
gpt4 key购买 nike

我正在使用 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

为什么这个字符'|'是非法的。

最佳答案

来自文档:

command - Executes a command on a remote node

The command module takes the command name followed by a list ofspace-delimited arguments. The given command will be executed on allselected nodes. It will not be processed through the shell, sovariables like $HOME and operations like "<", ">", "|", and "&" willnot 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 commandthrough a shell (/bin/sh) on the remote node.

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

关于shell - Ansible 命令模块显示 '|' 是非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24679591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com