尝试在ansible中转换纪元时间,但任务失败,因为我试图循环json输出。
我想做的是:
- 注册 win_find 模块的输出
- 仅过滤此寄存器中的创建时间值(eposh 值)
使用 strftime 过滤器将纪元值转换为可读值
- win_find: path: <path> register: register_path - set_fact: epoch_format: "{{ register_path | json_query('files[*].creationtime')}}"
这是创建时间过滤器输出
"ansible_facts": {
"epoch_format": [
1564668211.4169703,
1564668218.4144595,
1564668227.1259055,
1564668236.2201614,
1564668243.202953,
1564668251.3371267,
1564668259.5494978,
1564668268.9736576
现在我尝试转换
- set_fact:
readable_format: "{{ '%Y-%m-%d' | strftime({{ item }}) }}"
loop: '{{ epoch_format}}'
这就是我得到的结果
"msg": "template error while templating string: expected token ':',
got '}'. String: {{ '%Y-%m-%d' | strftime({{ item }}) }}",
如果我尝试引用 {{ item }} >> '{{ item }}' 我会得到这个:
"msg": "Invalid value for epoch value ({{ item }})",
有办法完成这个任务吗?
最佳答案
您应该从item
中删除花括号:
- set_fact:
readable_format: "{{ '%Y-%m-%d' | strftime(item) }}"
loop: '{{ epoch_format}}'
希望对你有帮助
关于ansible - 无法将纪元转换为ansible中可读的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57315441/