Appendix C. Troubleshooting and Debugging

When developing Puppet types and providers, it’s not uncommon to run into bugs that are hard to trace and debug. There are a few options in Puppet that will simplify the troubleshooting process. The option --trace will provide the Ruby stacktrace when Puppet execution fails. This will give you a file and line number to provide additional insight into the problem at hand.

The Puppet.debug('message') method can be used in conjunction with the Puppet --debug flag to output troubleshooting messages to the console. This is helpful to output variables and ensure that certain methods are invoked as expected. But it’s often far more helpful to dive directly into a debugger. The ruby-debug gem gives you the ability to interactively troubleshoot by breaking at a specific line in the code:

require 'ruby-debug'; debugger

The post_mortem option allows ruby-debug to enter into an interactive session when an exception occurs:

require 'ruby-debug';
Debugger.start(:post_mortem => true)

Let’s update our custom_package resource and add a breakpoint inside the type’s exists? method:

def exists?
  require 'ruby-debug'; debugger
  ...
end

Now when we manage a custom_package, the debugger drops us into an interactive session when the method exists? is invoked:

$ puppet apply -e "custom_package { 'bash': ensure=>present }"
/etc/puppetlabs/puppet/modules/custom_packages/lib/puppet/provider/custom_package/yum.rb:32
@property_hash[:ensure] == :present
(rdb:1)

In this interactive ...

Get Puppet Types and Providers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.