Using ansible to deploy my checkin scripts

https://github.com/halemiles/checkin-memory

Keeping your checkin scripts up to date

If you havn’t checked out checkin-memory, you should here so you get some context for this post.

Keeping track of the health and status of servers is a critical task for any operations team (regardless of the project size). One way to do this is to use check-in scripts which run periodically on each server. The scripts report back to a service. However, deploying these scripts and configuring them to run on a regular basis can be a challenge. I’ve decided to use Anisble to manage this for me.

Queue Ansible

FYI: Before we dive into my experiences with Ansible, I want to clarify that I have no affiliation with Ansible or its development team. My choice to use Ansible is purely a personal one, driven by my own needs and preferences.

It is however a powerful automation tool has helped me deploy my script to my servers with ease. In this post, I’ll show you how I use Ansible to deploy my checkin scripts my checkin-memory project.

Once the script has been deployed to my servers, I get ansible to set up my cron jobs so that each server is periodically posting to my checkin service.

Let’s deploy your scripts

I am going to use a generic checkin script for most of my servers. I want to know the server name (not always the same as hostname), the IP address, the last time it “Checked In” and some information about services running. The goal here is to determine what servers are up and if any services are not running on them. Then I know which servers I need to log in and fix.

To get started, you’ll need to create an Ansible playbook that will copy the check-in scripts to the appropriate location on each server. Here’s an example playbook:


---
- name: Deploy check-in scripts
  hosts: servers
  become: true
  tasks:
    - name: Copy check-in scripts
      copy:
        src: /path/to/checkin-memory/scripts/
        dest: /opt/checkin-memory/scripts/

This playbook will copy the check-in scripts from the local directory /local/path/to/checkin-memory/scripts/ to the remote directory /remote/path/to/checkin-memory/scripts/ on all hosts defined in the hosts section of the playbook.

Set up cronjobs

Once the scripts have been deployed, we need to ensure that they run on a regular basis (I use every 2 minutes). We can do this by creating a cron job that executes the scripts at a specific time. Here’s an example playbook that creates a cron job that runs the check-in scripts every hour:


---
- name: Create check-in cron job
  hosts: servers
  become: true
  tasks:
    - name: Create cron job
      cron:
        name: "checkin-memory"
        minute: "*/2"
        job: "/remote/path/to/checkin-memory/scripts/checkin.sh"

This part of the playbook deploys the script as a cron job which will run every 2 minutes (or whatever you decide to use). Once the cron job has been deployed give it a bit of time and it should show up in the /device endpoint.

Example

I have an ansible playbook and example script in the checkin-memory deploy folder.

Testing

However, it’s important to note that deploying check-in scripts using Ansible requires careful planning and testing. You’ll need to ensure that the scripts are compatible with each server’s operating system and software stack, and that they can run without causing any adverse effects on the server. Making the scripts as generic as possible helps a lot, however if you do need something more specific then you will probably need to have multiple tasks in your playbook(s) to deploy the relevant scripts to the correct servers.

Summary

In summary, using Ansible to deploy check-in scripts for a project like checkin-memory can be a great way to automate server monitoring and ensure consistent, reliable server check-ins. With careful planning and testing, you can deploy your scripts with little effort.