top of page
Writer's pictureSiddhesh Kadam

Ansible Archive/Unarchive Module

You can use Ansible archive module to create a compressed archive of one or more files or trees. Remote server should have define compress packages like tar,bzip2,gzip,zipfile installed.

Lets cover different scenario to get more hands on it.


Scenario 1 : Create a Zip Archive

To create a zip archive you need to define a remote file path, You can also define a multiple files path at a same time to create a compress archive. If destination path is not mention then archive module creates its archive copy in same folder of file path. This can be set using 'dst' parameter in respective location.

[root@siddhesh ~]# cat archive.yml
- hosts: dbserver
  tasks:
  - name: Create Zip Archive of multiple file
    archive:
      path:
       - /home/siddhesh/file2.pl
       - /home/siddhesh/file1.pl
       - /home/siddhesh/file3.pl
      dest: /tmp/remotefile.zip
      format: zip
[root@siddhesh ~]#

Here :

- hosts: dbserver ==> Host Inventory Group On which this action needs to be performed. - name: Create Zip Archive of multiple file ==> Task Description. archive: ==> Load archive module. path: /home/siddhesh/file2.pl ==> File Path of remote server to include in archive dest: /tmp/remotefile.zip ==> The file name of the destination archive. format: zip ==> The type of compression to use.


This playbook includes a task of archiving multiple remote files to make a zip compress file. Lets execute this and verify on remote server.

[root@siddhesh ~]# ansible-playbook archive.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [dbserver] *****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [Create Zip Archive of multiple file] **************************************************************************************************************************
changed: [node1.tecgeek.info] => {"archived": ["/home/siddhesh/file2.pl", "/home/siddhesh/file1.pl", "/home/siddhesh/file3.pl"], "arcroot": "/home/siddhesh/", "changed": true, "dest": "/tmp/remotefile.zip", "expanded_paths": ["/home/siddhesh/file2.pl", "/home/siddhesh/file1.pl", "/home/siddhesh/file3.pl"], "gid": 0, "group": "root", "missing": [], "mode": "0644", "owner": "root", "size": 304, "state": "file", "uid": 0}

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0
[root@siddhesh ~]#

On Remote Server :

[root@node1 ~]# ls -lhrt /tmp/remotefile.zip
-rw-r--r-- 1 root root 304 May  6 22:24 /tmp/remotefile.zip
[root@node1 ~]# unzip -l /tmp/remotefile.zip
Archive:  /tmp/remotefile.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  05-06-2020 22:20   file2.pl
        0  05-06-2020 22:20   file1.pl
        0  05-06-2020 22:20   file3.pl
---------                     -------
        0                     3 files
[root@node1 ~]#

Scenario 2 : Extract a Zip Arcive

In this scenario we'll see how to extract a content of Zip using ansible unarchive module.

[root@siddhesh ~]# cat unarchive.yml
- hosts: dbserver
  tasks:
  - name: extract Zip Archive content
    unarchive:
      src: /tmp/remotefile.zip
      dest: /tmp/code
[root@siddhesh ~]#

Here : - hosts: dbserver ==> Host Inventory Group On which this action needs to be performed. - name: extract Zip Archive content ==> Task Description. unarchive: ==> Load unarchive module. src: /tmp/remotefile.zip ==> local path to archive file to copy to the target server dest: /tmp/code ==> Remote absolute path where the archive should be unpacked

Lets run this playbook and verify content of destination path.

[root@siddhesh ~]# ansible-playbook unarchive.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [dbserver] *****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]

TASK [extract Zip Archive content] **********************************************************************************************************************************
changed: [node1.tecgeek.info] => {"changed": true, "dest": "/tmp/code", "extract_results": {"cmd": ["/usr/bin/unzip", "-o", "/root/.ansible/tmp/ansible-tmp-1588785354.56-147284268902512/source", "-d", "/tmp/code"], "err": "", "out": "Archive:  /root/.ansible/tmp/ansible-tmp-1588785354.56-147284268902512/source\n  inflating: /tmp/code/file2.pl      \n  inflating: /tmp/code/file1.pl      \n  inflating: /tmp/code/file3.pl      \n", "rc": 0}, "gid": 0, "group": "root", "handler": "ZipArchive", "mode": "0755", "owner": "root", "size": 4096, "src": "/root/.ansible/tmp/ansible-tmp-1588785354.56-147284268902512/source", "state": "directory", "uid": 0}

PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info         : ok=2    changed=1    unreachable=0    failed=0
[root@siddhesh ~]#

On Remote Server :

[root@node1 ~]# ls -lrht /tmp/code/
total 0
-rw-r--r-- 1 root root 0 May  6 22:20 file1.pl
-rw-r--r-- 1 root root 0 May  6 22:20 file2.pl
-rw-r--r-- 1 root root 0 May  6 22:20 file3.pl
[root@node1 ~]#

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
bottom of page