Using puppet custom facts

Posted on Posted in DevOps & Networking

Puppet custom facts can be very useful if you want to take action based on a special state of your server.

I had to use it a few times and I will demonstrate a couple of use cases I encountered:

  1. Application installation that has to be treated differently if it was already installed vs. a new deployment
  2. Checking the current application version before taking action

So lets dive into it, in the first case we have a custom service that I want to treat differently if its a fresh installation or an upgrade.

If its a fresh installation I want to install and start the application, otherwise if it was already installed I need to perform an action that will reduce traffic towards this server (in my case stop a heartbeat action), wait and only then upgrade the application.

Lets see how its done.

First of all you have to make sure the puppet.conf configuration on the puppet agent has pluginsync = true
It should be enabled by default in newer versions, but you can verify it using the following command:

If pluginsync is not enabled you need to add pluginsync = true to the puppet agent’s puppet.conf

The second step is actually creating the custom facts, in my case we decided on a special puppet module called custom_facts that will contain all of these custom facts.
This module doesn’t contain a manifests directory or any .pp files, this is the folder structure:

This is myapplication.rb in my case, I just check if the application’s app.exe exists on the server in the correct directory path and return a boolean value

Now this is how I use this fact in my puppet app module (this is a different module called app, not custom_facts)

The second use case checks the installed RPM version of a package, which is later used in a module that determines what to do based on that version.

Now this is how I use this fact in the mcollective module section

Leave a Reply

Your email address will not be published. Required fields are marked *