首页 > 解决方案 > 使用 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": ""
            }
        ]

标签: ansible

解决方案


问:如何使用 with_items 加入所有索引?

答:试试 json_query

loop: "{{ results|json_query('[].files[].path')|flatten }}"

(未测试)


推荐阅读