Day: April 16, 2024

  • Validate the Integrity of a File Backup using Ansible

    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

    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.

  • Update, Reboot, & Get the Health of Remote Servers

    Update, Reboot, & Get the Health of Remote Servers

    Introduction

    Ansible was designed to remotely manage multiple Linux servers simultaneously. Scripts can be used for many common tasks like updating, rebooting, or to the check health of your Linux servers . Ansible scripts are very format sensitive, so be sure that all columns match up, as below, or it will not run.

    Notice that some scripts call sudo and you need the ‘-K’ switch in the command. You can tell if the script calls sudo by the line ‘become: yes’.

    Update & Reboot all Servers

    This will updated, upgrade, remove unnecessary files, and clear the local repository cache. Finally, it will send an email when completed. It will run against all servers, in the ini file, listed in the group called ‘all_servers’.

    Reboot Specific Servers

    Check the Health of the Remote Servers

    The check health script gathers basic information about the remote servers. Is the hard disk drive full? Does the server need a reboot? How long has the server been up?

    The ansible script calls a bash script, that is then executed on all remote hosts. The results are returned and printed to a text file. An email copies the contents of the text file to the body of the email and results are emailed. Be sure to save the inventory.ini, bash scripts, and the ansible scripts in the same directory.

  • Managing User Accounts with Ansible

    Managing User Accounts with Ansible

    Introduction

    Ansible is a program designed to manage Linux servers. See blog post on setting up Ansible, creating a script, creating an inventory file, and calling a script from the command line. You can call a single server or multiple servers by separating then with a colon on the ‘hosts’ line. If a large number of hosts needs to be called, create a group in the inventory.ini file and call the group on the ‘hosts’ line.

    Create a New User’s Account on Multiple Servers

    When a new hire comes onboard, rather than log into each server directly and manually create their accounts, run this script and it will create the accounts on all servers simultaneously.

    Get a List of Servers that have a Specific User’s Account

    If a user leaves the company, you can never be sure what Linux servers they were given access to, so I run this to get a list of what servers they have accounts. It outputs the results to a text file, which the results can be easily be viewed. This script calls a host group called “all_servers” in the inventory.ini file.

    Remove A User’s Account

    Once you have identified which servers the user has an account on, add the username to the script and specify the target hosts. As before, you can list multiple servers, separated by a colon, or create a group in the *ini file and then add the group name to the ‘hosts’ line.

    Push a Key to Multiple Servers

    It is recommended that users login using public and private keys. It is easy to push a users public key to multiple servers at same time. Replace the ‘key’, with the user’s actual public key surrounded by double quotes.

    The authorized key command handles creating the directors and setting permissions on all files.

    Change a User’s Password

    Perhaps a user forgot their password, or they have left the company. You may need to change their password. Again, modify the ‘hosts’ line as necessary, with a single, multiple, or a group of servers.

  • Make a Batch Script to Map Your Drives

    Make a Batch Script to Map Your Drives

    Introduction

    Although Microsoft calls it mapping a drive, in truth, you are just mapping the location of a specific local or remote folder. You are not technically mapping an entire hard drive.

    There are several reasons it is worthwhile to write a batch script that can auto connect your frequently used folders. A common reason is that enterprise users frequently get their network folders disconnected. Problems arise from VPN disconnects, power fluctuations, or other concerns.

    We can make a batch script and save it on their desktop (or in their startup folder) to quickly get their folders re-connected. The user’s just need to double click the script file and it will quickly restore their network folders.

    Map the folders (aka. drives)

    Let’s create a script called ‘mapdrives.bat’ using notepad.

    Add information to the headers. Anything with REM or :: will not be executed.

    Use ‘echo off’, at the top of the script. This tells the script to not display the commands or results to the screen, as they are executed. Use ‘echo.’ to print a blank line, and use just ‘echo’ to print data to the screen.

    Comment your code using descriptors and use ‘net use’ to map the folder paths. Be sure to put quotes around the file path if there is a space somewhere in the path.

    Let’s create a short delay and exit the program. We can add the command ‘pause’ and it will hold the command prompt open until a key is struck or use ‘ping’ and it will wait three seconds and exit automatically.

    Finally, save the file with ‘.bat’ file extension and then double click the file to execute it.

    Open File Explorer and you will see your newly mapped folders.