尽管“when”子句,但未定义变量的Ansible弃用警告

时间:2016-05-06 08:35:11

标签: ansible ansible-playbook

我在一本安息书中有这个:

- name: install custom packages for the host if there are any
    apt: pkg={{ item }} state=latest
    with_items: "{{ extra_packages }}"
    when: extra_packages is defined

运行它会导致:

  

[DEPRECATION WARNING]:由于未定义错误导致跳过任务,将来这将是致命错误:'extra_packages'未定义。

但我正在检查变量是否在when子句中定义。如何修复此问题,以便在ansible升级时不会导致致命错误?

1 个答案:

答案 0 :(得分:4)

该解决方案最初并不明显,并且确实感觉像是一种不一致的问题。

  - name: install custom packages for the host if there are any
    apt: pkg={{ item }} state=latest
    with_items: "{{ extra_packages | default([]) }}"

基本上,在循环中使用“when”并不适用于此用例。

记录在案here

  

如果需要根据定义的循环变量跳过整个任务,请使用| default过滤器提供空迭代器

但是,对于特定的错误消息,该文档不适用于Google。现在这只是一个问题,以前(不知不觉)所依赖的行为已被弃用。