我想使用dpkg -l | grep -i staff
ansible 命令,但它给了我一个错误:
ansible all -m 'dpkj -l | grep -i stuff'
ERROR! this task 'dpkj' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, include_tasks, include_role, import_tasks, import_role, add_host, group_by, set_fact, raw, meta
然后我为其制作了剧本:
- hosts: all
tasks:
- name: Ansible command
command: dpkg -l | grep -i staff
它再次给我一个错误:
FAILED! => {"changed": true, "cmd": ["dpkg", "-l", "|", "grep", "-i", "staff"], "delta": "0:00:00.010754", "end": "2019-06-26 18:15:10.373642", "msg": "non-zero return code", "rc": 1, "start": "2019-06-26 18:15:10.362888", "stderr": "dpkg-query: no packages found matching |\ndpkg-query: no packages found matching -i\ndpkg-query: no packages found matching stuff"
我如何使用ansible执行这个命令?
答案1
您在一个简单的命令中使用管道;Ansible:“shell”和“命令”模块之间的区别提到了该模块的 Ansible 文档command
:
命令不会通过 shell 进行处理,因此 $HOME 等变量以及
<
、>
、|
和;
等操作&
将不起作用。如果您需要这些功能,请使用 shell 模块。
请注意|
该操作列表中“不起作用”的符号。