Ansible - Convert an integer, boolean, or AnsibleVaultEncryptedUnicode object into a string using the string filter
by
Jeremy Canfield |
Updated: March 01 2023
| Ansible articles
string is a Jinja2 filter, commonly used to:
- Convert an integer, boolean, or AnsibleVaultEncryptedUnicode object into a string
For example, lets say you use the vars plugin to create a variable named foo that contains an integer.
---
- hosts: locahost
vars:
foo: 10
...
The debug module and type_debug filter can be used to verify that the value in the foo variable is an integer, not a string.
- debug:
msg: "The 'foo' variable contains an {{ foo | type_debug }}"
Something like this should be returned.
TASK [debug]
ok: [localhost] => {
"msg": "The 'foo' variable contains an integer"
}
The set_fact module and string filter can be used to convert the integer into a string.
- name: convert integer to string
set_fact:
foo: "{{ foo | string }}"
The debug module and type_debug filter can again be used to verify that the value in the foo variable is now a string, not an integer.
- debug:
msg: "The 'foo' variable object type is {{ foo | type_debug }}"
Something like this should be returned.
TASK [debug]
ok: [localhost] => {
"msg": "The 'foo' variable object type is unicode"
}
Did you find this article helpful?
If so, consider buying me a coffee over at