Debugging Puppet functions in the SIMPLE Grid Puppet module

Debugging ruby functions for puppet can be painful since the debug statements inside the functions are not visible in the output of puppet runs. (Note: Functions are executed before resources)

Also, from the framework’s perspective, let’s say if you’re debugging a function in the pre_deploy stage, you would have to comment out code in a few locations to make sure your debugging does not create more errors in other parts of the puppet code i.e. we need to block execution of puppet at the line of code we are debugging to avoid it from proceeding further until we have fixed the issue.

One cool way to do so is described below: Let’s consider that we want to check the value of the meta_info_parent variable in this function:

On line 31, you would then return the value of meta_info_parent as follows:

return meta_info_parent

Now, let’s assume the generate_dns_file_content function call was made from the following puppet manifest:

Right after the function call, you can use the fail() function with your debug message as follows
fail($dns_file_content)

When you run puppet again, it will not execute anything after the fail() function and print the debug message as an error message.

For example, I used the same trick to return a special value from inside the function that I could analyse in the puppet error message:

Info: Retrieving locales
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, {string => - container_fqdn: simple-lc01.cern.ch
  host_fqdn: simple-lc01.cern.ch
  host_ip: 188.184.104.25
  container_ip: 10.10.0.10
  type: compute_element
  execution_id: 0
- container_fqdn: HTCondor-Worker_simple-lc03_2.cern.ch
  host_fqdn: simple-lc03.cern.ch
  host_ip: 188.184.84.189
  container_ip: 10.10.0.12
  type: worker_node
  execution_id: 2
, hash => [{container_fqdn => simple-lc01.cern.ch, host_fqdn => simple-lc01.cern.ch, host_ip => 188.184.104.25, container_ip => 10.10.0.10, type => compute_element, execution_id => 0}, {container_fqdn => HTCondor-Worker_simple-lc03_2.cern.ch, host_fqdn => simple-lc03.cern.ch, host_ip => 188.184.84.189, container_ip => 10.10.0.12, type => worker_node, execution_id => 2}], dns_pre_exists => true} (file: /etc/puppetlabs/code/environments/simple/modules/simple_grid/manifests/components/swarm.pp, line: 101, column: 3) on node simple-lc01.cern.ch

Hope this saves some time!