我已经搜索了以下问题的答案。我可以找到一些相关的答案,但不幸的是不能解决我的问题。
我从调试win_find任务中的寄存器中取出了这个json:
- name: find docs older than 15 days
win_find:
paths: '{{ main_dir }}\{{ item }}\docs'
age: 15d
loop: '{{ all_folders}}'
register: register_files
- debug:
var: register_files
这是调试输出(我已删除一些属性以使代码更短且更易于解释):
"register_files": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_label": "folder_1",
"changed": false,
"examined": 378,
"files": [
{
"attributes": "Archive",
"checksum": "0351d8fffad7a16a5c21136ac5a77b33931237ad",
"creationtime": 1568633779.2339206,
},
{
"attributes": "Archive",
"checksum": "d25dd0915fe825790c8c3a27acdf6f5b34f6686b",
"creationtime": 1568633767.7103796,
"item": folder_1",
"matched": 302
},
"_ansible_item_label": "folder_2",
"changed": false,
"examined": 266,
"files": [
{
"attributes": "Archive",
"checksum": "0351d8fffad7a16a5c21136ac5a77b33931237ad",
"creationtime": 1568633779.2339206,
},
{
"attributes": "Archive",
"checksum": "d25dd0915fe825790c8c3a27acdf6f5b34f6686b",
"creationtime": 1568633767.7103796,
"item": folder_2",
"matched": 125
因为代码查找两个不同的文件夹,所以我将结果作为两个不同的字典列表返回。我如何将它们组合为一个,以便获得结果:
"files": [
{
"attributes": "Archive",
"checksum": "0351d8fffad7a16a5c21136ac5a77b33931237ad",
"creationtime": 1568633779.2339206,
},
{
"attributes": "Archive",
"checksum": "d25dd0915fe825790c8c3a27acdf6f5b34f6686b",
"creationtime": 1568633767.7103796,
{
"attributes": "Archive",
"checksum": "0351d8fffad7a16a5c21136ac5a77b33931237ad",
"creationtime": 1568633779.2339206,
},
{
"attributes": "Archive",
"checksum": "d25dd0915fe825790c8c3a27acdf6f5b34f6686b",
"creationtime": 1568633767.7103796,
答案 0 :(得分:2)
这是您要找的吗?
my_files: "{{ register_files.results|json_query('[].files')|flatten }}"
(未经测试)
答案 1 :(得分:2)
获得所需结果的一种简单方法是将json_query
filter与正确的jmespath
表达式一起使用
.and()
.exceptionHandling()
.accessDeniedHandler(accessDeniedHandler())
这已针对您的示例中采用的以下数据结构进行了测试,并已修复为以正确的json结尾。
- name: Get file info
debug:
msg: "{{ register_files | json_query('results[].files[]') | list }}"
请确保您在以后的问题中检查自己的MCVE。