Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Ansible中使用调试模块访问嵌套变量_Python_Variables_Debugging_Ansible - Fatal编程技术网

Python 如何在Ansible中使用调试模块访问嵌套变量

Python 如何在Ansible中使用调试模块访问嵌套变量,python,variables,debugging,ansible,Python,Variables,Debugging,Ansible,我使用此任务访问文件: - name: Find logs files find: paths: /var/log/ patterns: "*.log" recurse: yes register: list_matched - debug: var: list_matched.files 输出为: ok: [127.0.0.1] => { "changed": false, "examined": 17, "file

我使用此任务访问文件

- name: Find logs files
  find:
    paths: /var/log/
    patterns: "*.log"
    recurse: yes
    register: list_matched

- debug:
    var: list_matched.files
输出为:

ok: [127.0.0.1] => {
    "changed": false,
    "examined": 17,
    "files": [
        {
            "atime": 1576496687.5594354,
            "ctime": 1576496687.5594354,
            "dev": 2049,
            "gid": 0,
            "gr_name": "root",
            "inode": 922589,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "",
            "mtime": ,
            "nlink": 1,
            "path": "/var/log/mikrotik/mikrotik.log",
            "pw_name": "root",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
如何获取路径参数?我也尝试过这样的事情:

- debug:
    var: list_matched.files.path
many_paths = []
for i in range(len(list_matched.files)):
    print(list_matched.files[i].path)
    many_paths.append(list_matched.files[i].path)

print(many_paths)
但是发生了一个错误:

ok: [127.0.0.1] => {
    "files_matched.path": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'path'"

我想捕获路径参数,但不知道如何捕获。也很难找到有关这个嵌套调试案例的一些信息。

find返回一个
dict
值,因此您必须遍历该列表

- name: Show file paths
  debug:
    msg: "{{ item.path }}"
  with_items: "{{ list_matched.files }}"
“files:”后面有一个开放的括号“[”,因此在转到path之前,需要转到数组中的该位置

尝试:

或:

要回答“当我获得更多路径时,我应该如何使用它?”的评论,请尝试获取“list_matched.files”数组的长度,并像下面这样循环:

- debug:
    var: list_matched.files.path
many_paths = []
for i in range(len(list_matched.files)):
    print(list_matched.files[i].path)
    many_paths.append(list_matched.files[i].path)

print(many_paths)

也许您想将它们保存到一个路径数组中。

谢谢!这对一个路径非常有效,但是当我获得更多路径时,我应该如何使用它?我用循环更新了答案,以打印出其他路径并将它们保存到一个变量中。