Val*_*Ski 3 json yaml ansible ansible-awx ansible-tower
我还没有找到任何这方面的东西。
我正在使用 Ansible,并尝试在相当长的时间后更改一些值。
但我的问题是我得到的值如下:
2020 Nov 19
Run Code Online (Sandbox Code Playgroud)
但我需要它是这样的:
2020 11 19
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
使用to_datetime
并且strftime
你可以实现它。要应用的过滤器是:
"{{ '%Y %m %d' | strftime(( my_date_string | to_datetime('%Y %b %d')).strftime('%s')) }}"
Run Code Online (Sandbox Code Playgroud)
作为参考,请参阅此处的示例。这个想法是使用 将字符串值转换为日期时间to_datetime
,然后使用 将其转换为纪元时间strftime
,最后将其重新格式化为您想要的格式YYYY MM DD
。
包含 3 个日期示例的 PB:
---
- hosts: localhost
gather_facts: false
vars:
my_date_examples:
- "2020 Nov 04"
- "2020 Apr 11"
- "2020 Aug 23"
tasks:
- name: reformat date string
debug:
msg: "{{ '%Y %m %d' | strftime(( item | to_datetime('%Y %b %d')).strftime('%s')) }}"
loop: "{{ my_date_examples }}"
Run Code Online (Sandbox Code Playgroud)
输出:
TASK [reformat date string] *******************************************************************************************************************************************************************************************
ok: [localhost] => (item=2020 Nov 04) => {
"msg": "2020 11 04"
}
ok: [localhost] => (item=2020 Apr 11) => {
"msg": "2020 04 11"
}
ok: [localhost] => (item=2020 Aug 23) => {
"msg": "2020 08 23"
}
Run Code Online (Sandbox Code Playgroud)
干杯