Tony1
(Tony)
September 11, 2021, 4:09pm
1
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!
Tony1
(Tony)
September 12, 2021, 7:50am
3
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:
Tony1
(Tony)
September 12, 2021, 8:18am
4
Sorry, that was out of date code, see below for latest
Tony1
(Tony)
September 12, 2021, 8:20am
5
{
“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
}
Tony1
(Tony)
September 12, 2021, 3:25pm
6
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!
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)
Tony1
(Tony)
September 12, 2021, 9:38pm
8
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
Tony1
(Tony)
September 13, 2021, 10:34am
10
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 }}”
Tony1
(Tony)
September 13, 2021, 11:08am
12
again, makes sense, thank you.
I’ve changed as you suggested but get the same error.
Thanks!
Tony1
(Tony)
September 13, 2021, 12:48pm
13
thanks everyone for your help
Resolved with the below: