Cyberithub

Using Arithmetic Operators in Ansible [Best Examples]

Advertisements

In this article, we will see different arithmetic operators in Ansible with the help of best examples. While writing ansible playbooks, many times you might have faced a scenario where you have to write some logic to perform certain types of operations by using different operators. In ansible, there are many different types of operators which are used to perform multiple different operations. Arithmetic operators are one such set of operators that are used to perform different arithmetic operations using jinja2 syntax in Ansible. Here we will see all those arithmetic operators and understand how to use it with the help of some real world examples.

 

Lab Setup

Although you can use any system with ansible installed on them for running below examples but in our case, we are using a localhost Ubuntu 20.04 LTS control node System. In this system, we have installed ansible using the steps mentioned in the article How to Install Ansible on Ubuntu 20.04 LTS (Focal Fossa).

 

Using Arithmetic Operators in Ansible [Best Examples]

Using Arithmetic Operators in Ansible [Best Examples]

Also Read: 15 ansible-vault command examples to encrypt and decrypt sensitive data/files on Linux

In the below given examples, we are using two variables a and b to store the values and performing different arithmetic operations on them and then displaying the output using debug module. Below are the list of arithmetic operators in ansible.

 

a) Addition(+)

If you would like to perform addition of two numbers in ansible then you can use the addition operator(+) and perform the sum as shown in below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 40
     b: 60
   tasks:
    - name: "Addition"
      debug:
        msg:
         - "The sum of two number is : {{ a+b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Addition] *********************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The sum of two number is : 100"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

b) Subtraction(-)

If you are looking to subtract two numbers in ansible then you need to use subtraction operator(-) and perform the operation as shown in below example.

Advertisements
cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 60
     b: 40
   tasks:
    - name: "Subtraction"
      debug:
        msg:
         - "The subtraction of two number is : {{ a-b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Subtraction] ******************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The subtraction of two number is : 20"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

c) Multiplication(*)

If you are looking to multiply two numbers in ansible then you need to use multiplication operator(*) to perform the operation as shown in below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 60
     b: 40
   tasks:
    - name: "Multiplication"
      debug:
        msg:
         - "The multiplication of two number is : {{ a*b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Multiplication] ***************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The multiplication of two number is : 2400"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

d) Division(/)

If you are looking to perform the division of two numbers, then you need to use division(/) arithmetic operator to perform the operation as explained in below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 80
     b: 40
   tasks:
    - name: "Division"
      debug:
        msg:
         - "The division of two number is : {{ a/b }}"

Output 

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Division] *********************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The division of two number is : 2.0"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

e) Truncated Integer Result(//)

If you are interested in finding the truncated integer result only then you need to perform the operation as explained in below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 70
     b: 40
   tasks:
    - name: "Truncated Integer Result"
      debug:
        msg:
         - "The truncated integer result of two number is : {{ a//b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Truncated Integer Result] *****************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The truncated integer result of two number is : 1"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

f) Modulo(%)

If you are interested in finding the remainder of an integer division then you need to use the modulo(%) operator to perform the operation as shown in the below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 70
     b: 40
   tasks:
    - name: "Remainder of an Integer Division"
      debug:
        msg:
         - "The remainder of an integer division is : {{ a%b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Remainder of an Integer Division] *********************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The remainder of an integer division is : 30"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

g) Raise left operand to the power of right operand(**)

You can also calculate the power of some integer by using a special arithmetic operator denoted as double star(**) as shown in the below example.

cyberithub@ubuntu:~$ nano operator.yml
---
 - name: Arithmetic Operator
   hosts: localhost
   gather_facts: false
   vars:
     a: 2
     b: 3
   tasks:
    - name: "Raise left operand to the power of right operand"
      debug:
        msg:
         - "Raise left operand to the power of right operand : {{ a**b }}"

Output

cyberithub@ubuntu:~$ ansible-playbook operator.yml

PLAY [Arithmetic Operator] **********************************************************************************************************************************

TASK [Raise left operand to the power of right operand] *****************************************************************************************************
ok: [localhost] => {
    "msg": [
        "Raise left operand to the power of right operand : 8"
    ]
}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Leave a Comment