ddro
ddro

Reputation: 211

Ansible - is there an elegant way to tar.gz and rsync a large directory?

I'm trying to avoid a list of 'command' modules in my ansible play, but there seems to be a void of ansible docs regarding tar/gz and the synch module seems... incomplete.

I'd like to gzip a tarball of a big directory then rsync it to another host. Even using 'command' seems to not work for me :<

 "warnings": ["Consider using unarchive module rather than running tar"]}
 [WARNING]: Consider using unarchive module rather than running tar


PLAY RECAP *********************************************************************
ctarlctarl                 : ok=2    changed=0    unreachable=0    failed=1   

The 'unarchive' module seems to expect an already compressed/archived directory and doesn't appear to be the solution I want.

Related but unanswered: ansible playbook unable to continue as the `tar` fails due to `file change as we read`

(Edit) showing the task since it was asked if I remembered the z. =)

  - name: tar ball app dir, exclude conf files
    command: "tar -zcvf {{ item.code_dir }}.tar.gz --exclude '*config*' ."
    args:
      chdir:  "{{ apps_home }}/{{ item.code_dir }}"
    with_items:
      - "{{ processes }}"

Upvotes: 2

Views: 5488

Answers (3)

Robin Roth
Robin Roth

Reputation: 1329

Starting from version 2.3 Ansible has the archive module: https://docs.ansible.com/ansible/2.9/modules/archive_module.html

Since Ansible version 8.4 this module was moved to community.general collection: https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html

Also, you can use the synchronize module to avoid calling rsync via command.

With that you can archive, transfer and unarchive all with Ansible modules and don't have to fiddle with command line arguments.

Upvotes: 2

elkan1788
elkan1788

Reputation: 95

This warning tip you can use the ansible unarchive module instead of tar commnad. The syntax is very easy just like below:

- unarchive: src=foo.tgz dest=/var/lib/foo

And more detail info you can got from here: unarchive_module

Upvotes: -1

ddro
ddro

Reputation: 211

So, I got it working... just inelegantly.

  1. With the v flag set, my ansible stdout was horrendously verbose (even without calling -vvvv with ansible-playbook). Turning off the switch inside the tar command made tracking down the problem easier.
  2. The problem was actually the error "error: file changed as we read it" - no doubt because I was adding the archive file into the directory it was compressing. I solved it by simply moving the archived file up a directory when running.

This, however, leaves me with the 'command after command' solution - which is what I hope I'll eventually be able to avoid. Half way there, though.

Upvotes: 0

Related Questions