DEV Community

Martez Reed for puppet

Posted on • Originally published at Medium on

Collecting System Information Using Puppet Bolt

As a system administrator collecting information about what version of software is installed or what’s the setting in a particular file across dozens or hundreds of systems is a fairly common task. The task itself is fairly common but the manner in which it is accomplished can be widely different even from script to script in your environment.

Puppet Bolt provides an excellent scaffolding or framework to allow the focus to be on fetching the valuable information from the systems and not the logistics of how to find and connect to them.

In this blog post we’ll be creating a Puppet Bolt task to check if Docker is installed and running on a number of machines.

Initialize a New Bolt Project

Ensure that the latest version of Puppet Bolt is installed before getting started.

Puppet Bolt utilizes Project directories as launching points for running Bolt operations. In this post we’ll create a Bolt project called systeminfo that will contain our tasks for gather system information. The following command will create a directory named systeminfo in the current working directory.

bolt project init systeminfo
Enter fullscreen mode Exit fullscreen mode

There should now be a bolt.yaml file in the systeminfo directory. In the systeminfo project directory create a file named bolt-project.yaml with the following content.

# bolt-project.yaml
name: systeminfo
Enter fullscreen mode Exit fullscreen mode

Create a Systeminfo Task

With the bolt project instantiated we are now ready to create our Bolt tasks for fetching the information from our systems.

Create a Tasks directory

Create a tasks directory in the project directory.

mkdir tasks
Enter fullscreen mode Exit fullscreen mode

Change directory to the newly created tasks directory.

cd tasks
Enter fullscreen mode Exit fullscreen mode

We are now ready to create our script that checks the system for Docker and to see if it is running. Create a file named checkdocker.sh in the current directory and add the following content.

#!/bin/bash
output=$(docker ps 2>&1)
retVal=$?
if [$retVal -ne 0]; then
    echo "Docker is not present or running"
else
    echo "Docker is present and running"
fi
Enter fullscreen mode Exit fullscreen mode

Now that we have the shell script for checking a Docker installation we just need to create a metadata file for our task that provides information about the task. The name of the task file needs to be mirror the shell script so it’ll be name checkdocker.json.

{
  "description": "Check if docker is install and running",
  "input_method": "stdin",
  "parameters": {}
}
Enter fullscreen mode Exit fullscreen mode

We can now verify that Bolt recognizes our new task by running the following command that lists registered Bolt tasks.

bolt task show
Enter fullscreen mode Exit fullscreen mode

If the task registers properly the output should include a systeminfo::checkdocker entry.

facts Gather system facts
package Manage and inspect the state of packages
pkcs7::secret_createkeys Create a key pair
pkcs7::secret_decrypt Encrypt sensitive data with pkcs7
pkcs7::secret_encrypt Encrypt sensitive data with pkcs7
puppet_agent::install Install the Puppet agent package
puppet_agent::version Get the version of the Puppet agent package installed. Returns nothing if none present.
puppet_conf Inspect puppet agent configuration settings
reboot Reboots a machine
reboot::last_boot_time Gets the last boot time of a Linux or Windows system
service Manage and inspect the state of services
**systeminfo::checkdocker Check if docker is install and running**
terraform::apply Apply an HCL manifest
terraform::destroy Destroy resources managed with Terraform
terraform::initialize Initialize a Terraform project directory
terraform::output JSON representation of Terraform outputs
Enter fullscreen mode Exit fullscreen mode

Run the systeminfo::checkdocker task

With the task registered we are ready to run the task by running the following command. In this example we’ll be running the task against multiple nodes in our environment.

bolt task run systeminfo::checkdocker --no-host-key-check --user root --targets 10.0.0.25,10.0.0.252,10.0.0.224,10.0.0.80
Enter fullscreen mode Exit fullscreen mode

If the task ran successfully it should have generated output similar to that displayed below.

bolt task run systeminfo::checkdocker --user root --targets 10.0.0.25,10.0.0.252,10.0.0.224,10.0.0.80
Started on 10.0.0.224...
Started on 10.0.0.252...
Started on 10.0.0.25...
Started on 10.0.0.80...
Finished on 10.0.0.25:
  Docker is present and running
Finished on 10.0.0.252:
  Docker is not present or running
Finished on 10.0.0.80:
  Docker is not present or running
Finished on 10.0.0.224:
  Docker is not present or running
Successful on 4 targets: 10.0.0.25,10.0.0.252,10.0.0.224,10.0.0.80
Ran on 4 targets in 4.62 sec
Enter fullscreen mode Exit fullscreen mode

The output from the task can be made much fancier but even in a few minutes we were able to create a Bolt task that queries our systems and lets us know if Docker is running on that system. This is a fairly simple example of what can be done using Puppet Bolt but the possibilities are endless for collecting valuable system information.

Top comments (0)