What is the Archive and Unarchive used for?
The Ansible archive helps you compress files into bz2, gz, tar, xz and zip formats. You can compress files and folders on local or remote hosts.
The Ansible unarchive unpacks archives. The default behavior of the unarchive is to copy from the local to the remote host and then uncompress.
Why use Archive and Unarchive?
The archive and unarchive modules are useful for moving large files and folders across host machines. For example, if you have a bunch of NGINX configuration files, you can use the unarchive command to download a zipped folder from an URL and unzip it. On the other hand, the archive module can be used to backup files and folders for future use.
An Example
Let’s try our hands in running an Ansible playbook to try out the archive and unarchive commands. For this example, we are going to use the localhost as both the source and the destination. We are going to first create a folder with a few files, zip it and then unzip it to a new location.
Let’s try by creating the following folders /test1 and /test2. In the test1 folder, create the folder project with text1.txt and text2.txt.
# mkdir test2
# cd test1
# mkdir project
# touch project/text1.txt
# touch project/text2.txt
So we have this directory structure in test1:
test1
`– project
|– text1.txt
`– text2.txt
1 directory, 2 files
Let’s create a simple playbook called Archive.yml in the test1 folder with following content:
– name: This is an archive example
hosts: 127.0.0.1
tasks:
– name: Archives the files and folders
archive:
path: /test1/project/*
dest: /test1/project.zip
format: zip
The playbook is instructing Ansible to create a zip file called project.zip with all the content inside the project folder on the local host (127.0.0.1).
Let’s run the playbook.
[WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match ‘all‘
PLAY [This is an archive example]
**********************************************************************************
***********
TASK [Gathering Facts]
***********************************************************************************
*********************************
ok: [127.0.0.1]
TASK [Archives the files and folders]
***********************************************************************************
*******************
changed: [127.0.0.1]
PLAY RECAP
***********************************************************************************
**********************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
If we check, we see that Ansible has created the zip file:
Archive.yml project project.zip
Now let’s unarchive. We can create an Unarchive.yml file with the following content in the /test2 folder:
– name: This is an unarchive example
hosts: 127.0.0.1
tasks:
– name: Unarchives the zip file
unarchive:
src: /test1/project.zip
dest: /test2
Let’s run the playbook:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match ‘all‘
PLAY [This is an unarchive example]
***********************************************************************************
*********************
TASK [Gathering Facts]
***********************************************************************************
**********************************
ok: [127.0.0.1]
TASK [Unarchives the zip file]
************************************************************************************
*********************
changed: [127.0.0.1]
PLAY RECAP
************************************************************************************
********************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
Now if we check test2 folder:
Unarchive.yml text1.txt text2.txt
We see that the text1.txt and text2.txt files have been uncompressed into the /test2 folder.
Using Ansible playbooks, we have successfully archived a folder and unarchived it in a different location.
Further Study:
