- roles
- consul
- vars/main.yml
在main.yml
我尝试:
consul_is_server: {{ true if consul_server is defined else false }}
和剧本:
- hosts: consul-server
roles:
- consul
vars:
consul_server: true
收到错误:
consul_is_server: {{ "true" if consul_server is defined else "false" }}
^
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
如何在任务变量中使用条件?
答案 0 :(得分:2)
Vars文件不包含条件,它们应该只包含变量。条件在剧本的任务部分使用。
final Handler handler = new Handler(); // Here is main / UI thread.
new Thread(new Runnable(){
@Override
public void run(){
/* Background tasks go here */
/* Once you're done ...*/
handler.post(new Runable(){
@Override
public void run(){
/* Post-execution code go here */
}
})
}
}).start();
然后条件执行可能如下所示:
vars:
consul_server: true
答案 1 :(得分:1)
现在已经成功了:
consul_is_server: >
{{ true if consul_server is defined and consul_server==true else false }}
但在这种情况下consul_is_server
是字符串:“False”或“True”。
因此,当在模板中使用此var时,我需要使用强制转换为bool
:
"server": {{ "true" if consul_is_server |bool else "false" }}