# EC2 comparing dates

**URL:** <https://forum.ansible.com/t/ec2-comparing-dates/35723>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [September 11, 2021, 4:09pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723 "2021-09-11T16:09:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 11, 2021, 4:09pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/1 "2021-09-11T16:09:47Z")

</div>

Hi, would really appreciate some help if possible.

I am using ec2\_ami\_info to capture the creation date.

I register the output as ami\_creation\_date.

Output is: 2021-09-09

I would like to delete the AMI if it is 2 days older than now.

I’ve tried some code I’ve used in the past but getting templating errors etc.

Using the latest release of AWX.

Would really appreciate it if someone can help 🙂

thank you!

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [September 11, 2021, 6:56pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/2 "2021-09-11T18:56:23Z")

</div>

[https://stackoverflow.com/questions/48101921/ansible-compare-difference-between-two-dates-for-the-last-hour](https://stackoverflow.com/questions/48101921/ansible-compare-difference-between-two-dates-for-the-last-hour)

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 12, 2021, 7:50am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/3 "2021-09-12T07:50:47Z")

</div>

Thanks David

I have tried similar to the above but get a templating error:

{  
“msg”: “Unexpected templating type error occurred on ({{ ( (ansible\_date\_time.date - date\_earlier).total\_seconds() / 3600 ) | int }}): unsupported operand type(s) for -: ‘AnsibleUnsafeText’ and ‘dict’”,  
“\_ansible\_no\_log”: false  
}

Here is my code:

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 12, 2021, 8:18am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/4 "2021-09-12T08:18:34Z")

</div>

Sorry, that was out of date code, see below for latest

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 12, 2021, 8:20am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/5 "2021-09-12T08:20:02Z")

</div>

{  
“msg”: “Unexpected templating type error occurred on ({{ ( (date\_later - date\_earlier).total\_seconds() / 3600 ) | int }}): unsupported operand type(s) for -: ‘dict’ and ‘dict’”,  
“\_ansible\_no\_log”: false  
}

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 12, 2021, 3:25pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/6 "2021-09-12T15:25:09Z")

</div>

Anyone help me on this one please 🙂

I think I need to convert string to date but I cannot seem to get it to work.

Cheers!

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [September 12, 2021, 7:30pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/7 "2021-09-12T19:30:53Z")

</div>

Really not sure why you are trying to use Ansible for this type of workflow ?

Using Boto3 would be much better

.

import boto3  
import datetime

LaunchAge = 30

def days\_old(date):  
date\_obj = date.replace(tzinfo=None)  
diff = datetime.datetime.now() - date\_obj  
return diff.days

ec2 = boto3.client(‘ec2’)  
instance = ec2.describe\_instances()  
for i in instance[‘Reservations’]:  
for instance in i[“Instances”]:  
instance\_id = instance[“InstanceId”]  
LaunchDate = instance[‘LaunchTime’]

day\_old = days\_old(LaunchDate) # Get Date from Launch

if day\_old \> LaunchAge:  
response = ec2.stop\_instances(InstanceIds=[instance\_id])  
print(response)

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 12, 2021, 9:38pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/8 "2021-09-12T21:38:27Z")

</div>

Hi, bit of an odd statement no?

I use Ansible with AWX to make use of the task scheduling amongst all the other features.

This is just another use case to add to the list of many.

I think you may have misunderstood what I am trying to achieve.

This usecase is for a backup schedule.

I want to remove any AWS AMIs 2 days older than current date.

---

<div class="post-metadata">

**Author:** ![Oskar\_Almlov](https://avatars.discourse-cdn.com/v4/letter/o/34f0e0/32.png) [@Oskar\_Almlov](https://forum.ansible.com/u/Oskar_Almlov)\
**Post date:** [September 13, 2021, 10:00am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/9 "2021-09-13T10:00:10Z")

</div>

Hi,

When you register the debug task you are not only handed back the printed string but a dictionary containing information about the task execution.  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#registering-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables)

What goes wrong is that in your code you are trying to subtract a dictionary (date\_earlier) from a string.

Take a look at the below example and I think things will become clearer:

user@dev:~/code/snippets$ cat register.yml  
#!/usr/bin/ansible-playbook

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 13, 2021, 10:34am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/10 "2021-09-13T10:34:14Z")

</div>

Hi Oskar,

That makes sense, thank you

I now get a different error.

**Here is the code:**

---

<div class="post-metadata">

**Author:** ![Oskar\_Almlov](https://avatars.discourse-cdn.com/v4/letter/o/34f0e0/32.png) [@Oskar\_Almlov](https://forum.ansible.com/u/Oskar_Almlov)\
**Post date:** [September 13, 2021, 10:45am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/11 "2021-09-13T10:45:44Z")

</div>

HI,

When you set your fact it is being casted into a string and then you are trying to subtract a string from a string, which isn’t possible.

You need to convert the variable to an integer in order to perform mathematical operations on it:

- name: AMI Date  
set\_fact:  
date\_earlier: “{{ ec2\_ami\_info.images.0.creation\_date **| int** }}”

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 13, 2021, 11:08am UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/12 "2021-09-13T11:08:11Z")

</div>

again, makes sense, thank you.

I’ve changed as you suggested but get the same error.

Thanks!

---

<div class="post-metadata">

**Author:** ![Tony1](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Tony1](https://forum.ansible.com/u/Tony1)\
**Post date:** [September 13, 2021, 12:48pm UTC](https://forum.ansible.com/t/ec2-comparing-dates/35723/13 "2021-09-13T12:48:06Z")

</div>

thanks everyone for your help

Resolved with the below:
