Bootstrap FreeKB - Ansible - Increment or Decrement an integer using the int filter
Ansible - Increment or Decrement an integer using the int filter

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


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