Newbie namespace question: how to access variable in different module

55 views
Skip to first unread message

Thomas Hartmann

unread,
Apr 2, 2014, 11:51:36 AM4/2/14
to puppet...@googlegroups.com
Hi all,

I am quite new to Puppet and are currently struggeling with the namespace.

Thus, I have a manifest to deploy my service organized in
manifests --> myservice.pp

node "myservice.thing.foo"{
  class {'myservice::deploy':}
}

The module is located in
modules --> myservice --> manifests --> deploy.pp

class myservice::deploy()
{
...dostuffhere...
}

So, now I would like to move some variables to a separate module, e.g.,
modules --> myservice --> manifests --> subdir --> variablefoo.pp

class variablefoo
{
  $myvar1 = "HALLOWORLD"
  class variablebar
  {
    $myarr1 = [1,2,3]
  }
}

How can I access the elements in variablefoo.pp in my modules::deploy.pp, i.e., what is the complete namespace path??

I tried several pathes that seemed to be somewhat reasonable to me as

$testvar = $subdir::variablefoo::myvar
$testvar = myservice::subdir::variablefoo::myvar

but apparently I have not really got into puppet yet :(

Maybe, somebody can help me out?

Cheers and thanks,
  Thomas

jcbollinger

unread,
Apr 3, 2014, 10:15:39 AM4/3/14
to puppet...@googlegroups.com


On Wednesday, April 2, 2014 10:51:36 AM UTC-5, Thomas Hartmann wrote:
Hi all,

I am quite new to Puppet and are currently struggeling with the namespace.

Thus, I have a manifest to deploy my service organized in
manifests --> myservice.pp

node "myservice.thing.foo"{
  class {'myservice::deploy':}
}

The module is located in
modules --> myservice --> manifests --> deploy.pp



Ok, stop there.
  1. Terminology: a Puppet module is a collection of sero or more classes, definitions, files, templates, plugins, and data, using a common top-level namespace and physically grouped together in a subdirectory of one of the directories in your module path.  A module is not "located in" a single manifest file, even if there is only one manifest file belonging to that module.
  2. Terminology: Puppet classes are not modules.
  3. Usage: your particular usage of the term "module" seems Pythonesque.  If you have that mindset or a similar one, then you must take care to shed it when working on Puppet code.  Specifically, in Python and some other languages, files provide both physical and logical organization of your code, whereas in Puppet they provide only physical organization.  The main practical implication is that you should have only one top-level declaration in any manifest file other than your site manifest and any other manifest logically included (e.g. via 'import') in your site manifest.
  4. Organization: most Puppeteers would expect a manifest file named "myservice.pp" to contain the definition of a class or definition named [some_module::[inner_namespace::]]myservice.  I guess a definition of a node "myservice" works, too, but it is more common to organize single-node node definition manifests under manifests/nodes/.

 
class myservice::deploy()
{
...dostuffhere...
}

So, now I would like to move some variables to a separate module, e.g.,
modules --> myservice --> manifests --> subdir --> variablefoo.pp

class variablefoo
{
  $myvar1 = "HALLOWORLD"
  class variablebar
  {
    $myarr1 = [1,2,3]
  }
}


Do not nest classes. It only confuses people.

Moreover, you define a class named 'variablefoo', but you put its manifest where Puppet will look for a class named "myservice::subdir::variablefoo".  If you really want the class to be named "variablefoo" (and to be recognized by the autoloader) then it should go in modules/variablefoo/manifests/init.pp, where it would be the main class of a module of the same name.  Otherwise, you should define it with the correct name.



How can I access the elements in variablefoo.pp in my modules::deploy.pp, i.e., what is the complete namespace path??


The fully-qualified name of any variable is ${::classname::variablename}.  Often people use a mostly-qualified form instead: ${classname::variablename}.  The curly braces are optional in many contexts.  The "classname" part is whatever you define the class's name to be.  In your example it is "variablefoo", but if you named the class so that the autoloader could find it at its current location then its name would need to be "myservice::subdir::variablefoo".

 

I tried several pathes that seemed to be somewhat reasonable to me as

$testvar = $subdir::variablefoo::myvar
$testvar = myservice::subdir::variablefoo::myvar

but apparently I have not really got into puppet yet :(

Maybe, somebody can help me out?



I think you want to rename the class to "myservice::subdir::variablefoo", and then refer to the variable as $myservice::subdir::variablefoo::myvar.


John

Thomas Hartmann

unread,
Apr 8, 2014, 8:06:40 AM4/8/14
to puppet...@googlegroups.com
Hi John,

many thanks for your detailed answer!

Yes, I am somewhat socialized with Python and have just started with Puppet ;)

Cheers and thanks,
 Thomas
Reply all
Reply to author
Forward
0 new messages