Ansible';何时';条件不从数组返回结果
面对一个有着可解释的情况的问题。寄存器返回一个json数组列表,但“when”条件不起作用。我将解释我在这里面临的问题: 首先,我注册目录:Ansible';何时';条件不从数组返回结果,ansible,Ansible,面对一个有着可解释的情况的问题。寄存器返回一个json数组列表,但“when”条件不起作用。我将解释我在这里面临的问题: 首先,我注册目录: - name: check if log dir exists win_stat: path: some\path register: register_some_dir 这是寄存器输出: "changed": false, "register_some_dir": { "msg": "All items completed",
- name: check if log dir exists
win_stat:
path: some\path
register: register_some_dir
这是寄存器输出:
"changed": false,
"register_some_dir": {
"msg": "All items completed",
"changed": false,
"results": [
{
"_ansible_parsed": true,
"stat": {
"isdir": true,
"isarchive": false,
"exists": true,
"isreadonly": false,
"creationtime": 1535985185.638003,
"isjunction": false,
"lastaccesstime": 1566915269.0181007,
"owner": "BUILTIN\\Administrators",
"nlink": 1,
"isreg": false,
"lastwritetime": 1566915269.0181007,
"islnk": false,
"attributes": "Directory",
"path": "f:\\some\\paths",
"filename": "logging",
"ishidden": false,
"isshared": false,
"hlnk_targets": [],
"size": 4619
},
"changed": false,
"_ansible_no_log": false,
"item": "Folder_One",
"_ansible_item_result": true,
"failed": false,
"_ansible_ignore_errors": null,
"_ansible_item_label": "Folder_One"
},
{
"_ansible_parsed": true,
"stat": {
"isdir": true,
"isarchive": false,
"exists": true,
"isreadonly": false,
"creationtime": 1535985188.0229728,
"isjunction": false,
"lastaccesstime": 1568382094.0390675,
"owner": "BUILTIN\\Administrators",
"nlink": 1,
"isreg": false,
"lastwritetime": 1568382094.0390675,
"islnk": false,
"attributes": "Directory",
"path": "f:\\some\\other\\paths",
"filename": "logging",
"ishidden": false,
"isshared": false,
"hlnk_targets": [],
"size": 248191373
},
"changed": false,
"_ansible_no_log": false,
"item": "Folder_Two",
"_ansible_item_result": true,
"failed": false,
"_ansible_ignore_errors": null,
"_ansible_item_label": "Folder_Two"
},
{
"_ansible_parsed": true,
"stat": {
"isdir": true,
"isarchive": false,
"exists": true,
"isreadonly": false,
"creationtime": 1535985191.886996,
"isjunction": false,
"lastaccesstime": 1566829433.7600543,
"owner": "BUILTIN\\Administrators",
"nlink": 1,
"isreg": false,
"lastwritetime": 1566829433.7600543,
"islnk": false,
"attributes": "Directory",
"path": "f:\\another\\paths",
"filename": "logging",
"ishidden": false,
"isshared": false,
"hlnk_targets": [],
"size": 0
},
"changed": false,
"_ansible_no_log": false,
"item": "Folder_Three",
"_ansible_item_result": true,
"failed": false,
"_ansible_ignore_errors": null,
"_ansible_item_label": "Folder_Three"
}
]
},
"_ansible_verbose_always": true,
"_ansible_no_log": false
使用json过滤器,我可以过滤如下路径:
- set_fact:
log_paths: "{{ register_some_dir | json_query('results[*].stat.path') }}"
when: register_some_dir.results[*].stat.exists == true
set_fact模块完全返回不带when条件的路径
但是“when”条件不接受[*],它表示这是意外的。但它是数组所必需的
如果我将[*]从when条件中删除,它表示“stat”没有arrtibuut
如何将when条件设置为起作用您必须循环检查结果并测试when条件中的每个元素 幸运的是,通过将过滤器集成到
json\u查询中的单个表达式中,您可以做您想做的事情
- set_fact:
log_paths: "{{ register_some_dir | json_query('results[?stat.exists].stat.path') }}"