ansible - 使用 find (ansible) 模块读取多数组
问题描述
我有以下情况,有一些通过列表接收的目录,例如:
目录: - /path/to/dir1 - /path/to/dir2 - /path/to/dir3
我的目标是使用 find 模块获取这些目录的扩展名为 .sh 的文件并设置 +x 权限,当我注册 find 命令时,会收到如下输出:
[见代码]
如所见,文件是一个元组,然后为了加入第一个索引,我可以使用这个变量:
item.files.0.path
但是有可能为了目录的大小更大,我如何使用 with_items 加入所有索引?
非常感谢。此致
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"examined": 2,
"failed": false,
"files": [
{
..
"path": "/path/to/dir1/ficheros20.txt",
..
},
{
..
"path": "/path/to/dir1/ficheros20.txt",
..
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"depth": null,
"excludes": null,
"file_type": "file",
"follow": false,
"get_checksum": false,
"hidden": false,
"path": "/path/to/dir1/",
"paths": [
"/path/to/dir1/"
],
"patterns": [
"*"
],
"recurse": false,
"size": null,
"use_regex": false
}
},
"item": "/path/to/dir1/",
"matched": 2,
"msg": ""
},
{
"ansible_loop_var": "item",
"changed": false,
"examined": 2,
"failed": false,
"files": [
{
..
"path": "/path/to/dir2/ficheros20.txt",
..
},
{
..
"path": "/path/to/dir2/ficheros20.txt",
..
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"depth": null,
"excludes": null,
"file_type": "file",
"follow": false,
"get_checksum": false,
"hidden": false,
"path": "/path/to/dir2/",
"paths": [
"/path/to/dir2/"
],
"patterns": [
"*"
],
"recurse": false,
"size": null,
"use_regex": false
}
},
"item": "/path/to/dir2/",
"matched": 2,
"msg": ""
}
]
解决方案
问:如何使用 with_items 加入所有索引?
答:试试 json_query
loop: "{{ results|json_query('[].files[].path')|flatten }}"
(未测试)
推荐阅读
- html - div 元素上的样式应用于 div 内的按钮
- python - 将 RGB 深度图与没有内在矩阵的 RGB 图像对齐
- uikit - 我们如何在 UIStackview 中的两个视图之间创建一个具有自动宽度的间隔视图?
- python - 为什么 plot_model 会引发显示此神经网络的断言错误?
- python - 如何从缓冲区加载图像以进行预测?
- swift - UITableViewCell 中快速、模糊的图像运行不流畅
- ruby-on-rails - 图像未在 rails 6 视图中显示
- css - css input[type=radio] 在 IE 11 上无法正常工作
- bash - 从 bash 脚本作为此处文档运行时未执行 Snowsql 脚本
- python - 使用python在文件中写入avro记录