当ansible中的变量未定义时,如何运行任务? [英] How to run a task when variable is undefined in ansible?

查看:118
本文介绍了当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

推荐答案

来自可访问的文档: 如果尚未设置必需变量,则可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆