If you are not familiar with modules, check out Ansible - Getting Started with Modules.
Likewise, if you are not familiar with the unarchive module, check out my article Extract a tar zip bzip2 gzip archive using the unarchive module.
include can be used to only extract specific files from the archive. For example, let's say you first use list_files to extract an archive and store the list of files in the archive in a variable named list_files.
---
- hosts: localhost
tasks:
- name: extract example.zip to /tmp and list files too
unarchive:
src: /path/to/example.zip
dest: /tmp
list_files: true
register: list_files
- debug:
var: list_files
...
The list_files list could have something like this.
ok: [dlmbweb011.thrivent.com] => {
"list_files": {
"changed": false,
"dest": "/tmp",
"failed": false,
"files": [
"/path/to/directory/",
"/path/to/directory/foo.txt",
"/path/to/directory/bar.txt"
],
"gid": 0,
"group": "root",
"handler": "TgzArchive",
"mode": "01777",
"owner": "root",
"size": 4096,
"src": "/path/to/example.zip",
"state": "directory",
"uid": 0
}
}
By default, the unarchive module will extract all of the files and directories in the archive to a target directory. include can be used to only extract certain files and directories in the archive to a target directory. In this example, only files and directories containing "foo" will be extracted to the /tmp/foo_files directory.
---
- hosts: localhost
tasks:
- name: extract example.zip to /tmp and list files too
unarchive:
src: /path/to/example.zip
dest: /tmp/list_files
list_files: true
register: list_files
- debug:
var: list_files
- name: delete the /tmp/list_files directory
file:
path: /tmp/list_files
state: absent
- name: extract files and directories in example.zip containing 'foo'
unarchive:
src: /path/to/example.zip
dest: /tmp
include: "{{ item }}"
with_items: "{{ list_files.files }}"
when: item is search 'foo'
...
Did you find this article helpful?
If so, consider buying me a coffee over at