Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Created January 26, 2018 00:36
Show Gist options
  • Save natemccurdy/e5237015ffc63f67dc44ccc19ebd380a to your computer and use it in GitHub Desktop.
Save natemccurdy/e5237015ffc63f67dc44ccc19ebd380a to your computer and use it in GitHub Desktop.
Ruby script to get the names of compile masters from 'puppet infra'
#!/opt/puppetlabs/puppet/bin/ruby
# This script returns an array of compile masters in JSON based on the output of `puppet infra status`.
# Useful for feeding into a task or a plan.
require 'facter'
require 'json'
require 'open3'
def puppet_infra_status
stdout, _stderr, _status = Open3.capture3('/opt/puppetlabs/puppet/bin/puppet infra status --format json')
stdout.strip
end
services = JSON.parse(puppet_infra_status)
compile_masters = []
services.each do |service|
compile_masters << service['server'] if service['service'] == 'pe-master' && service['server'] != Facter.value('fqdn')
end
puts compile_masters.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment