community.general.ansible_type 测试 – 验证输入类型

注意

此测试插件是 community.general 集合(版本 10.1.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general

要在 playbook 中使用它,请指定:community.general.ansible_type

community.general 9.2.0 中的新增功能

概要

  • 此测试验证输入类型。

输入

这描述了测试的输入,即 is community.general.ansible_typeis not community.general.ansible_type 之前的值。

参数

注释

输入

any / 必需

输入数据。

关键字参数

这描述了测试的关键字参数。这些是以下示例中的值 key1=value1key2=value2 等:input is community.general.ansible_type(key1=value1, key2=value2, ...)input is not community.general.ansible_type(key1=value1, key2=value2, ...)

参数

注释

别名

字典

数据类型别名。

默认值: {}

dtype

any / 必需

要验证的单个数据类型或数据类型列表。

示例

# Substitution converts str to AnsibleUnicode
# -------------------------------------------

# String. AnsibleUnicode.
dtype: AnsibleUnicode
data: "abc"
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# String. AnsibleUnicode alias str.
alias: {"AnsibleUnicode": "str"}
dtype: str
data: "abc"
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# List. All items are AnsibleUnicode.
dtype: list[AnsibleUnicode]
data: ["a", "b", "c"]
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# Dictionary. All keys are AnsibleUnicode. All values are AnsibleUnicode.
dtype: dict[AnsibleUnicode, AnsibleUnicode]
data: {"a": "foo", "b": "bar", "c": "baz"}
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# No substitution and no alias. Type of strings is str
# ----------------------------------------------------

# String
dtype: str
result: '{{ "abc" is community.general.ansible_type(dtype) }}'
# result => true

# Integer
dtype: int
result: '{{ 123 is community.general.ansible_type(dtype) }}'
# result => true

# Float
dtype: float
result: '{{ 123.45 is community.general.ansible_type(dtype) }}'
# result => true

# Boolean
dtype: bool
result: '{{ true is community.general.ansible_type(dtype) }}'
# result => true

# List. All items are strings.
dtype: list[str]
result: '{{ ["a", "b", "c"] is community.general.ansible_type(dtype) }}'
# result => true

# List of dictionaries.
dtype: list[dict]
result: '{{ [{"a": 1}, {"b": 2}] is community.general.ansible_type(dtype) }}'
# result => true

# Dictionary. All keys are strings. All values are integers.
dtype: dict[str, int]
result: '{{ {"a": 1} is community.general.ansible_type(dtype) }}'
# result => true

# Dictionary. All keys are strings. All values are integers.
dtype: dict[str, int]
result: '{{ {"a": 1, "b": 2} is community.general.ansible_type(dtype) }}'
# result => true

# Type of strings is AnsibleUnicode or str
# ----------------------------------------

# Dictionary. The keys are integers or strings. All values are strings.
alias: {"AnsibleUnicode": "str"}
dtype: dict[int|str, str]
data: {1: 'a', 'b': 'b'}
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# Dictionary. All keys are integers. All values are keys.
alias: {"AnsibleUnicode": "str"}
dtype: dict[int, str]
data: {1: 'a', 2: 'b'}
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# Dictionary. All keys are strings. Multiple types values.
alias: {"AnsibleUnicode": "str"}
dtype: dict[str, bool|dict|float|int|list|str]
data: {'a': 1, 'b': 1.1, 'c': 'abc', 'd': True, 'e': ['x', 'y', 'z'], 'f': {'x': 1, 'y': 2}}
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# List. Multiple types items.
alias: {"AnsibleUnicode": "str"}
dtype: list[bool|dict|float|int|list|str]
data: [1, 2, 1.1, 'abc', True, ['x', 'y', 'z'], {'x': 1, 'y': 2}]
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# Option dtype is list
# --------------------

# AnsibleUnicode or str
dtype: ['AnsibleUnicode', 'str']
data: abc
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# float or int
dtype: ['float', 'int']
data: 123
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# float or int
dtype: ['float', 'int']
data: 123.45
result: '{{ data is community.general.ansible_type(dtype) }}'
# result => true

# Multiple alias
# --------------

# int alias number
alias: {"int": "number", "float": "number"}
dtype: number
data: 123
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

# float alias number
alias: {"int": "number", "float": "number"}
dtype: number
data: 123.45
result: '{{ data is community.general.ansible_type(dtype, alias) }}'
# result => true

返回值

描述

返回值

布尔值

数据类型是否有效。

已返回: 成功

作者

  • Vladimir Botka (@vbotka)

提示

每个条目类型的配置条目都有从低到高的优先级顺序。 例如,列表中较低的变量将覆盖较高的变量。