Validate the Integrity of a File Backup using Ansible

Introduction

Running nightly file backups is a common task for administrators. How do we know the file was copied successfully with no errors? In this post, we will set up an ansible script and it will run a file integrity check using MD5 on both the source and the destination files to verify it was not corrupted during the copy process. In this process the Ansible server is assumed to be a separate server from both the source server and the designation server.

Specifically, we will tell Ansible to execute a bash script on the source and destination servers, gather the results and store them in a temp text file, then it will output the text file to the body of an email and send it to interested parties for review.

Create the Ansible Script

Add comments to the head of the script. I like to include an example of the command, so that it can be easily copied to the command line.

Add the variables to the script. All ansible scripts start with three dashes. Also note the Ansible is very sensitive to the placement of the columns. The names, hosts, and tasks columns must be lined up exact or the script will not execute.

Add the tasks that must be executed.

Finally we will send an email to interested parties.

Build the Bash Script

Remember, in Ansible, it will execute the code on all servers simultaneously. So, we don’t know what server’s results will be returned to Ansible first. That is why we need the server hostname.

Create the headers.

Create the variables.

Execute the comamnds, to gather the needed data.

Output the results. Remember these results will be returned to Ansible.

This is my own method for verifying files were copied correctly. I hope you find it useful.

Related Posts