Ansible - Increment or Decrement an integer using the int filter
by
Jeremy Canfield |
Updated: September 23 2024
| Ansible articles
Here is an example of how you can increment an integer using the int filter. In this example, the "count" variable will be incremented by 1.
---
- hosts: localhost
vars:
count: 10
tasks:
- ansible.builtin.set_fact:
count: "{{ count | int + 1 }}"
- ansible.builtin.debug:
var: count
...
Running this playbook should return the following, showing that the count variable is now 11.
ok: [localhost] => {
"count": "11"
}
And here is how you would decrement count by 1.
---
- hosts: localhost
vars:
count: 10
tasks:
- ansible.builtin.set_fact:
count: "{{ count | int - 1 }}"
- ansible.builtin.debug:
var: count
...
Did you find this article helpful?
If so, consider buying me a coffee over at