Skip to main content

Using Ansible Modules to Manage Archiving in Your Environment

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:15:00

Difficulty

Intermediate

Dealing with compressed files is one of the less glamorous things that a System Administrator is called upon to do. Fortunately, Ansible has modules to help work with archived files as well. This lab will help cement those skills.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Ansible Modules to Manage Archiving in Your Environment

Introduction

Dealing with compressed files is one of the less glamorous things that a System Administrator is called upon to do. Fortunately, Ansible has modules to help work with archived files as well. This lab will help cement those skills.

The Scenario

We've gotten a ticket from the DBA team that says they need a couple of tarballs unpacked on the DB server. One is on the Ansible host, /root/file.tar.gz, while the other is /tmp/devstuff.tar.gz on the DB Server. Both need to be extracted into /opt on the DB Server.

We have another ticket requesting that we compress /var/log/messages on the DB server and put the result in /tmp on the Ansible host. We've decided to do both tasks at once.

We need to write a playbook that will unpack all of the mentioned tarballs, compress /var/log/messages on dbserver1, and then get that tarball into /tmp on the Ansible Host.

Logging In

Use the credentials provided on the hands-on lab page to get into Server1 to begin with. Since we need root privileges, let's just run sudo -i right off and become root.

Unpack the /root/file.tar.gz Tarball into /opt on DBServer1

Let's create a playbook:

vim archive.yml

We'll put the first task in, which will be what extracts a local archive. It should look similar to this when we're done:

---
- name: Copy data dev stuff and inflate data and dev stuff
  hosts: dbservers
  become: yes

  tasks:
  - unarchive:
     src: /root/file.tar.gz
     dest: /opt/

Unpack the devstuff Tarball into /opt on dbserver1

This part will extract a remote archive. It should look similar to this when we're finished:

  - unarchive:
     src: /tmp/devstuff.tar.gz
     dest: /opt/
     remote_src: yes

Compress and Copy /var/log/messages from dbserver1 to the Ansible Host

The next play in the book creates an archive on dbserver1, then retrieve it. When we're done, the section should look like this:

  - archive:
     path: /var/log/messages
     dest: /tmp/messages.tar.gz

  - fetch:
     src: /tmp/messages.tar.gz
     dest: /tmp/

Run the Playbook

As long as we don't have any typos in our playbook, we should see all of the successful task output march across the screen.

Conclusion

We've done it. There's a playbook now that will extract remote archives, then create others that we fetch to the Ansible host. That's exactly what we wanted. Congratulations!