FreeKB - Ansible unarchive module (tar zip bzip2 gzip)
Ansible - unarchive module (tar zip bzip2 gzip)

The unarchive module is used to extract an archive, such as a .zip file or a tar archive. By default, unarchive will extract the archive on your control node (that's your Ansible server) to a directory on the managed node (e.g. target system). Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server).

The ansible-doc unarchive command can be used to show the Ansible documention on the unarchive module.

In this example, foo.zip on the control node (that's your Ansible server) will be extracted to the /tmp directory on the managed node (e.g. target system). 

NOTE

The dest directory must exist on the managed node. Use the file module to create the dest directory if it does not exist.

The unzip package must exists on the managed node. Use the yum module to install the unzip package.

- name: extract /path/to/foo.zip
  unarchive:
    src: /path/to/foo.zip
    dest: /tmp

 


remote_src: yes is used to extract an archive on the managed node to a directory on the managed node. In this example, foo.zip on the managed node will be extracted to the /tmp directory on the managed node.

- name: extract /path/to/foo.zip
  unarchive:
    src: /path/to/foo.zip
    dest: /tmp
    remote_src: yes

 

The register parameter can be used to store the result of the unarchive in a variable. In this example, the results will be stored in the 'out' variable.

- name: extract /path/to/foo.zip
  unarchive:
    src: /path/to/foo.zip
    dest: /tmp
  register: out

 

The debug module can be used to print the 'out' variable.

- name: "print the 'out' variable"
  debug: 
    var: out

 

Something like this should be returned. When "changed" is "false", this means the file was not extracted.

ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "dest": "/tmp", 
        "failed": false, 
        "gid": 0, 
        "group": "root", 
        "handler": "ZipArchive", 
        "mode": "01777", 
        "owner": "root", 
        "secontext": "system_u:object_r:tmp_t:s0", 
        "size": 8192, 
        "src": "/tmp/foo.zip", 
        "state": "directory", 
        "uid": 0
    }
}

 

When "changed" is "false", this means the file was extracted.

changed: [server1.example.com] => {
    "msg": {
        "changed": true, 
        "dest": "/tmp", 
        "diff": {
        }
        "extract_results": {
            "cmd": [
                "/bin/unzip", 
                "-o", 
                "/tmp/foo.zip", 
                "-d", 
                "/tmp"
            ], 
            "err": "", 
            "out": "Archive:  /tmp/foo.zip\n  inflating: /tmp/path/to/foo.txt  \n  inflating: /tmp/path/to/bar.txt
        }
        "failed": false, 
        "gid": 0, 
        "group": "root", 
        "handler": "ZipArchive", 
        "mode": "01777", 
        "owner": "root", 
        "secontext": "system_u:object_r:tmp_t:s0", 
        "size": 8192, 
        "src": "/tmp/foo.zip", 
        "state": "directory", 
        "uid": 0
    }
}

 


Optional Parameters

The unarchive module accepts the following parameters.

  • owner (e.g. owner: "root")
  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")
  • seuser - SELinux user (e.g. seuser: "unconfined_u")
  • serole - SELinux role (e.g. serole: "object_r")
  • setype - SELinux type (e.g. setype: "httpd_sys_content_t")
  • selevel - SELinux level (e.g. selevel: "s0")
  • remote_src - Do something on the managed node (e.g. remote_src: "yes")

 

Here is an example of how these parameters would be used.

- name: "extract /path/to/foo.zip"
  unarchive:
    src: "/path/to/foo.zip"
    dest: "/tmp"
    owner: "foo"
    group: "foo"
    mode: "0644"
    seuser: "unconfined_u"
    serole: "object_r"
    setype: "httpd_sys_content_r"
    selevel: "s0"
    remote_src: "yes"

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 40b8a in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |