Ansible 日期比较 EC2 [英] Ansible date compare EC2
本文介绍了Ansible 日期比较 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 .
感谢任何指导.
问候,纳雷什·夏尔马
推荐答案
从 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屋!
查看全文