簡體   English   中英

Ansible 命令模塊說“|” 是非法字符

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

我正在使用 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 - 在遠程節點上執行命令

命令模塊采用命令名稱后跟以空格分隔的參數列表。 給定的命令將在所有選定的節點上執行。 它不會通過 shell 進行處理,因此諸如 $HOME 之類的變量和諸如“<”、“>”、“|”和“&”之類的操作將不起作用(如果需要這些功能,請使用 shell 模塊)。

shell - 在節點中執行命令

shell 模塊采用命令名稱后跟以空格分隔的參數列表。 它幾乎與命令模塊完全一樣,但通過遠程節點上的 shell (/bin/sh) 運行命令。

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

閱讀Ansible 文檔中命令模塊

它不會通過 shell 進行處理,因此 .. 諸如“<”、“>”、“|”和“&”之類的操作將不起作用

按照它的建議,使用shell 模塊

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

對於它的價值,您可以使用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