Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Last active November 6, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessereynolds/991a331acd62de2cbf91344051101c0a to your computer and use it in GitHub Desktop.
Save jessereynolds/991a331acd62de2cbf91344051101c0a to your computer and use it in GitHub Desktop.
read a puppet report yaml file with ruby

Reading a Puppet report with Ruby

Puppet writes a report file in the yaml format after every agent run. The yaml format includes a yaml document tag, or 'taguri', which indicates the ruby object type. This is fine if you have the puppet gem loaded however if you want to use pure ruby to read in the report you need to use a lower level interface of ruby's yaml library (psych) so that the taguri can be removed. In this way we can effectively ignore the ruby object type encoded in the report yaml file.

The taguri can be seen in the first line of the report yaml file:

--- !ruby/object:Puppet::Transaction::Report
host: pe-20198
time: '2020-10-08T00:12:25.779388944+00:00'
configuration_version: 1602115959
transaction_uuid: c3ec7329-736d-46e7-8445-025f7962f170
report_format: 10
puppet_version: 6.17.0

The two ruby files on this gist provide with-puppet, and without-puppet mechanisms for reading the puppet report into ruby so it can then be manipulated, reported on, etc.

#!/usr/bin/env ruby
require 'yaml'
report_file = '/opt/puppetlabs/puppet/cache/state/last_run_report.yaml'
report = YAML.parse(File.read(report_file))
# nuke the yaml document tag that sets the ruby object
report.root.tag = ''
report_hash = report.root.to_ruby
#puts report_hash.keys
puts "tags for resource: 'File[/opt/puppetlabs/bin/puppet-enterprise-uninstaller]'"
puts report_hash['resource_statuses']['File[/opt/puppetlabs/bin/puppet-enterprise-uninstaller]']['tags']
#!/usr/bin/env /opt/puppetlabs/puppet/bin/ruby
require 'yaml'
require 'puppet'
report_file = '/opt/puppetlabs/puppet/cache/state/last_run_report.yaml'
report = YAML.load_file(report_file)
puts "tags for resource: 'File[/opt/puppetlabs/bin/puppet-enterprise-uninstaller]'"
puts report.resource_statuses['File[/opt/puppetlabs/bin/puppet-enterprise-uninstaller]'].tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment