Puppet Exported Resources

Veeksha A V
Sep 9, 2018 · 2 min read

PUPPET being one of the configuration management tools provides a way where one can export resources such as files, ssh keys etc.. of a node to Puppet DB and then realize or collect it on other nodes.This allows to share the activities on one node to other nodes in the system, thereby helping to resolve dependencies among the components in the system.

Export and Collect a resource in Puppet

If we used Puppet to deploy and manage a software solution and the solution had several components out of which one of them was a database component and other components accessed the database when their respective services were bought up, then in order to resolve the dependency between the database and other components, the database had to be populated with the initial data before bringing up any process successfully in the system. And to resolve this in Puppet, we can use exporting and collection of resources feature of Puppet as stated below.

  • Assuming update of database with data is by running some existing SQL scripts.

<resource-type> { ‘SQL Update’: …… }

  • Store the result of execution of SQL scripts in a file named “.db_status”. If SQL scripts ran successfully without any errors, then file “.db_status” was exported with text “Success” else if SQL scripts failed, then the file “.db_status” was not exported at all.

@@file { “${home_dir}/.db_status”:
content => “Success”
tag => “${::customer}_${::environment}_DB”,
require => <resource-type>[‘SQL Update’],
}

  • Collect the above exported file “.db_status” on other system components. Here tagging ‘.db_status’ file using custom facts based on customer environment is helpful in uniquely identifying database belonging to a setup.

File <<| tag == “${::customer}_${::environment}_DB” |>>

  • Next, check if the collected “.db_status” file on other system components had text “Success” , if so start the remaining services of the system else do not start the services at all assuming database is not updated.

exec { ‘Test DB Initialization’:
command => “cat ${home_dir}/.db_status | grep Success”,
path => “/usr/bin:/bin”,
}

service { ‘<service_name>’:
ensure => ‘running’,
require => Exec[‘Test DB Initialization’],
}

Notes:

  1. One should enable storeconfig in puppet master (puppet.conf), to use exporting and collection of resources. (set storeconfigs=True and storeconfigs_backend=puppetdb in puppet.conf)
  2. Realization of collected resources may not happen instantly , usually it takes two puppet runs on the servers where resources are collected.
  3. Same node can export and collect a resource.

Written by

A Software Engineer with a can do attitude, enjoy learning new things in life.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade