如何在ansible中未定义变量时运行任务? [英] How to run a task when variable is undefined in ansible?
本文介绍了如何在ansible中未定义变量时运行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种在 ansible 变量不是注册/未定义时执行任务的方法,例如
I am looking for a way to perform a task when ansible variable is not registers /undefined e.g
-- 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
推荐答案
来自 ansible 文档:如果未设置必需的变量,您可以使用 Jinja2 的定义测试跳过或失败.例如:
From the ansible docs: If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:
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
应该可以工作
So in your case, when: deployed_revision is not defined
should work
这篇关于如何在ansible中未定义变量时运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文