检查列表是否包含 Ansible 中的项目 [英] Check if a list contains an item in Ansible

查看:29
本文介绍了检查列表是否包含 Ansible 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查提供的版本是否是有效的受支持版本.我已经在一个变量中设置了可接受版本的列表,如果提供的版本不在列表中,我想退出任务.但是,我不确定如何做到这一点.

I'm trying to check if the version supplied is a valid supported version. I've set the list of acceptable versions in a variable, and I want to fail out of the task if the supplied version is not in the list. However, I'm unsure of how to do that.

#/role/vars/main.yml
---
  acceptable_versions: [2, 3, 4]

#/role/tasks/main.yml
---
  - fail: 
      msg: "unsupported version"
      with_items: "{{acceptable_versions}}"
      when: "{{item}} != {{version}}"

  - name: continue with rest of tasks...

以上是我想做的事情,但我一直无法弄清楚是否有一种单行方式来构造失败模块的列表包含"调用.

Above is sort of what I want to do, but I haven't been able to figure out if there's a one line way to construct a "list contains" call for the fail module.

推荐答案

你不需要{{}} in when 条件.您要搜索的是:

You do not need {{}} in when conditions. What you are searching for is:

- fail: msg="unsupported version"
  when: version not in acceptable_versions

这篇关于检查列表是否包含 Ansible 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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