scope of variable inside defined type

41 views
Skip to first unread message

Rajgourav Jain

unread,
Nov 17, 2014, 8:06:25 AM11/17/14
to puppet...@googlegroups.com
Hi,

I have written two defined type and trying to use some variables between them. 

----foo/manifest/init.pp ------------
define foo( $x=undef, $y=undef)
{

  if $x == undef {
   $xx = $defualt_x
  } else {
    $xx  = $x
  }

if $y == undef {
   $yy $defualt_y
  } else {
    $yy= $y
  }

 fooinstall {$xx:}
}


--------------foo/manifest/install.pp------------------- 
define foo::install (){
  if  $yy= "some text" 
  { 
     puppet code here
   }
  else {
     puppet code here 
  }
}


------ sites.pp ----
{
foo { "Aname" :
   $x => a,
   $y => b,
 }
}

Could you suggest how can I make this code work. I am not able to access $xx and $yy in defined type foo::install.

Thanks,
Rajgourav Jain

Steven Post

unread,
Nov 17, 2014, 10:21:27 AM11/17/14
to puppet...@googlegroups.com
Hi,

The simplest I can think of would be to set up '$yy' as a parameter, and then pass that one down from install.pp
However, looking at this limited code snippet, perhaps you shouldn't even have a define in the install.pp, but a class that inherits from 'foo'.
Then you can access the $yy variable like this: $localvariable = $::foo::yy

Best regards,
Steven

jcbollinger

unread,
Nov 17, 2014, 2:54:28 PM11/17/14
to puppet...@googlegroups.com
No, you're not.  They are not in scope there.  Your simpler options include:
  1. Passing the values as parameters of foo::install, or
  2. merging the foo::install defined type into the foo defined type (provided foo::install is declared only in foo).
On the other hand, the pattern you are using looks like it might be one better served by classes.  This is the case if it doesn't make sense for any node to have two different Foo or Foo::Install resources.  In that event, if you change at least foo to a class then foo::install can refer to its variables by their qualified names: $::foo::xx (or usually $foo::xx works too), etc..


John

Rajgourav Jain

unread,
Nov 18, 2014, 6:56:01 AM11/18/14
to puppet...@googlegroups.com
Thanks Steven and jcbollinger for the suggestion. 

Either solution works for me and I have parameterized define type foo::install and it works. 

Since I will be calling foo module multiple time for a node I have to keep foo/init.pp as defined type. 
 
Thanks,
Rajgourav Jain

Antoine Cotten

unread,
Nov 18, 2014, 1:24:15 PM11/18/14
to puppet...@googlegroups.com
What about the getparam function from the stdlib module?

Quote:
define example_resource($param) { }

example_resource
{ "example_resource_instance": param => "param_value" }

getparam(Example_resource["example_resource_instance"], "param")


jcbollinger

unread,
Nov 18, 2014, 6:07:30 PM11/18/14
to puppet...@googlegroups.com


On Tuesday, November 18, 2014 7:24:15 AM UTC-6, Antoine Cotten wrote:
What about the getparam function from the stdlib module?


It's bad juju, if for no other reason than that it is evaluation-order dependent.  It wouldn't work anyway in this particular case because you have to feed it a reference to the resource from which to obtain the parameter, and the 'fooinstall' instance in the question does not have such a reference and cannot construct one (since it doesn't know the title).  You could pass the title as a parameter, but why do that when you could instead pass the wanted value directly?
 

John

Reply all
Reply to author
Forward
0 new messages