Home > Software engineering >  Failing to create an archive with Ansible - Receiving a file not found error
Failing to create an archive with Ansible - Receiving a file not found error

Time:09-08

This seems like a silly question to ask, but I'm struggling to create a compressed archive using ansible then copying that to a remote host. I'm receiving an error that the target directory/file doesn't exist during a copy task. I've verified that he /home/ansible-admin/app/certs directory exists. But from what I can tell, the zip file is never being created.

---
- hosts: example
  become: yes
  
  tasks:
    - name: Create cert archive
      archive:
        path:
          - /home/ansible-admin/app/certs
        dest: /home/ansible-admin/app/app_certs.zip
        format: zip

    - name: Copy certs to target servers
      copy:
        src: /home/ansible-admin/app/app_certs.zip
        dest: /home/ubuntu/app_certs.zip
        owner: ubuntu
        group: ubuntu
        mode: "0400"

This is the error message I'm consistently getting An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option fatal: [app.example.com]: FAILED! => {"changed": false, "msg": "Could not find or access '/home/ansible-admin/app_certs.zip' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

I'm hoping I'm just missing something trivial here. But looking at the docs and the yaml file, I'm not seeing where the issue is. https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html

CodePudding user response:

From the documentation of the archive module:

The source and archive are on the remote host, and the archive is not copied to the local host.

I think that is your problem.

Should that path indeed exist on the remote host and create the archive at least the copy will fail as the archive won't be present on the controller.

CodePudding user response:

As tink pointed out, I was trying to archive a directory on the remote host that didn't exist rather than archiving a local directory. I resolved this by added a localhost task to be performed prior to.

- hosts: localhost
  tasks:
    - name: Create cert archive
      archive:
        path:
          - /home/ansible-admin/app/certs
        dest: /home/ansible-admin/app/app_certs.zip
        format: zip

Then copying it to the remote servers and extracting the archive worked as expected.

  • Related