Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Clean up/purge unresponsive nodes that count against your license count. Puppet Enterprise 2016.1 2016.2 2016.
require "httparty"
# Unresponsive node cleanup for Puppet Enterprise 2016.2
#
# When our system scales down, we often have many unresponsive nodes. These
# nodes count towards our overall license count. Future scaling events will
# keep stacking these unused licenses. This is a simple method to purge the
# unresponsive nodes.
#
# This script can be run periodically on the puppet master to clean up
# unresponsive nodes.
#
# To access the puppet api from the command line, you'll need to generate an
# access token. In puppet console, create a user who is part of the admin
# group. Follow this to create a token.
# https://docs.puppet.com/pe/latest/rbac_token_auth.html#setting-a-token-specific-lifetime
# $ puppet-access login --lifetime 0
# Location for Ubuntu 14.04.5 LTS
token = `cat /home/ubuntu/.puppetlabs/token`
# query used to determine unresponive nodes (nodes that haven't reported in
# over an hour. using 2 hours.)
query = ["<", "report_timestamp", (Time.now - 7200).utc.iso8601].to_json
url = "http://localhost:8080/pdb/query/v4/nodes?query=#{query}"
#
response = HTTParty.get(url, headers: {"X-Authentication" => token}, verify: false)
# unresponsvie node certs
certs = response.collect{|x| x["certname"]}
# https://docs.puppet.com/pe/latest/node_deactivation.html
puts "purging old node certs..."
result = false
certs.each do |cert|
# returns true if successfully executed
result = system( "sudo puppet node purge #{cert}")
end
# if we purged nodes, we'll need to run puppet agent and restart puppet server
if result
puts "run puppet agent..."
system( "sudo puppet agent -t" )
puts "restart puppet server...(this may take a few minutes)"
system( "sudo service pe-puppetserver restart" )
else
puts "no unresponsive nodes"
end
puts "done!"
Owner

todgru commented Feb 7, 2017 edited

An older version of this, for earlier versions of Puppet 3, can be found here https://gist.github.com/todgru/1caf3333021c9b40c36e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment