Ansible日期比较EC2 [英] Ansible date compare EC2
问题描述
我是Ansible的新手. 我使用ansible创建了EC2实例,并能够使用EC2事实来检索启动时间.
I am new to Ansible . I created EC2 instances using ansible and able to to retrieve the launch time using EC2 facts .
但是我无法以日期格式存储启动时间.
But i am unable to store the launch time in date format .
我的目标是获得启动时间日期与系统日期的差(无法同时找到它并执行一些操作.
My goal is to get the difference of launch time date with system date (unable to find that also and perform some operations .
任何指导表示赞赏.
关于, 纳雷什·夏尔马(Naresh Sharma)
Regards, Naresh Sharma
推荐答案
自Ansible 2.2起,便提供了一个方便的to_datetime(format)
过滤器.
Since Ansible 2.2 there is a handy to_datetime(format)
filter available.
以下是您的任务示例:
---
- 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"
请注意[:19]
要获取前19个字符,以避免处理毫秒和时区字符.
Note [:19]
to get first 19 characters to avoid handling of milliseconds and timezone characters.
结果:
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
这篇关于Ansible日期比较EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!