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.
list_files and register can be used to:
- extract an archive
- store the list of files in the archive in a variable
In this example, the foo.zip archive will be extracted to the /tmp directory and the files and directories in the foo.zip archive will be stored in a variable named list_files. I often find that all I want at first is to list the files in the archive. But, Ansible will extract the archive, so what I tend to do is to delete the extract archive, since the results will be stored in the list_files variable.
---
- hosts: localhost
tasks:
- name: extract example.zip to /tmp/list_files and list files too
unarchive:
src: /path/to/example.zip
dest: /tmp/list_files
list_files: true
register: list_files
- name: delete the /tmp/list_files directory
file:
path: /tmp/list_files
state: absent
- 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
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at