【问题标题】:Ansible converting month to integerAnsible将月份转换为整数
【发布时间】:2021-03-03 00:44:30
【问题描述】:

我还没有找到任何东西。

我正在使用 Ansible,我正在尝试在相当长的一段时间后更改一些值。

但我的问题是我得到的值如下:

2020 Nov 19

但我需要它是这样的:

2020 11 19

我该怎么做?

【问题讨论】:

  • 你从哪里得到这些输入值?

标签: json ansible yaml ansible-awx ansible-tower


【解决方案1】:

使用to_datetimestrftime 你可以实现它。要应用的过滤器是:

"{{ '%Y %m %d' | strftime(( my_date_string | to_datetime('%Y %b %d')).strftime('%s')) }}"

作为参考,请参阅examples here。这个想法是使用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 }}"

输出:

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"
}

干杯

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 2013-03-08
    • 2023-03-13
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多