0

In Ansible, I want to fetch or copy zip files from remote server-B to currently logged remote server-A (both are Linux hosts).  As you can see below, I am specifying the source files with a wildcard (glob):

- hosts: server-A
  become: yes
  tasks:
  - name: copy the zip files
    fetch:
      src: /tmp/data/*.zip
      dest: /tmp/app/
      flat: yes
    register: zip_status
    delegate_to: server-B

Below is the error I get; I tried with copy/synchronize module as well, it's not working.  actually the file exist at destination server-B

fatal: [server-B -> 10.98.68.222]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "src": "/tmp/data/*.zip"
        }
    "msg": "file not found: /tmp/data/*.zip"

I tried Tim Kennedy's answer.  As stated above, I first login into Server-A.  As shown in the code above, I delegate to Server-B.  I added another delegate_to: server-B line, below.  As stated above, I want to copy .zip files from /tmp/data/ (from Server-B) to /tmp/app/ (Server-A).

- hosts: server-A
  become: yes
  tasks:
  - name: find the zip files
    find:
      paths: "/tmp/data"
      recurse: no
      patterns: "*.zip"
    register: zip_files
    delegate_to: server-B

  - name: copy the zip files
    fetch:
      src: "{{ item.path }}"
      dest: "/tmp/app/"
      flat: yes
    with_items: "{{ zip_files.files }}"
    register: zip_status
    delegate_to: server-B

Using find module, it displays the files correctly, but error using fetch module, it is trying to create directory /tmp/app/ when already existing. full permission in place.

fatal: [server-A]: FAILED! => {
    "msg": "Unable to create local directories(/tmp/app/): [Errno 13] Permission denied: '/tmp'"
2
  • What does "actually the file exist at destination server-B"  mean? Commented Apr 9, 2021 at 21:09
  • The user that ansible runs as doesn't have permission to create the local directory on server-A, /tmp/app. You should be able to fix that by setting an ACL or using a simple chmod Commented Apr 15, 2021 at 13:36

1 Answer 1

2

Fetch doesn't support directories or wildcards. The documentation is pretty clear that the string will be interpreted as a filename, and only a filename. You may be able to make it work in conjunction with a list of files provided by a separate lookup process.

Maybe something like:

- hosts: server-A
  become: yes
  tasks:
  - name: find the zip files
    find:
      paths: "/tmp/data"
      recurse: no
      patterns: "*.zip"
    register: zip_files
  - name: copy the zip files
    fetch:
      src: "{{ item.path }}"
      dest: "/tmp/app/"
      flat: yes
    with_items: "{{ zip_files.files }}"
    register: zip_status
    delegate_to: server-B

I'm not completely sure that this will work the way we want with the delegate_to, but it should at least get you pointed in the right direction.

2
  • In case you hadn't noticed, the OP says that they tried your suggested code and still see error; see the edit to the question. Commented Apr 9, 2021 at 21:13
  • @G-ManSays'ReinstateMonica' - thanks for the mention. I hadn't seen the update. Commented Apr 15, 2021 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .