# Ansible how to subtract two variables in a playbook?

**URL:** <https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216>\
**Category:** Ansible Project\
**Created:** [April 29, 2019, 9:43pm UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216 "2019-04-29T21:43:07Z")\
**Posts on this page:** 5\
**Page:** 1

<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:43pm UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216/1 "2019-04-29T21:43:07Z")

</div>

So I created a simple bash script to compare dates of the current date and last date of a directory that was last modified.

What I am looking for is something done in Ansible that can take the converted date string of two variables and subtract the difference.

We have these DAT MacAfee files, I just don’t like the bash script I created and looking for something that can be done with a yaml playbook.

So here is my script:

`

```auto
#!/bin/bash
# TO PRINT THE NUMBER OF DAYS SINCE LAST AV UPDATE
past_date=$(du -h --time testdate | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}')

# Perform a subtraction of past_date string
# from today's string divided by seconds times 12 hour days  
diff=$((($(date +%s)-$(date +%s --date "$past_date"))/(3600*24)))
echo It has been $diff days since the last AV DAT update

```

`

```auto

```

Thanks in advance.

Andy Magana

Oklahoma, City OK

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [April 30, 2019, 5:42pm UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216/2 "2019-04-30T17:42:43Z")

</div>

You could run a “shell” command and register the output to a variable using a command something like the following:

```auto
echo $((($(date +%s) - $(date +%s -r "$filename")) / 86400))

```

Alternatively, you could develop a [action\_plugin](https://docs.ansible.com/ansible/latest/plugins/action.html) that could accept the filename as a parameter and perform the logic in python by first calling the [stat](https://docs.ansible.com/ansible/latest/modules/stat_module.html) module on the file as it also returns the last modification time of the file via mtime.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 30, 2019, 6:18pm UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216/3 "2019-04-30T18:18:26Z")

</div>

You could do something like this:

- stat:  
path: “/path/to/file”  
register: result

- debug:  
msg: “File was modified {{ days\_ago }} days ago”  
vars:  
days\_ago: “{{ ((result.stat.mtime - ansible\_date\_time.epoch|int)|int / 3600 / 24)|round(0, ‘floor’)|abs|int }}”

---

<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:** [May 1, 2019, 2:46am UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216/4 "2019-05-01T02:46:33Z")

</div>

So I really have been racking my melon on this so your saying I can echo with the shell module what is the -r any way ?

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [May 1, 2019, 3:19am UTC](https://forum.ansible.com/t/ansible-how-to-subtract-two-variables-in-a-playbook/30216/5 "2019-05-01T03:19:35Z")

</div>

Yeah, you can register the output of a shell command as a fact, below is simple example. Matts reply looks much nicer though, i would use his so you are not dependent on shell commands.

from the man page of date:

> -r, --reference=FILE  
> display the last modification time of FILE

working example (replace filenametocheck with your actual filename)
