基于变量,我需要两个版本的“ same”列表,并且我想避免重复以下代码:
- set_fact:
type1: false (or true!)
- set_fact:
mylist:
- "show command1"
- "show command2.1"
- "show command3"
- "show command4"
when: type1
- set_fact:
mylist:
- "show command1"
- "show command2.1"
- "show command2.2"
- "show command3"
- "show command4"
when: not type1
有没有办法(金贾)避免重复?
Riccardo Russo
答案 0 :(得分:1)
我当前的解决方案是:
mycmd:
- "show command1"
- "show command2.1"
{% if not type1 %}
- "show command2.2"
{% endif %}
- "show command3"
- "show command4"
- set_fact:
myvar: "{{ lookup('template', './mytemplate.j2') | from_yaml }}"
- debug:
var: myvar["cmd"]
答案 1 :(得分:0)
您可以在merge lists中输入ansible:
- set_fact:
mylist:
- "show command1"
- "show command2.1"
- "show command3"
- "show command4"
- set_fact:
mylist: '{{ mylist | union([ "show command2.2" ]) }}'
when: not type1
如果订单很重要,您可以对列表进行排序。
mylist: '{{ mylist | union([ "show command2.2" ]) | sort }}'