条件:检查元素的类型 [英] Conditionals: check type of element
本文介绍了条件:检查元素的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种语法,以检查变量是否包含任何子元素或仅仅是字符串.可以说我有一个文件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
可能是AnsibleUnicode
或unicode
,具体取决于您如何定义值.
For a string your_wanted_type
might be AnsibleUnicode
or unicode
, depending on how you define the value.
这篇关于条件:检查元素的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文