Compressing files and directories is common in system administration. It helps save storage space and prepares data for transfer. Ansible’s archive module makes this task easier by automating the compression of files and directories on remote hosts. The module can create archive files in formats like gz, bz2, and xz.
This guide will show you how to use the archive module with practical examples.
Table of Contents
Basic Syntax of the archive Module
The basic syntax of the archive module is as follows:
- name: Archive files or directories
ansible.builtin.archive:
path: /path/to/source
dest: /path/to/destination.tar.gz
format: gz
owner: user
group: group
mode: '0644'
extra_opts: --exclude=*.log
remove: no
follow: no
include_hidden: no
Here is the explanation of each parameter.
- path. The path pf the file or directory you want to archive.
- dest. The destination path where the archive file will be created. The extension (.tar.gz, .tar.bz2, etc.) should match the format parameter.
- format. The format of the archive. Possible values are gz (default), bz2, xz, and zip.
- owner. The owner of the archived files.
- group. The group ownership for the archived files.
- mode. The permissions mode of the files inside the archive. Should be specified as a string, e.g., ‘0644’.
- extra_opts. Additional options to pass to the archive command, such as –exclude=*.log to exclude specific files.
- remove. Whether to remove the original files after archiving. Defaults to no.
- follow. Whether to follow symbolic links. Defaults to no.
- include_hidden. Whether to include hidden files (dotfiles) in the archive. Defaults to no.
- exclude_path. Specifies files or directories to exclude from the archive.
Compressing a Single File
To compress a single file, you can use the following playbook:
---
- name: Compress a single file using Ansible archive module
hosts: all
tasks:
- name: Archive a single file
ansible.builtin.archive:
path: /home/user/file.txt
dest: /home/user/file.tar.gz
format: gz
This playbook archives file.txt and saves it as file.tar.gz.
Compressing Multiple Files
To compress multiple files, specify the paths in the path parameter. Here’s an example:
---
- name: Compress multiple files using Ansible archive module
hosts: all
tasks:
- name: Archive multiple files
ansible.builtin.archive:
path:
- /home/user/file1.txt
- /home/user/file2.txt
dest: /home/user/files.tar.gz
format: gz
This playbook archives file1.txt and file2.txt into files.tar.gz.
Compressing a Directory
To compress an entire directory, use the following playbook:
---
- name: Compress a directory using Ansible archive module
hosts: all
tasks:
- name: Archive a directory
ansible.builtin.archive:
path: /home/user/directory
dest: /home/user/directory.tar.gz
format: gz
This playbook archives the contents of the directory into directory.tar.gz.
Excluding Files and Directories
To exclude specific files or directories from the archive, use the exclude_path parameter. Here’s an example:
---
- name: Exclude files and directories using Ansible archive module
hosts: all
tasks:
- name: Archive a directory excluding specific files
ansible.builtin.archive:
path: /home/user/directory
dest: /home/user/directory_excluded.tar.gz
format: gz
exclude_path:
- /home/user/directory/exclude_file.txt
- /home/user/directory/exclude_directory
This playbook archives the directory but excludes exclude_file.txt and exclude_directory.
Changing the Compression Format
You can change the compression format using the format parameter. Supported formats are gz, bz2, and xz. Here’s an example with bz2:
---
- name: Compress using bz2 format with Ansible archive module
hosts: all
tasks:
- name: Archive a directory with bz2 format
ansible.builtin.archive:
path: /home/user/directory
dest: /home/user/directory.tar.bz2
format: bz2
This playbook compresses the directory into directory.tar.bz2 using bz2 format.
Real-World Use Case: Automating Backup of Web Server Files
Imagine you have a web server running on multiple hosts with the document root at /var/www/html. You want to archive the contents of this directory daily, excluding any temporary files (*.tmp) or cache directories (cache). The archive should be saved in a backup directory with a timestamped filename for easy identification.
Let’s automate the backup process for a web server’s document root using the Ansible archive module. We’ll create a playbook that runs daily to compress and save the web server files, excluding temporary or cache files.
---
- name: Automated Backup of Web Server Files
hosts: web_servers
vars:
backup_dir: /backup/webserver
document_root: /var/www/html
tasks:
- name: Ensure backup directory exists
ansible.builtin.file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: Archive web server document root
ansible.builtin.archive:
path: "{{ document_root }}"
dest: "{{ backup_dir }}/web_backup_{{ ansible_date_time.date }}.tar.gz"
format: gz
exclude_path:
- "{{ document_root }}/cache"
- "{{ document_root }}/*.tmp"
- name: Verify backup file creation
ansible.builtin.stat:
path: "{{ backup_dir }}/web_backup_{{ ansible_date_time.date }}.tar.gz"
register: backup_file
- name: Print backup file status
ansible.builtin.debug:
msg: "Backup file created: {{ backup_file.stat.exists }}"
This playbook performs the following tasks.
- Ensure Backup Directory Exists: Uses file module to check if the backup directory (/backup/webserver) exists on the remote hosts. If it doesn’t, Ansible will create it with the specified permissions.
- Archive Web Server Document Root: Compresses the contents of the document root (/var/www/html), excluding any cache directories and temporary files. The resulting archive is saved in the backup directory with a timestamped filename (web_backup_YYYY-MM-DD.tar.gz).
- Verify Backup File Creation: Verify that the backup file was created successfully by checking its existence.
- Print Backup File Status: Prints a debug message indicating whether the backup file was successfully created.
You can also schedule it using a cron job on your Ansible control node.
Let’s, open the crontab editor.
# crontab -e
Add the following line to schedule the playbook:
0 0 * * * ansible-playbook /path/to/your/playbook.yml
This cron job runs the playbook daily at midnight, automating the backup process.
Conclusion
The archive module in Ansible is a powerful tool for compressing files and directories. It makes it easier to manage data storage and transfer. With these practical examples and real-world use cases, you can now integrate the archive module into your Ansible playbooks and streamline your system administration tasks.
You can use the unarchive module If you want to uncompress the compressed files and directories.
FAQs
1. How do I specify the compression format?
You can specify the format using the format parameter, with options like tar, zip, or gztar
2. Where does the compressed file get created?
The output archive file is created on the remote host at the path specified in the dest parameter.
3. Can I use the Archive Module to extract files?
No, the Archive Module is only for creating archives. To extract files, you would typically use the unarchive module.
4. Can I use variables for paths and filenames?
Yes, you can use Ansible variables to dynamically set paths and filenames for the archive.