Ansible how to subtract two variables in a playbook?

1,400 views
Skip to first unread message

Andy Magana

unread,
Apr 29, 2019, 5:43:07 PM4/29/19
to Ansible Project

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:


#!/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



Thanks in advance. 

Andy Magana

Oklahoma, City OK

Adam E

unread,
Apr 30, 2019, 1:42:43 PM4/30/19
to Ansible Project
You could run a "shell" command and register the output to a variable using a command something like the following:

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

Alternatively, you could develop a action_plugin that could accept the filename as a parameter and perform the logic in python by first calling the stat module on the file as it also returns the last modification time of the file via mtime.

Matt Martz

unread,
Apr 30, 2019, 2:19:16 PM4/30/19
to ansible...@googlegroups.com
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 }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/48624339-9ab3-45aa-bd25-28a129ea2300%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Andy Magana

unread,
Apr 30, 2019, 10:47:04 PM4/30/19
to ansible...@googlegroups.com
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 ?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Adam E

unread,
Apr 30, 2019, 11:19:35 PM4/30/19
to Ansible Project
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)

- hosts: localhost
  connection: local
  tasks:
    - shell: "echo $((($(date +%s) - $(date +%s -r 'filenametocheck')) / 86400))"
      register: file_check
    - set_fact:
        file_days: "{{ file_check.stdout |int }}"
    - debug:
        msg: "file is {{ file_days }} days old"


On Tuesday, April 30, 2019 at 7:47:04 PM UTC-7, Andy Magana wrote:
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 ?



On Tue, Apr 30, 2019 at 12:42 PM Adam E <abed...@gmail.com> wrote:
You could run a "shell" command and register the output to a variable using a command something like the following:

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

Alternatively, you could develop a action_plugin that could accept the filename as a parameter and perform the logic in python by first calling the stat module on the file as it also returns the last modification time of the file via mtime.

On Monday, April 29, 2019 at 2:43:07 PM UTC-7, Andy Magana wrote:

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:


#!/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



Thanks in advance. 

Andy Magana

Oklahoma, City OK

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages