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:

Sandclock 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, Sandclock 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

How to use inotify and rsync to create a live backup system using only a while loop in a bash script

Why should you use Bash Scripts to perform folder synchronizations and backups? Bash is by far the most popular and used...
28/12/2020

Bash base64 encode and decode

To encode or decode standard input/output or any file content, Linux uses base64 encoding and decoding system. Data are...
29/12/2020

Bash builtin examples

builtin candidate_builtin arg … in bash allows you to only call bash builtins. That is, even if an external command or...
29/12/2020
Bài Viết

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

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021