with_items的可见任务变量回退



我想知道是否有一种方法可以指示ansible使用在with_items参数中发现的第一个变量,例如:

- name: install additional php.d ini files
template: src=etc/opt/remi/php82/{{ item }} dest=/etc/opt/remi/php82/{{ item }}
with_items: "{{ Server.Php82.AdditionalInis | Group.Php82.AdditionalInis | [] }}"
notify:
- restart apache

在上面的例子中:

  1. 使用主机文件中定义的Server.Php82.AdditionalInis,或者;
  2. 使用Group.Php82.AdditionalInis可选地在共享组文件中定义,或者:
  3. 按此顺序跳过该任务。

这样的事情可能吗?我已经看到了{{ var | default('my_var') }},但它只支持一个回退,就我所见。我也不知道当什么都没找到时如何跳过任务。

任何帮助将不胜感激-谢谢

您可以尝试使用可变条件执行两个任务,如下所示:

- name: condition 1
template: //smth//
when: Server.Php82.AdditionalInis is defined
- name: condition 2
template: //smth//
when: (Group.Php82.AdditionalInis is defined and Server.Php82.AdditionalInis is not defined)

但是前面注释中的链默认值看起来更聪明:)

"{{ Server.Php82.AdditionalInis | default(Group.Php82.AdditionalInis) | default([]) }}"

最新更新