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 Head and Tail Command Tutorial

Many types of commands are available in bash to show the content a file. Most commonly used commands are ‘cat’,...
29/12/2020

Bash Comments

How to use single and multiple line comments in BASH Using comments in any script or code is very important to make the...
28/12/2020

Bash Arithmetic Operations

Doing arithmetic operations in bash is not similar to other standard programming languages. One of the limitations of bash...
29/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024