Comparison date of EC2 comparison
I am new to Ansible. I have created EC2 instances using the ability and ability to retrieve startup time using EC2 facts.
But I am unable to store the start time in date format.
My goal is to get the difference from the start date with the system date (could not find this and do some operations.
Any guidance is greatly appreciated.
Best regards, Naresh Sharma
0
source to share
1 answer
Since Ansible 2.2 a handy to_datetime(format)
filter is available .
Here's an example of your task:
---
- hosts: localhost
gather_facts: yes
tasks:
- name: local date
debug:
msg: "{{ ansible_date_time.iso8601 }}"
- ec2_remote_facts:
region: eu-west-1
register: ec2
- name: instance date
debug:
msg: "{{ ec2.instances[0].launch_time }}"
- name: date difference in days
debug:
msg: "{{ (ansible_date_time.iso8601[:19] | to_datetime(fmt) - ec2.instances[0].launch_time[:19] | to_datetime(fmt)).days }}"
vars:
fmt: "%Y-%m-%dT%H:%M:%S"
Note [:19]
to get the first 19 characters to avoid handling milliseconds and timezone characters.
Result:
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [local date] **************************************************************
ok: [localhost] => {
"msg": "2017-02-03T18:39:12Z"
}
TASK [ec2_remote_facts] ********************************************************
ok: [localhost]
TASK [instance date] ***********************************************************
ok: [localhost] => {
"msg": "2016-09-21T15:43:40.000Z"
}
TASK [date difference in days] *************************************************
ok: [localhost] => {
"msg": "135"
}
PLAY RECAP *********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
+3
source to share