Ansible - Determine the type of value stored in a variable using the type_debug filter
by
Jeremy Canfield |
Updated: June 28 2023
| Ansible articles
type_debug is a filter that can be used to determine if the value stored in a variable is a boolean, an integer, unicode, AnsibleUnsafeText, AnsibleUnicode, or AnsibleVaultEncryptedUnicode.
- AnsibleUnsafeText = a string, such as Hello World
- AnsibleUnicode = a string, such as Hello World
- AnsibleVaultEncryptedUnicode = a string encrypted by the ansible-vault command
- boolean = true, false, yes, no, 1, 0
- integer = a number other than 1 or 0, such as 123456
- unicode = a string, such as Hello World
For example, lets say you have the following playbook.
---
- hosts: localhost
vars:
my_string: "hello"
my_list: ['foo', 'bar']
my_boolean: true
my_integer: 123
tasks:
- shell: ps
register: out
- set_fact:
set_fact_register_out: out
- debug:
var: my_string | type_debug
- debug:
var: my_list | type_debug
- debug:
var: my_boolean | type_debug
- debug:
var: my_integer | type_debug
- debug:
var: out | type_debug
- debug:
var: set_fact_register_out | type_debug
- debug:
var: my_extra_variable | type_debug
...
Something like this should be returned.
TASK [debug]
ok: [localhost] => {
"my_string | type_debug": "AnsibleUnicode"
}
TASK [debug]
ok: [localhost] => {
"my_list | type_debug": "list"
}
TASK [debug]
ok: [localhost] => {
"my_boolean | type_debug": "bool"
}
TASK [debug]
ok: [localhost] => {
"my_integer | type_debug": "int"
}
TASK [debug]
ok: [localhost] => {
"out | type_debug": "dict"
}
TASK [debug]
ok: [localhost] => {
"set_fact_register_out | type_debug": "AnsibleUnicode"
}
TASK [debug]
ok: [localhost] => {
"my_extra_variable | type_debug": "unicode"
}
Did you find this article helpful?
If so, consider buying me a coffee over at