Ansible-Shell命令中的未定义变量

时间:2019-02-08 15:27:02

标签: ansible yaml

我有以下剧本,应该仅提取软件包名称和版本并打印以进行调试:

---
- hosts: localhost
  tasks:
  - name: Get current versions of packages
    shell: "yum list installed {{ packages }} | grep vim-enhanced | awk '{print $1 $2}'"
    var:
      packages:
      - vim-enhanced
      - nss-pem
    register: packages_installed
    changed_when: False

  - name: Print packages installed
    debug:
      var: packages_installed

运行时,出现以下错误:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'packages' is undefined\n\nThe error appears to have been in '/etc/ansible/patching/patching.yaml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: Get current versions of packages\n    ^ here\n"}

我对此还很陌生,因此很可能我缺少一些基本的知识,但是目前我还没有找到任何文章可以解决。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为有两个问题。

首先,它必须是vars,而不是var

- name: Get current versions of packages
  shell: ...
  vars:     # <---
    packages:
    - vim-enhanced
    - nss-pem

第二,packages是一个列表,将其作为[u'vim-enhanced', u'nss-pem']插入到shell命令中。那可能不是您想要的。 同样,您对vim-enhanced的grep毫无意义。