If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The archive module is used to:
- Create a gzip compressed tar archive of one or more directories
- Create a gzip compressed archive of one or more files
AVOID TROUBLE
The archive module is part of the community.general collection. The ansible-galaxy collection install community.general command can be used to install the community.general collection.
In this example, the /tmp/foo directory will be compressed into a TAR archive. By default, a gzip compressed tar archive will be created. Even if dest contains something like foo.tar.bz2 or foo.zip, the destination file will actually be gzip compressed.
---
- hosts: all
tasks:
- name: create foo.tar.gz
community.general.archive:
path: /tmp/foo
dest: /path/to/foo.tar.gz
...
In this example, the /tmp/foo.txt file will be gzip compressed.
When compressing a file, a copy of the file will be created. For example, when compressing foo.txt, the original foo.txt file will not be removed. For this reason, you may want to include remove: true to remove the original file
---
- hosts: all
tasks:
- name: create foo.txt.gz
community.general.archive:
path: /tmp/foo.txt
dest: /path/to/foo.gz
remove: true
...
The format parameter can be used to specify the type of compression to use. In this example, a directory is compressed into a zip archive.
---
- hosts: all
tasks:
- name: zip foo.txt
community.general.archive:
path: /path/to/directory
dest: /path/to/directory.zip
format: zip
...
In this example, bzip2 TAR archive will be created.
---
- hosts: all
tasks:
- name: bzip compress foo.tar
community.general.archive:
path: /path/to/directory
dest: /path/to/directory.tar.bz2
format: bz2
...
Be aware that if the target directory (path) does not exist, something like this will be returned.
TASK [create foo.tar.gz]
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "path /tmp/foo.tar.gz does not exist", "path": "/tmp/foo.tar.gz"}
For this reason, you may want to first use the find module to determine if the target directory exists, and then only run the archive module when the target directory exists.
---
- hosts: all
tasks:
- find:
paths: /tmp
patterns: foo
file_type: directory
recurse: false
register: out
- name: create foo.tar.gz
community.general.archive:
path: /tmp/foo
dest: /path/to/foo.tar.gz
when:
- out.matched == 1
...
The content of multiple files can be appended to a gzip archive.
---
- hosts: all
tasks:
- name: create foo.tar.gz
community.general.archive:
path:
- /path/to/foo.txt
- /path/to/bar.txt
dest: /path/to/foo.gz
...
Multiple directories can be appended to a TAR archive.
---
- hosts: all
tasks:
- name: create foo.tar.gz
community.general.archive:
path:
- /path/to/dir1
- /path/to/dir2
dest: /path/to/foo.tar.gz
...
The register parameter can be used to store the output. In this example, the output is stored in the "out" variable.
---
- hosts: all
tasks:
- name: create foo.tar.gz
community.general.archive:
path: /path/to/directory
dest: /path/to/foo.tar.gz
register: out
...
The debug module can be used to print the out variable.
- debug:
var: out
If the archive is not created, something like this should be returned.
TASK [debug]
ok: [server1.example.com] => {
"out": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
If the archive is created, something like this should be returned.
TASK [debug]
ok: [server1.example.com] => {
"out": {
"archived": [
"/tmp/foo.txt",
"/tmp/bar.txt",
"/tmp/files.tar.gz",
"/tmp/ansible_archive_payload_rbz5HT/ansible_archive_payload.zip"
],
"arcroot": "//",
"changed": true,
"dest": "/tmp/files.tar.gz",
"expanded_exclude_paths": [],
"expanded_paths": [
"/tmp"
],
"failed": false,
"gid": 0,
"group": "root",
"missing": [],
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 84348,
"state": "file",
"uid": 0
}
}
Append
The archive module does not have an option to append to an archive that already exists. In this example, the find module is used to find files matching foo below the /tmp directory, and then the foo array contains each matched file. Each file in the foo array will be appended to the archive.
---
- hosts: all
tasks:
- find:
paths: /tmp
patterns: foo
register: out
- name: create foo.tar.gz
community.general.archive:
path: "{{ foo }}"
dest: /path/to/foo.tar.gz
vars:
foo: "{{ out.files | map(attribute='path') | list }}"
...
Did you find this article helpful?
If so, consider buying me a coffee over at