条件:检查元素类型 [英] Conditionals: check type of element

查看:22
本文介绍了条件:检查元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种语法来检查变量是否包含任何子元素或只是一个字符串.假设我有一个文件 vars/myvars.yml

I'm looking for a syntax to check if a variable contains any subelements or is just a string. Lets say I have a file vars/myvars.yml

myvars: {
  key1: {
    greeting: "hello"
  },
  key2: {
    greeting: {
      en: "hi",
      fr: "bonjour"
    }
  }
}

我正在寻找一个只接受字符串而不接受对象的条件.所以在这种情况下,key1.greeting 应该匹配条件,但 key2.greeting 应该.

I'm looking for a condition to only accept a string and no object. So in this case, key1.greeting should match the condition, but key2.greeting should not.

---
- name: test
  vars_files:
    - vars/myvars.yml
  hosts: all
  tasks:
  - debug: 
      msg: "greeting is '{{ item.value.greeting }}'"
    when: item.value.greeting ???is type string???
    with_dict: "{{ myvars }}"

有什么想法吗?

推荐答案

您可以使用 type_debug 过滤器:

You can use the type_debug filter:

when: "item.value.greeting | type_debug == 'your_wanted_type'"

对于字符串,your_wanted_type 可能是 AnsibleUnicodeunicode,具体取决于您定义值的方式.

For a string your_wanted_type might be AnsibleUnicode or unicode, depending on how you define the value.

这篇关于条件:检查元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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