Updated April 15, 2023
Introduction to Ansible Unarchive
Ansible provides very helpful modules that can be used to improve work efficiency and save time. One such module is Ansible unarchive, which is used to unpack an archive on target remote machines. This is useful in such cases where you want to install software on remote target machines using a tar.gz package file, sending an archived file from a local controller machine to remote target machines and unpacks.
Source of archived files can be remote target machine, local controller machine, or a location on the internet which is accessible via a URL. In this article, we will see the possible ways to use it and some practical examples.
What is Ansible Unarchive?
Ansible unarchive is a module which can unpack an archive file on remote target machines, after copying it either from local controller machine to remote machines or from with in the same machine on one location to other location on the same machine. There are a few points which we must consider before using this module: –
- This module is used to unarchive an archive file, which can be compressed But if a file is only compressed and not archived inside-outside, then this module will not work as expected.
- As per default, this module copies archived file to target remote machines and then unarchive there. In such cases where archive files are on remote machines only, then we can use parameter remote_src to tell that the source file is also remotely
- On remote machines, we must have zipinfo, gtar, or unzip commands For zip files it uses unzip and for .tar, .tar.gz, .tar.xz, .tar.bz2, it used gtar.
- Existing directories and files on target location are ignored, this is same as normal
- For windows systems, Ansible provides a similar module named Use this module instead of an unarchive module, as this is only for Linux like systems.
How Does Ansible UnarchiveWork?
Like any other Ansible module, an unarchive module has a set of parameters and related acceptable values. We can use a combination of those parameters to get acceptable results. Below is a list of some of the important parameters if not all.
- copy – This parameter is mutually exclusive with So this is deprecated. When this is used, it will copy the file from the local controller machine to remote machines, else the plugin will search for archive file on the remote machines. Acceptable values are “yes” and “no”. The default is “yes”.
- creates – This parameter will create the directory or file (if not already exists). If file or directory already exists, then it will
- decrypt – This option is used to control the auto-decryption of source files using Acceptable values are “yes” and “no”. The default is “yes”.
- src – The path of src file, it is local to controller machine by default, but when remote_src is used, then it will be considered on remote
- dest – Absolute path on the remote target machine, where packing
- exclude – This is the list of files and directories which you want to exclude from the
- owner – Same as chown, which will be used to set the ownership of files/directories.
- remote_src – This is used to set when the source of the file is on remote target
- selevel, serole, setype, seuser – These are used to update the selinux file
- group – To set the group owner of unpacked
- keep_owner – This is used to control the behavior of unpacked file so that newer files do not replace the existing files on the archive. Acceptable values are “yes” and “no”. The default is “no”.
- list_files – When this is set to “yes”. A list of files in the tarball will be returned. Acceptable values are “yes” and “no”. The default is “no”.
- mode – To set the permission of the target file or directory. The better practice is use 4 octal numbers inside single quotes to represent the permission like ‘0777’ or ‘0644’.
- validate_certs – This is used when we are using an https URL as the source of the file. Here the acceptable values are “yes” and “no”. The default is “yes”.
Examples of Ansible Unarchive
Now by using examples, we will try to learn about Ansible unarchive module, which you might have to use in day to day operations.
We will take some examples, but before going there, we first understand our lab, we used for testing purpose. Here we have an Ansible control server named ansible-controller and two remotes hosts named host-one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.
In this example, we will use a playbook like below content and try to unarchive a tar.gz file on remote
---
hosts: all gather_facts: no tasks:
name: here we check the current status of /var/tmp/sync_folder command: ls -l /var/tmp/sync_folder
register: remote_output ignore_errors: yes
debug:
msg:
standard output is "{{ remote_output.stdout_lines }}"
standard error is "{{ remote_output.stderr_lines }}"
name: Extract /var/tmp/sync_folder from local to /var/tmp/sync_folder on remote unarchive:
src: /var/tmp/sync_folder/file_dir.tar.gz dest: /var/tmp/sync_folder
name: here we check again the current status of /var/tmp/sync_folder
command: ls -l /var/tmp/sync_folder register: remote_output_1 ignore_errors: yes
debug:
msg:
standard output is "{{ remote_output_1.stdout_lines }}"
standard error is "{{ remote_output_1.stderr_lines }}"
We run this playbook like below: –
# ansible-playbook ansible_unarchive.yaml
The output will be below, where we can see the previously there was no file in folder
/var/tmp/sync_folder. But after unarchiving we have many files in the same folder.
In this example, we will use a playbook like below content and try to
---
hosts: all gather_facts: no tasks:
name: here we check the current status of /var/tmp/vlc command: ls -l /var/tmp/vlc
register: remote_output ignore_errors: yes
debug:
msg:
standard output is "{{ remote_output.stdout_lines }}"
standard error is "{{ remote_output.stderr_lines }}"
name: Here we download a file from internet and unarchive on remote unarchive:
src: http://download.videolan.org/pub/videolan/vlma/0.2.0/vlma-0.2.0- bin.tar.gz
dest: /var/tmp/vlc remote_src: yes
name: here we check the current status of /var/tmp/vlc command: ls -l /var/tmp/vlc
register: remote_output ignore_errors: yes
debug:
msg:
standard output is "{{ remote_output.stdout_lines }}"
standard error is "{{ remote_output.stderr_lines }}"
We run this playbook like below: –
# ansible-playbook ansible_unarchive_internet_tarball.yaml
The output will be like below: –
Conclusion
As we saw in this article, an Ansible unarchive module is a useful tool in your Ansible skillset, which is very handy to use. Also, in a Linux environment where software files are mostly in .tar.gz format, it becomes necessary to have knowledge of his module and using it in operations tasks. So learn it first, then use it.
Recommended Articles
This is a guide to Ansible Unarchive. Here we discuss How Does Ansible UnarchiveWork and Examples along with the codes and outputs. You may also look at the following articles to learn more –