2015-05-08 89 views
60

我寻找到执行任务的方式,当ansible变量不是寄存器/未定义如如何在变量未定义的情况下运行任务?

-- name: some task 
    command: sed -n '5p' "{{app.dirs.includes}}/BUILD.info" | awk '{print $2}' 
    when: (! deployed_revision) AND (!deployed_revision.stdout) 
    register: deployed_revision 

回答

112

ansible docs: 如果所需的变量尚未设置,您可以跳过或使用Jinja2的的定义会失败测试。例如:

tasks: 

- shell: echo "I've got '{{ foo }}' and am not afraid to use it!" 
    when: foo is defined 

- fail: msg="Bailing out. this play requires 'bar'" 
    when: bar is not defined 

所以你的情况,when: deployed_revision is not defined应该工作

+3

感谢这个工作对我来说'时:deployed_revision没有定义或deployed_revision.stdout没有定义或deployed_revision.stdout ==“' – sakhunzai

+3

”你也可以将它与不同的条件结合起来:'when:item.sudo is defined and item.sudo == true' – czerasz

+2

不要做我所做的事情,并且在'when:foo被定义'时在foo周围放置大括号。这不起作用:'当{{foo}}被定义' – David

相关问题