我们可以在剧本的“vars:”部分使用条件,我知道任务中的set_fact,但我想知道我们可以在下面的部分中使用:
---
- hosts: "{{ host1 }}"
vars:
var1: "{{ passed_var1 }}"
var2: "<conditionally assign value based on {{ var1 }}"
答案 0 :(得分:1)
您可以执行以下操作:
---
- name: Test Play
hosts: local
gather_facts: false
vars:
var1: hola
var2: "{{ 'foo' if var1 else '' }}"
tasks:
- name: debug var1
debug:
var: var1
- name: debug var2
debug:
var: var2
你会得到:
PLAY [Test Play] ***************************************************************
TASK [debug1] ******************************************************************
ok: [localhost] => {
"var1": "hola"
}
TASK [debug1] ******************************************************************
ok: [localhost] => {
"var2": "foo"
}
另一个例子:
---
- name: Test Play
hosts: local
gather_facts: false
vars:
var1: hola
var2: "{{ 'foo' if var1 == 'hola' else '' }}"
tasks:
- name: debug var1
debug:
var: var1
- name: debug var2
debug:
var: var2