# store the output of math operator

**URL:** <https://forum.ansible.com/t/store-the-output-of-math-operator/23727>\
**Category:** Ansible Project\
**Created:** [February 14, 2017, 12:30pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727 "2017-02-14T12:30:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 14, 2017, 12:30pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/1 "2017-02-14T12:30:12Z")

</div>

- name: Set the output of the this state  
set\_fact: A=“{{image\_avl.stdout}}” – this is coming from the previous registered output  
tags: avl1

- name: Define our static value for the desired image number  
set\_fact: B={{retain}} – this is coming from the inventory  
tags: num2

- name: difference  
set\_fact: C = {{A}} - {{B}}  
tags: del1

I want to store the value of A -B in C. However, its not storing and showing blank result

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 14, 2017, 3:12pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/2 "2017-02-14T15:12:37Z")

</div>

solved it.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 14, 2017, 3:28pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/3 "2017-02-14T15:28:53Z")

</div>

Well done!

Any chance of sharing \*how\* you solved it?

Thanks,

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 14, 2017, 4:28pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/4 "2017-02-14T16:28:21Z")

</div>

- name: Set the output of the this state  
set\_fact: A=“{{image\_avl.stdout}}” – this is coming from the previous registered output  
tags: avl1

- name: Define our static value for the desired image number  
set\_fact: B={{retain}} – this is coming from the inventory  
tags: num2

- name: difference  
shell: C=`expr "{{image_avl.stdout}}" - {{retain}}`  
register: num\_del  
tags: del1

- debug: var=“num\_del”

---

<div class="post-metadata">

**Author:** ![Nehal\_J\_Wani](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@Nehal\_J\_Wani](https://forum.ansible.com/u/Nehal_J_Wani)\
**Post date:** [February 15, 2017, 12:30pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/5 "2017-02-15T12:30:12Z")

</div>

Invoking the shell module is not necessary. One can use jinja2. An example:

$ cat a.yaml  
- hosts: localhost

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact: A="5"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact: B="6"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact: C="{{ A|int - B|int }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=C

$ ansible-playbook -v a.yaml  
No config file found; using defaults  
[WARNING]: Host file not found: /etc/ansible/hosts

[WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost]

---

<div class="post-metadata">

**Author:** ![asmagana](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/asmagana/32/2963_2.png) [@asmagana](https://forum.ansible.com/u/asmagana)\
**Post date:** [April 29, 2019, 9:57pm UTC](https://forum.ansible.com/t/store-the-output-of-math-operator/23727/6 "2019-04-29T21:57:36Z")

</div>

Hello from Oklahoma City,

My name is Andreas I noticed your post looking around for something about subtracting two variables but my vars are dates converted to strings but I can’t seem to get the two vars to subtract I started with a bash script.
