Ansible:通过调试提取文件名

时间:2017-09-22 13:35:32

标签: ansible

我的Ansible游戏中有以下调试输出,我正在尝试在"路径"中提取文件名。我想将它们(多个文件)从远程复制到本地计算机。但我失败了,细节如下。

ok: [environment_1] => {
    "changed": false,
    "examined": 42,
    "files": [
        {
            "atime": 1506085793.8824277,
            "ctime": 1506085493.1115315,
            "dev": 64771,
            "gid": 0,
            "inode": 41,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506085398.274815,
            "nlink": 1,
            "path": "/tmp/env1_2017-09-22_command_output.txt",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 70728,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1506085725.0863488,
            "ctime": 1506085718.8424354,
            "dev": 64771,
            "gid": 0,
            "inode": 40,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506085636.4565618,
            "nlink": 1,
            "path": "/tmp/env2_2017-09-22_command_output.txt",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 70952,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    ],
    "invocation": {
        "module_args": {
            "age": null,
            "age_stamp": "mtime",
            "contains": null,
            "file_type": "file",
            "follow": false,
            "get_checksum": false,
            "hidden": false,
            "path": "/tmp",
            "paths": [
                "/tmp"
            ],
            "patterns": [
                "*command_output.txt"
            ],
            "recurse": false,
            "size": null,
            "use_regex": false
        }
    },
    "matched": 2,
    "msg": ""
}

我有没有办法提取"path": "/tmp/env2_2017-09-22_command_output.txt" ""path": "/tmp/env2_2017-09-22_command_output.txt",这样我就可以使用fetch模块将它们从远程机器复制到本地机器。

现在我的游戏看起来像下面

---                                              
      - name: find the file in the temp location 
        find:                                    
            path: /tmp                           
            patterns: '*command_output.txt'      
        register: path_name                      

      - debug: msg={{ path_name.files.0.path }}  

      - fetch:                                   
            src: "{{ item }}"                    
            dest: ./environment_1/                 
            flat: yes                            
        with_items:                              
            - "path_name.files.0.path"           
            - "path_name.files.1.path"      

我的错误

{
changed": false,
    "file": "path_name.files.1.path",
    "item": "path_name.files.1.path",
    "msg": "the remote file does not exist, not transferring, ignored"
}

请帮助我理解我的错误。

1 个答案:

答案 0 :(得分:2)

您应该遍历path_name.files

- fetch:                                   
    src: "{{ item.path }}"
    dest: ./environment_1/
    flat: yes
  with_items: "{{ path_name.files }}"