Ansible-检查变量类型 [英] Ansible - check variable type
本文介绍了Ansible-检查变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
显然,根据几个小时的搜索,没有人遇到此用例:
很简单-我想根据变量类型执行一些ANsible逻辑。基本上等同于instanceof(dict, var_name)
,但在Ansible中:
- name: test
debug:
msg: "{{ instanceof(var_name, dict) | ternary('is a dictionary', 'is something else') }}"
有什么方法可以做到这一点吗?
推荐答案
A:The tests包括mapping按预期工作。例如q:&q;根据变量类型执行一些可分析的逻辑。
- set_fact:
myvar:
key1: value1
- debug:
msg: "{{ (myvar is mapping)|
ternary('is a dictionary', 'is something else') }}"
给予
"msg": "is a dictionary"
q:&q;可选-检查变量类型
A:一个选项是discover the data type和动态include_tasks
。例如,下面的任务
shell> cat tasks-int
- debug:
msg: Processing integer {{ item }}
shell> cat tasks-str
- debug:
msg: Processing string {{ item }}
shell> cat tasks-list
- debug:
msg: Processing list {{ item }}
shell> cat tasks-dict
- debug:
msg: Processing dictionary {{ item }}
使用此攻略
- hosts: localhost
vars:
test:
- 123
- '123'
- [a,b,c]
- {key1: value1}
tasks:
- include_tasks: "{{ 'tasks-' ~ item|type_debug }}"
loop: "{{ test }}"
给予(删节)
msg: Processing integer 123
msg: Processing string 123
msg: Processing list ['a', 'b', 'c']
msg: 'Processing dictionary {''key1'': ''value1''}'
这篇关于Ansible-检查变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文