ansible.builtin.unarchive module – Unpacks an archive after (optionally) copying it from the local machine
Note
This module is part of ansible-core
and included in all Ansible installations. In most cases, you can use the short module name unarchive
even without specifying the collections keyword. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.unarchive
for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.
Synopsis
- The ansible.builtin.unarchive module unpacks an archive. It will not unpack a compressed file that does not contain an archive.
- By default, it will copy the source file from the local system to the target before unpacking.
- Set
remote_src=yes
to unpack an archive which already exists on the target. - If checksum validation is desired, use ansible.builtin.get_url or ansible.builtin.uri instead to fetch the file and set
remote_src=yes
. - For Windows targets, use the community.windows.win_unzip module instead.
Note
This module has a corresponding action plugin.
Parameters
Parameter | Comments |
---|---|
attributes aliases: attr string | The attributes the resulting filesystem object should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr. The |
copy boolean | If true, the file is copied from local controller to the managed (remote) node, otherwise, the plugin will look for src archive on the managed machine. This option has been deprecated in favor of This option is mutually exclusive with Choices:
|
creates path | If the specified absolute path (file or directory) already exists, this step will not be run. The specified absolute path (file or directory) must be below the base path given with |
decrypt boolean | This option controls the auto-decryption of source files using vault. Choices:
|
dest path / required | Remote absolute path where the archive should be unpacked. The given path must exist. Base directory is not created by this module. |
exclude list / elements=string | List the directory and file entries that you would like to exclude from the unarchive action. Mutually exclusive with Default: |
extra_opts list / elements=string | Specify additional options by passing in an array. Each space-separated command-line option should be a new element of the array. See examples. Command-line options with multiple elements must use multiple lines in the array, one for each element. Default: |
group string | Name of the group that should own the filesystem object, as would be fed to chown. When left unspecified, it uses the current group of the current user unless you are root, in which case it can preserve the previous ownership. |
include list / elements=string added in ansible-core 2.11 | |
io_buffer_size integer added in ansible-core 2.12 | Size of the volatile memory buffer that is used for extracting files from the archive in bytes. Default: |
keep_newer boolean | Do not replace existing files that are newer than files from the archive. Choices:
|
list_files boolean | If set to True, return the list of files that are contained in the tarball. Choices:
|
mode any | The permissions the resulting filesystem object should have. For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must give Ansible enough information to parse them correctly. For consistent results, quote octal numbers (for example, Giving Ansible a number without following either of these rules will end up with a decimal number which will have unexpected results. As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, If If Specifying |
owner string | Name of the user that should own the filesystem object, as would be fed to chown. When left unspecified, it uses the current user unless you are root, in which case it can preserve the previous ownership. Specifying a numeric username will be assumed to be a user ID and not a username. Avoid numeric usernames to avoid this confusion. |
remote_src boolean | Set to This option is mutually exclusive with Choices:
|
selevel string | The level part of the SELinux filesystem object context. This is the MLS/MCS attribute, sometimes known as the When set to |
serole string | The role part of the SELinux filesystem object context. When set to |
setype string | The type part of the SELinux filesystem object context. When set to |
seuser string | The user part of the SELinux filesystem object context. By default it uses the When set to |
src path / required | If If |
unsafe_writes boolean | Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target filesystem object. By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target filesystem objects, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted filesystem objects, which cannot be updated atomically from inside the container and can only be written in an unsafe manner. This option allows Ansible to fall back to unsafe methods of updating filesystem objects when atomic operations fail (however, it doesn’t force Ansible to perform unsafe writes). IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption. Choices:
|
validate_certs boolean | This only applies if using a https URL as the source of the file. This should only set to Prior to 2.2 the code worked as if this was set to Choices:
|
Attributes
Attribute | Support | Description |
---|---|---|
action | Support: full | Indicates this has a corresponding action plugin so some parts of the options can be executed on the controller |
async | Support: none | Supports being used with the |
bypass_host_loop | Support: none | Forces a ‘global’ task that does not execute per host, this bypasses per host templating and serial, throttle and other loop considerations Conditionals will work as if This action will not work normally outside of lockstep strategies |
check_mode | Support: partial Not supported for gzipped tar files. | Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped. |
diff_mode | Support: partial Uses gtar’s | Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode |
platform | Platform: posix | Target OS/families that can be operated against |
safe_file_operations | Support: none | Uses Ansible’s strict file operation functions to ensure proper permissions and avoid data corruption |
vault | Support: full | Can automatically decrypt Ansible vaulted files |
Notes
Note
- Requires
zipinfo
andgtar
/unzip
command on target host. - Requires
zstd
command on target host to expand .tar.zst files. - Can handle .zip files using
unzip
as well as .tar, .tar.gz, .tar.bz2, .tar.xz, and .tar.zst files usinggtar
. - Does not handle .gz files, .bz2 files, .xz, or .zst files that do not contain a .tar archive.
- Existing files/directories in the destination which are not in the archive are not touched. This is the same behavior as a normal archive extraction.
- Existing files/directories in the destination which are not in the archive are ignored for purposes of deciding if the archive should be unpacked or not.
See Also
See also
- community.general.archive
-
Creates a compressed archive of one or more files or trees.
- community.general.iso_extract
-
Extract files from an ISO image.
- community.windows.win_unzip
-
Unzips compressed files and archives on the Windows node.
Examples
- name: Extract foo.tgz into /var/lib/foo ansible.builtin.unarchive: src: foo.tgz dest: /var/lib/foo - name: Unarchive a file that is already on the remote machine ansible.builtin.unarchive: src: /tmp/foo.zip dest: /usr/local/bin remote_src: yes - name: Unarchive a file that needs to be downloaded (added in 2.0) ansible.builtin.unarchive: src: https://example.com/example.zip dest: /usr/local/bin remote_src: yes - name: Unarchive a file with extra options ansible.builtin.unarchive: src: /tmp/foo.zip dest: /usr/local/bin extra_opts: - --transform - s/^xxx/yyy/
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key | Description |
---|---|
dest string | Path to the destination directory. Returned: always Sample: |
files list / elements=string | List of all the files in the archive. Returned: When Sample: |
gid integer | Numerical ID of the group that owns the destination directory. Returned: always Sample: |
group string | Name of the group that owns the destination directory. Returned: always Sample: |
handler string | Archive software handler used to extract and decompress the archive. Returned: always Sample: |
mode string | String that represents the octal permissions of the destination directory. Returned: always Sample: |
owner string | Name of the user that owns the destination directory. Returned: always Sample: |
size integer | The size of destination directory in bytes. Does not include the size of files or subdirectories contained within. Returned: always Sample: |
src string | The source archive’s path. If Returned: always Sample: |
state string | State of the destination. Effectively always “directory”. Returned: always Sample: |
uid integer | Numerical ID of the user that owns the destination directory. Returned: always Sample: |
Collection links
© 2012–2018 Michael DeHaan
© 2018–2024 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html