Ansible

Archive and Unarchive Modules in Ansible

We already know how to compress and uncompress the files or folders in Windows. But today, we learn how we compress the files and folders using the Ansible tool. To understand both terminologies of Ansible, let’s first discuss about the Archive module. Later, on we will discuss about the Unarchive module in Ansible with explained examples.

Archive Module in Ansible

An archive module in the Ansible tool is a collection of any content that is shifted to a keeping device or remote device so that we can keep the content for a long period which is held apart for regulatory needs or has been removed from the main keeping device or host machine. The content which we want to compress can be a document file or a directory that contains multiple subdirectories or document files in it. To use the archive functionality in the Ansible tool, we use the release of Ansible 2.3 or above version because before that version of Ansible, we utilize the shell module to compress a folder in Ansible. To compress the document files and directories, there are many formats in Ansible 2.3 version or above that we will use like .zip, .tar, .gz, .bz2, etc to archive the directory. To compress the document file or directory, the virtual host device must contain the content that we want to compress.

Prerequisites to Archive the Content in Ansible

We assume that you have an Ansible Controller with the configured Ansible on your device and some Virtual Machines so that we can validate the functionality of the archive modules in the Ansible tool.

To understand the concept of an archive module in Ansible, let’s implement an example of an Archive so that we can easily understand the concept of compressing the content in Ansible.

Example: Compressing the File or Directory in .Zip Format

Here is the very first example of the Ansible archive module where we compress the document or directory in a .zip format. To compress the document in .zip format, we first create the playbook. In the playbook, we write the location of the document or directory which we want to compress and also write the destination point where we want to place the archive file or folder. The command that is used to construct the playbook in the Ansible tool is listed in the following:

[root@master ansible]# nano archive.yml

After writing the command, an archive.yml playbook file appears. First, we provide “all” the remote hosts where we want to archive the document. To acquire the information about the remote hosts, the Ansible playbook’s gather_facts variable launches the configuration module by default at the beginning of the archive.yml playbook. After that, we write the tasks which we want to do in the archive.yml playbook.

In the task’s module, we write the name of the task. We write the archive module which shows that we want to compress the document. In the archive module, the document file or directory that is going to be compressed must be provided as the minimum single “path” argument or three arguments which are the “path”, “dest”, and “format” when compressing a document. The “path” argument contains the source document’s actual path which is /home/ansible/Test.

The targeted device’s path is /tmp/Test.zip and the format of the archive module is .zip. If somehow the target directory is not provided, it reverts to the default directory of the document file. The following is the playbook which contains the complete tasks of the archive module:

- hosts:

    - all
  gather_facts: false
  tasks:
      - name: Compress directory /home/ansible/Test into directory /tmp/Test.zip
        archive:
          path: /home/ansible/Test
          dest: /tmp/Test.zip
          format: zip

To exit the playbook archive.yml, we press “ctrl+x” and save the modifications by pressing the “y”.

If you want to run the archive.yml playbook, we first need to build the inventory file. Next, we establish the connections between both the remote hosts and the Ansible controller. To create the inventory file, we write the following command:

[root@master ansible]# nano host.yml

The host.yml inventory file appears on the new screen. Now, we write the information regarding the hosts. Since we write “all” hosts in the archive.yml playbook, we first write the “All” and then we write the “hosts” information. We provide the host IP address, the name of the ansible user, the password of the ansible, the connection name, and the available port number in the inventory file as you can see in the following:

All:

  hosts:
    Host1:
      ansible_host: 192.168.3.229
      ansible_user: ansible
      ansible_password: ********
      ansible_connection: ssh
      ansible_port: 22

Now, we execute the playbook and the inventory file which we already created by writing the following command:

[root@master ansible]# ansible-playbook archive.yml –i host.yml

Here is the outcome of the Ansible archove.yml playbook:

As you can see in the output, the directory of host1 is compressed by showing the “ok” signal.

Unarchive Module in Ansible

The unarchived module is used to unpack or uncompress the content of the document or directory. To use the unarchive module of Ansible, the document or directory should be available in archived form.

Example: Uncompressing the File or Directory of the .Zip Format

We already archived the directory which we recently created. When we uncompress the directory or file, it provides the option to duplicate the compressed document or directory to the remote host. To unarchive the document or directory, we first create the playbook where we write the source and the destination of the directory.

[root@master ansible]# nano unarchive.yml

We create the unarchive.yml playbook. Now, we write the remote hosts and the tasks which we want to implement in the playbook. In the tasks, we write the “src” variable and then pass the path of the archived directory which is /tmp/pre-requistes.zip. Then, we write the “dest” variable and place the path which is /home/ansible/ where we want to unpack the content of the directory.

- hosts:

- all

gather_facts: false

tasks:

- name: Unarchive pre-requisites.zip from the controller machine into /home/ansible on the target machine

unarchive:

src: /tmp/pre-requisites.zip

dest: /home/ansible/

We already created the previous inventory file which is host.yml so we don’t have to build the connection again between the Ansible controller and the remote hosts. We just simply write the ansible-playbook command and call the host.yml inventory file in it. If we don’t have the connection, we build the connection. To run the Ansible unarchive.yml playbook, write the following command:

[root@master ansible]# ansible-playbook unarchive.yml –i host.yml

The following is the executable output of the Ansible playbook where we unpack the content of the directory:

Conclusion

We learned how to compress and uncompress a document or directory using the Ansible tool. First, we have a default directory which we converted to an archive module. Then, we unarchived the archived directory by implementing the example of the .zip format with a detailed explanation.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content