new question ... is there a way to get the name of...
# puppet-enterprise
j
new question ... is there a way to get the name of the current class, similar to the
$module_name
variable? so in a
profile::base
class,
$module_name
is
profile
, and (something) is
profile::base
?
h
It it is not as easy as getting the $module_name. You need to get the puppet stack (a function in my tahu module at github will let you do that) and then from that figuring out the closest class context from there. In Ruby it is somewhat more direct, but not really an API. There is a $name variable that may be what you are looking for, but depends on the context where it is used (in a function called from a class body, in an included defined resurce, possilby nested, etc.
j
i'll look into
$name
next week (i'm shutting down now and i'm out tomorrow), thanks for the pointers
h
Copy code
cat namecheck.pp
class banana {
  notice "class banana name = ${name}"
  fruit { 'mango': }
  sallad()
}
define fruit() {
  notice $name
}
function sallad() {
  notice "name in sallad function is ${name}"
}
include banana
henrik$ cat namecheck.pp
class banana {
  notice "class banana name = ${name}"
  fruit { 'mango': }
  sallad()
}
define fruit() {
  notice $name
}
function sallad() {
  notice "name in sallad function is ${name}"
}
include banana

henrik$ puppet apply namecheck.pp
Notice: Scope(Class[Banana]): class banana name = banana
Notice: Scope(Class[main]): name in sallad function is main
Notice: Scope(Fruit[mango]): mango
The
$name
is always the name/title of the resource/class-body being evaluated. As you can see functions are typically in
main
scope. Functions in ruby can get the callers scope and resource associated with those scopes and also pass a scope to what it calls. this is used by functions like
include()
.