dumb question, and i feel like i've asked this her...
# puppet
j
dumb question, and i feel like i've asked this here before but i can't find it in my notes ... is there a way for a function, written in the puppet language, to find the name of the module, class, or define which called it? ... the
$title
of the define, and the caller's filename and line number, would also be "nice to have"
in this particular instance i want to create a
debug_notice()
function which runs
notice()
but only if "debugging is enabled" for the module which called the function ... i'm thinking the list of modules to have debug messages enabled will be configured via custom fact on the target machine ... this is to have debug messages working while i'm hacking on the code, but not pollute the puppetserver's logs with debugging logs that aren't needed ... if there's already some other way to do this, i'd love a pointer to that as well
c
See here for an example: https://github.com/hlindberg/tahu/blob/master/lib/puppet/functions/tahu/stacktrace.rb Makes total sense for adding context to a message such as in
debug_notice()
. I'd caution against using this to drive actual function logic as having function behavior depend on items that aren't explicitly passed as arguments leads to difficult debugging.
j
for now it's only needed so the function can tell whether "debugging is enabled" for the module which called it, it's not going to drive any changes to what is or is not added to the catalog. this pattern (deciding whether to print a message or not, based on the caller) is something i've been doing with other languages for a while now
so this can only be done using ruby? ... i'm not against doing that, it just means extra time explaining it to others (which seems to always happen, no matter how well i try to document it using comments in the code)
c
The closest thing at the puppet language level would be the "Compiler Variables": https://help.puppet.com/core/current/Content/PuppetCore/lang_facts_builtin_variables.htm But the docs note these conflict with
strict_variables
, which is on by default in Puppet 8. Historically, they were used for guiding Hiera lookups, but that use case has been supplanted by the Module Data layer.
j
okay, looks simple enough ... and i just did a search for
tahu
in obsidian and found where i wrote this down on 2025-05-05 ... i just added some text to that note (including the word "caller") to make it easier to find the next time i feel like asking the same question again
it looked simple ... turns out what i actually need is
$caller_module_name
, but that doesn't seem to work within a function (neither does
$module_name
for that matter) ... am i missing something maybe?
c
I don't think so. Functions written in the Puppet Language are a very modern feature, and a different sort of evaluation scope from class and define bodies. Very likely those "compiler variables" were not implemented for functions.
j
not great, but understandable ...
before i waste any more time on this, do you happen to know if the
Puppet::Pops::PuppetStack
API existed in PE 2016.2.0 (aka Puppet 4.5.2) ?
c
The logging interfaces is also a Ruby-layer thing. So, that's another reason this makes more sense as a Ruby function.
Puppet::Pops::PuppetStack
was definitely added sometime after Puppet 4, but I'm not sure when. Quickest way to find out would be to just draft out a function that calls it and see if you get an undefined method error.
j
okay ... i'm going to put this idea aside for now. some time in the next few weeks/months i'm going to be re-writing our entire code base for PE 2023.8.4, i'll figure it out then. i've already spent too much time thinking about this (i have a tendency to wander off on tangets, trying to work on that)
thanks for the pointers (which i HAVE written down this time) 😁
c
j
that figures, i'm currently on 4.5.2 so it wouldn't have worked anyway
some time in the next few weeks i'll be starting on a total re-write of our code base, one of the first things i write (or try to write) will be a "debug message framework" that the rest of the code can use ... i'll be making use of the
Puppetstack
stuff for file/line info, but i'm also hoping that
$module_name
and
$caller_module_name
will work within puppet-language functions, the same way they do within defined types