Get file(s) from a remote host to local host

11 views
Skip to first unread message

R-JRI

unread,
Sep 18, 2019, 2:31:17 PM9/18/19
to Ansible Project
Hi,

I created a simple yml file to get file or files from a remote host to local host:
$ cat fetch.yml
---
- hosts: serverA

  tasks:
  - name: find the files
    find:
      paths: /usr/local/apps/log
      patterns: AppsAdmin.*.ALLAPPS.20190828*
      use_regex: True
    register: Apps_files

  - debug:
      var: Apps_files.files.path

  - name:
    fetch:
      src: "{{ item }}"
      dest: /APPADMIN/ansible/nonprod/DR_test/
    with_items: "{{ Apps_files.files.path }}"

The variable 'Apps_files.files.path' is getting from the output of debug.

Here is the output when ran it.

[appadmin@serverB][/APPADMIN/ansible/nonprod/DR_test]$ ansible-playbook  fetch.yml

PLAY [serverA] ****************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [serverA]

TASK [find the files] ***********************************************************************************************************************************************************************************************************************
ok: [serverA]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [serverA] => {
    "Apps_files.files.path": "VARIABLE IS NOT DEFINED!"
}

TASK [fetch] ********************************************************************************************************************************************************************************************************************************
fatal: [serverA]: FAILED! => {"msg": "'list object' has no attribute 'path'"}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
serverA     : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


When debug model only has Apps_files as variable:
-debug:
     var: Apps_files

The output of debug is:
TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [serverA] => {
    "Apps_files": {
        "changed": false,
        "examined": 36,
        "failed": false,
        "files": [
            {
                "atime": 1567021803.0147924,
                "ctime": 1567021803.0087924,
                "dev": 64770,
                "gid": 0,
                "gr_name": "root",
                "inode": 134304,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0644",
                "mtime": 1567021803.0047922,
                "nlink": 1,
                "path": "/usr/local/apps/log/AppsAdmin.stop.ALLAPPS.201908281548.21882.log", <-- file wanted to get from serverA
                "pw_name": "root",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 10253,
                "uid": 0,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            },
            {
                "atime": 1568752920.7074213,
                "ctime": 1567023296.6011353,
                "dev": 64770,
                "gid": 0,
                "gr_name": "root",
                "inode": 134315,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0644",
                "mtime": 1567023296.5971353,
                "nlink": 1,
                "path": "/usr/local/apps/log/AppsAdmin.start.ALLAPPS.201908281606.30560.log",   <-- file wanted to get from serverA
                "pw_name": "root",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 8549,
                "uid": 0,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            }
        ],
        "matched": 2,
        "msg": ""
    }
}

So I modified variable of 'debug' to 'Apps_files.files.path' for the file name and location that I want to get. Does anyone know why it failed? Thanks.


Brian Coca

unread,
Sep 18, 2019, 4:52:13 PM9/18/19
to Ansible Project
Your issue is that Apps_files.files is a list, so Apps_files[0].path
would access the path of the FIRST file in that list, which means you
have to extract path for each item to make the fetch loop work:

- name:
fetch:
src: "{{ item }}"
dest: /APPADMIN/ansible/nonprod/DR_test/
loop: "{{ Apps_files.files|map(attribute='path')|list }}"


----------
Brian Coca

R-JRI

unread,
Sep 19, 2019, 1:10:06 PM9/19/19
to Ansible Project
Thanks, Brian

That works.
Reply all
Reply to author
Forward
0 new messages