Ansible - Return an integer using the int filter

int is a Jinja2 filter, commonly used to:

  • Convert a string, boolean, unicode, or AnsibleVaultEncryptedUnicode object into an integer
  • Perform a math function (addition, subtraction, multiplication, division)

Convert a string into an integer

For example, lets say you use the vars plugin to create a variable named foo. Notice in this example the double quotes are used, which you would think we set the variable to be a string. The debug module and type_debug filter can be used to show that foo is actually AnsibleUnicode.

---
- hosts: localhost
  vars:
    foo: "10"
  tasks:
  - debug:
      msg: "The 'foo' variable contains {{ foo | type_debug }}"
...

 

Something like this should be returned.

TASK [debug]
ok: [server1.example.com] => {
    "msg": "The 'foo' variable contains AnsibleUnicode"
}

 

AVOID TROUBLE

Jinja2 templating will always return a string

Let's say you then try to use the set_fact module and the int filter to convert foo into an integer.

---
- hosts: localhost
  vars:
    foo: "10"
  tasks:
  - set_fact:
      foo: "{{ foo | int }}"

  - debug:
      msg: "The 'foo' variable contains {{ foo | type_debug }}"
...

 

Something like this should be returned. Now the type is unicode.

TASK [debug]
ok: [server1.example.com] => {
    "msg": "The 'foo' variable contains unicode"
}

 

The underlying issue is that when templating (the Jinja2 curly braces) always returns a string. However, you can still use the int filter in a when statement, since when statements do not use templating, to convert a variable into an int.

---
- hosts: localhost
  vars:
    foo: "10"
  tasks:
  - debug:
      msg: The 'foo' variable contains a value of 10
    when:
      - foo | int == 10
...

 


Perform a math function

For example, the int filter could use + for addition, like this.

- name: 10 plus 1
  debug: 
    msg: "{{ 10 | int + 1 }}"

 

Which should return 11.

TASK [10 plus 1]
ok: [server1.example.com] => {
    "msg": [
        "11"
    ]
}

 

Or using - for subtraction.

- name: 10 minus 1
  debug: 
    msg: "{{ 10 | int - 1 }}"

 

Or using * for multiplication.

- name: multiply 2 by 3
  debug: 
    msg: "{{ 2 | int * 3 }}"

 

Or using / for division.

- name: 10 divided by 2
  debug: 
    msg: "{{ 10 | int / 2 }}"

 

Let's say you are using the vars plugin to create a variable. Notice in this example that the value is not wrapped in quotes. If the value were wrapped in quotes, the value would be interpreted as a string, not an integer.

vars:
  default_value: 100

 

The debug module can be used to output the value, like this.

- name: output the content of the 'default_value' variable
  debug: 
    msg: "{{ default_value }}"

 

Which should return the following.

TASK [output the content of the 'default_value' variable]
ok: [server1.example.com] => {
    "msg": [
        "100"
    ]
}

 

The int filter could be used to increment the value by 1, like this.

- name: increment 'default_value' by 1
  debug: 
    msg: "{{ default_value | int + 1 }}"

 

Which should return the following.

TASK [increment 'default_value' by 1]
ok: [server1.example.com] => {
    "msg": [
        "101"
    ]
}

 



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 ee6c1 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   |