unixia
unixia

Reputation: 4320

Ansible: Copy contents of a directory to a location

I'm willing to transfer the contents of a folder unzipped from a source say myfolder to a location say dest_dir but apparently everything I try moves/copies/generates myfolder in the dest_dir location.

I tried

command: mv src dest_dir

I also tried unarchiving in the dest_dir location using,

unarchive:
    src: /path/to/myfolder
    dest: dest_dir
    copy: no
  become: yes

Apparently, for copy module, I found that remote_src does not support recursive copying yet.

What is the correct way to go about this?

Normally, in my system, I would do mv /path/to/myfolder/* dest_dir but wildcards throw an error with Ansible.

I'm using Ansible 2.3.2.

Upvotes: 0

Views: 377

Answers (2)

The HCD
The HCD

Reputation: 510

I've had no issue with unarchive module from a tgz, perhaps you could consider creating tgz before unarchiving

- name: 'unarchive server files from last backup'
  unarchive:
    src: '/{{ backup_dir }}/backup.{{ ansible_hostname }}.tgz'
    dest: '{{ restore_dest_dir }}'
    remote_src: true
  register: unarchived_server_backup

where the backup was created as follows:

    # tgz backup server files
    - name: 'backup server files defined by variable'
      archive:
        path: '{{ files_to_archive }}'
        dest: '/{{ server_backup_dir }}/backup.{{ ansible_hostname }}.tgz'
        mode: 0755
        format: gz
      register: archived_server_backup

Upvotes: 0

techraf
techraf

Reputation: 68449

The reason you can't do it easily in Ansible is because Ansible was not designed to do it.

Just execute the command directly with shell module. Your requirement is not idempotent anyway:

- shell: mv /path/to/myfolder/* dest_dir
  become: yes

Pay attention to mv defaults, you might want to add -f to prevent it from prompting for confirmation.


Otherwise play with synchronize module, but there's no value added for "move" operation. Just complexity.

Upvotes: 1

Related Questions