EC2 comparing dates

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 :slight_smile:

thank you!

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

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:

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

{
“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
}

Anyone help me on this one please :slight_smile:

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

Cheers!

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)

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.

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

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

Hi Oskar,

That makes sense, thank you

I now get a different error.

Here is the code:

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 }}”

again, makes sense, thank you.

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

Thanks!

thanks everyone for your help

Resolved with the below: