Ansible Archive and Unarchive

29/12/2020
Ansible is a great tool to automate your configuration management. The benefit of Ansible is that you don’t need to set up a client on the remote machines. As long as there is an SSH connection with your control node, you can run your commands. The archive and unarchive are Ansible modules for compression.

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 test1
# mkdir test2
# cd test1
# mkdir project
# touch project/text1.txt
# touch project/text2.txt

So we have this directory structure in test1:

# tree 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.

# ansible-playbook Archive.yml
[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:

# ls
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:

# ansible-playbook Unarchive.yml
[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:

# ls
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:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash yes Command

Bash `yes` command is one of those commands of Linux that is related to the operation of another command. Using this command...
29/12/2020

Bash string manipulation

In bash, not unlike any other programing language, a program lives to put things into buckets and name them for later use....
29/12/2020

Bash Export Command

If you’ve ever stood in front of a terminal, typed `declare -p` to see what is going on with your variables in bash, and...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025