W3cubDocs

/Ansible

ansible.builtin.unarchive – 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 FQCN for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

New in version 1.4: of ansible.builtin

Synopsis

  • The 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 Choices/Defaults Comments
attributes
string
added in 2.3 of ansible.builtin
The attributes the resulting file or directory 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 = operator is assumed as default, otherwise + or - operators need to be included in the string.

aliases: attr
copy
boolean
    Choices:
  • no
  • yes
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 remote_src.
This option is mutually exclusive with remote_src.
creates
path
added in 1.6 of ansible.builtin
If the specified absolute path (file or directory) already exists, this step will not be run.
decrypt
boolean
added in 2.4 of ansible.builtin
    Choices:
  • no
  • yes
This option controls the autodecryption of source files using vault.
dest
path / required
Remote absolute path where the archive should be unpacked.
exclude
list / elements=string
added in 2.1 of ansible.builtin
Default:
[]
List the directory and file entries that you would like to exclude from the unarchive action.
Mutually exclusive with include.
extra_opts
list / elements=string
added in 2.1 of ansible.builtin
Default:
""
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.
group
string
Name of the group that should own the file/directory, as would be fed to chown.
include
list / elements=string
added in 2.11 of ansible.builtin
Default:
[]
List of directory and file entries that you would like to extract from the archive. Only files listed here will be extracted.
Mutually exclusive with exclude.
keep_newer
boolean
added in 2.1 of ansible.builtin
    Choices:
  • no
  • yes
Do not replace existing files that are newer than files from the archive.
list_files
boolean
added in 2.0 of ansible.builtin
    Choices:
  • no
  • yes
If set to True, return the list of files that are contained in the tarball.
mode
raw
The permissions the resulting file or directory should have.
For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.
Giving Ansible a number without following one 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, u+rwx or u=rw,g=r,o=r).
If mode is not specified and the destination file does not exist, the default umask on the system will be used when setting the mode for the newly created file.
If mode is not specified and the destination file does exist, the mode of the existing file will be used.
Specifying mode is the best way to ensure files are created with the correct permissions. See CVE-2020-1736 for further details.
owner
string
Name of the user that should own the file/directory, as would be fed to chown.
remote_src
boolean
added in 2.2 of ansible.builtin
    Choices:
  • no
  • yes
Set to yes to indicate the archived file is already on the remote system and not local to the Ansible controller.
This option is mutually exclusive with copy.
selevel
string
The level part of the SELinux file context.
This is the MLS/MCS attribute, sometimes known as the range.
When set to _default, it will use the level portion of the policy if available.
serole
string
The role part of the SELinux file context.
When set to _default, it will use the role portion of the policy if available.
setype
string
The type part of the SELinux file context.
When set to _default, it will use the type portion of the policy if available.
seuser
string
The user part of the SELinux file context.
By default it uses the system policy, where applicable.
When set to _default, it will use the user portion of the policy if available.
src
path / required
If remote_src=no (default), local path to archive file to copy to the target server; can be absolute or relative. If remote_src=yes, path on the target server to existing archive file to unpack.
If remote_src=yes and src contains ://, the remote machine will download the file from the URL first. (version_added 2.0). This is only for simple cases, for full download support use the ansible.builtin.get_url module.
unsafe_writes
boolean
added in 2.2 of ansible.builtin
    Choices:
  • no
  • yes
Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file.
By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, 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 files 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.
validate_certs
boolean
added in 2.2 of ansible.builtin
    Choices:
  • no
  • yes
This only applies if using a https URL as the source of the file.
This should only set to no used on personally controlled sites using self-signed certificate.
Prior to 2.2 the code worked as if this was set to yes.

Notes

Note

  • Requires zipinfo and gtar/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 using gtar.
  • Does not handle .gz files, .bz2 files, .xz, or .zst files that do not contain a .tar archive.
  • Uses gtar’s --diff arg to calculate if changed or not. If this arg is not supported, it will always unpack the 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.
  • Supports check_mode.

See Also

See also

community.general.archive

The official documentation on the community.general.archive module.

community.general.iso_extract

The official documentation on the community.general.iso_extract module.

community.windows.win_unzip

The official documentation on the community.windows.win_unzip module.

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 Returned Description
dest
string
always
Path to the destination directory.

Sample:
/opt/software
files
list / elements=string
When list_files is True
List of all the files in the archive.

Sample:
["file1", "file2"]
gid
integer
always
Numerical ID of the group that owns the destination directory.

Sample:
1000
group
string
always
Name of the group that owns the destination directory.

Sample:
librarians
handler
string
always
Archive software handler used to extract and decompress the archive.

Sample:
TgzArchive
mode
string
always
String that represents the octal permissions of the destination directory.

Sample:
0755
owner
string
always
Name of the user that owns the destination directory.

Sample:
paul
size
integer
always
The size of destination directory in bytes. Does not include the size of files or subdirectories contained within.

Sample:
36
src
string
always
The source archive's path.
If src was a remote web URL, or from the local ansible controller, this shows the temporary location where the download was stored.

Sample:
/home/paul/test.tar.gz
state
string
always
State of the destination. Effectively always "directory".

Sample:
directory
uid
integer
always
Numerical ID of the user that owns the destination directory.

Sample:
1000


Authors

  • Michael DeHaan

© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html