0. Related modules
- archive [OfficialDoc]
- $ ansible-doc archive
- unarchive [OfficialDoc]
- $ ansible-doc unarchive
1. Basics
Those modules do their job by utilizing tar, gzip, bzip2, lzma, and zip packages. Basically, it does what a command tool "tar" usually does.
- "archive" module is maintained by the Ansible Community and not guaranteed to be backward compatible.
- "archive" does not copy the source file from the control node to managed nodes.
- "unarchive" module is maintained by the Ansible Core team and not guaranteed to have a backwards compatible interface.
- "unarchive" unpacks an archive after (optionally) copying it from the control node.
- "unarchive" acts as the "copy" module with compression.
2. Examples
2.1 archive /etc/selinux/ and download the tarball file
---
- hosts: all
become: yes
vars:
- tarfile: /tmp/etc-selinux.tgz
tasks:
- name: tar test
archive:
path: /etc/selinux
dest: "{{ tarfile }}"
- name: get the file
fetch:
src: "{{ tarfile }}"
dest: ./data
- name: delete file
file:
path: "{{ tarfile }}"
state: absent
2.2 copy a tarball from the control node to target managed nodes and unpack it
---
- hosts: all
become: yes
tasks:
- name: Ensure httpd is installed
yum:
name: httpd
state: latest
- name: unarchive test
unarchive:
src: html.tgz
dest: /var/www/html
- name: Ensure httpd is restarted
service:
name: httpd
state: restarted
enabled: yes
- name: Test by curl
command: curl 127.0.0.1/test.html
register: r
- debug:
var: r.stdout
No comments:
Post a Comment