How to do Arithmetic Operations in Ansible

You can use arithmetic calculations in Ansible using the Jinja syntax. This is helpful in many situations where you have stored the output of an operation, and you need to manipulate that value. All usual operation like addition, subtraction, multiplication, division, and modulo are possible.

Let us start with an example. We will be using the debug module to print the out the result.

The following tasks show all the basic arithmetic operations. The output is given in comments.

Ansible arithmetic operation example

- hosts: loc
  tasks:
  - debug:
      msg: "addition{{ 4 +3 }}" #Ansible addition 7
  - debug:
      msg: "substraction {{ 4 - 3 }}"  #Ansible arithmetic substraction 1
  - debug:
      msg: "multiplication {{ 4 * 3 }}" #multiplication 12
  - debug:
      msg: "Modulo operation {{ 7 % 4}}" #ansible Modulo operation - find remainder 3
  - debug:
      msg: "floating division {{ 4 / 3}}" #ansible floating division 1.33333333333
  - debug:
      msg: "cube root {{ 27 | root(3)}}" # Ansible arithmetic cube root 3.0
  - debug:
      msg: "square root {{ 16 | root}}" # square root 3.0
  - debug:
      msg: "power {{ 3 | pow(3)}}" #Ansible arithmetic power of a number 27
  - debug:
      msg: "Absolute value {{ -16.7 | abs}}" #Absolute value of a number 16.7
  - debug: 
      msg: "Int conversion of float value {{ -19.23 |int}}" #int conversion of a float value -19
  - debug: 
      msg: "Multiple filters for getting absolute integer value of negative number {{ -43.33 |int|abs}}" #Absolute value of a number 43
  - debug: 
      msg: "Common Ansible round of a number {{ 39.7 | round}}" #40
  - debug: 
      msg: "Common Rounding of a number {{ 39.4 | round}}" #39
  - debug: 
      msg: "Common Rounding of a number {{ 39.5 | round}}" #40
  - debug: 
      msg: "Another way of Common Rounding of a number{{ 39.779 | round(0,'common')}}" #40
  - debug: 
      msg: "floor Rounding of a number {{ 39.779 | round(0,'floor')}}" #39.0 - - still a float. Use integer filter to convert to integer
  - debug: 
      msg: "Ansible floor Rounding of a number {{ 39.779 | round(1,'floor')}}" #39.7
  - debug: 
      msg: "floor Rounding of a number {{ 39.779 | round(2,'floor')}}" #39.77
  - debug: 
      msg: "Ceiling Rounding of a number {{ 39.779 | round(0,'floor')}}" #40.0 - still a float. Use integer filter to convert to integer
  - debug: 
      msg: "Ceiling Rounding of a number {{ 39.779 | round(1,'floor')}}" #39.8
  - debug: 
      msg: "Ceiling Rounding of a number {{ 39.779 | round(2,'ceil')}}" #39.78
  - debug: 
      msg: "log of a number {{ 16 | log(4)}}" #2.0

Ansible variable arithmetic example

You can also use the values stored in the variables for arithmetic operations. In the following task, I am storing the integer value ‘5’ in a variable int_var. In the debug task I am multiplying it with 5.

- hosts: loc
  vars:
    int_var: 5
  tasks:
  - debug:
      msg: "Ansible arithmetic opeartions using variable{{ int_var *5}}"

Converting String to Integer in Ansible

If the variable value is a string, you can convert it to an int using Jinja2 filters. In the following example, I am converting the string variable by using ‘|int’ filter.

- hosts: loc
  vars:
    str_var: "10"
  tasks:
  - debug:
      msg: "Ansible convertion of a string to an int {{ str_var|int * 5}}"

Calculating power of a number in Ansible

Calculating power or exponential of a number is possible using Ansible arithmetic operation. They have provided a ‘pow’ filter for the same.

The following example gives the value of 3^3 which is 27.

- hosts: all
  tasks:
  - name: Ansible arithemtic power filter example
    debug:
      msg: "{{ 3|pow(3)}}"

Calculating the root of numbers in Ansible

You can get the root of an umber in a similar manner using the root filter. The following example gives the cube root of 27 which is 3.

- hosts: all
  tasks:
  - name: Ansible arithemtic power filter example
    debug:
      msg: "{{ 27|root(3)}}"