Ansible - Convert an integer, boolean, or AnsibleVaultEncryptedUnicode object into a string using the string filter

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"
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter e4378 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |