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

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The archive module is used to create a compressed archive of one or more files or of a directory on a managed node (e.g. the target system). This is slightly different from the tar command. The tar command creates a tar archive of a directory, but the tar archive is not used to archive files. The archive module is a bit more robust in that it can archive both files and directories. 

By default, a gzip compressed tar archive will be created. Even if dest contains something like foo.tar.bz2 or foo.zip, the destination file will actually be gzip compressed.

- name: create foo.tar.gz
  archive:
    path: /path/to/directory
    dest: /path/to/foo.tar.gz

 

The format parameter can be used to specify the type of compression to use. In this example, a directory is compressed into a zip archive named foo.txt.zip.

- name: zip foo.txt
  archive:
    path: /path/to/directory
    dest: /path/to/foo.txt.zip
    format: zip

 

In this example, bzip2 archive foo.tar.bz2 will be created.

- name: bzip compress foo.tar
  archive:
    path: /path/to/directory
    dest: /path/to/foo.tar.bz2
    format: bz2

 

Be aware that if the target directory (path) does not exist, something like this will be returned.

TASK [create foo.tar.gz]
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "path /tmp/foo.tar.gz does not exist", "path": "/tmp/foo.tar.gz"}

 

For this reason, you may want to first use the find module to determine if the target directory exists, and then only run the archive module when the target directory exists.

- find:
    paths: /tmp
    patterns: foo
    file_type: directory
    recurse: false
  register: out

- name: create foo.tar.gz
  archive:
    path: /tmp/foo
    dest: /path/to/foo.tar.gz
  when:
    - out.matched == 1

 

Multiple files and directories can be appended to the archive.

- name: create foo.tar.gz
  archive:
    path: 
    - /path/to/dir1
    - /path/to/dir2
    dest: /path/to/foo.tar.gz

 

The register parameter can be used to store the output. In this example, the output is stored in the "out" variable.

- name: create foo.tar.gz
  archive:
    path: /path/to/directory
    dest: /path/to/foo.tar.gz
  register: out

 

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

- debug:
    var: out

 

If the archive is not created, something like this should be returned.

TASK [debug]
ok: [server1.example.com] => {
    "out": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}

 

If the archive is created, something like this should be returned.

TASK [debug]
ok: [server1.example.com] => {
    "out": {
        "archived": [
            "/tmp/foo.txt",
            "/tmp/bar.txt",
            "/tmp/files.tar.gz",
            "/tmp/ansible_archive_payload_rbz5HT/ansible_archive_payload.zip"
        ],
        "arcroot": "//",
        "changed": true,
        "dest": "/tmp/files.tar.gz",
        "expanded_exclude_paths": [],
        "expanded_paths": [
            "/tmp"
        ],
        "failed": false,
        "gid": 0,
        "group": "root",
        "missing": [],
        "mode": "0644",
        "owner": "root",
        "secontext": "unconfined_u:object_r:user_tmp_t:s0",
        "size": 84348,
        "state": "file",
        "uid": 0
    }
}

 


Append

The archive module does not have an option to append to an archive that already exists. In this example, the the find module is used to find files matching foo below the /tmp directory, and then the foo array contains each matched file. Each file in the foo array will be appended to the archive.

- find:
    paths: /tmp
    patterns: foo
  register: out

- name: create foo.tar.gz
  archive:
    path: "{{ foo }}"
    dest: /path/to/foo.tar.gz
  vars:
    foo: "{{ out.files | map(attribute='path') | list }}"

 



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 2d1b1 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   |