Out-of-Scope Variables

280 views
Skip to first unread message

Yaniv Fine

unread,
Jul 17, 2012, 9:24:46 AM7/17/12
to puppet...@googlegroups.com

Hi Team .

i am trying to use "Out-of-Scope Variables"
 trying to setup kind of like an "answer" file that would be global file for other modules to use .like 40 variables .
and call the variables  from template or manifest file .

lets say i have module   share_module/mainfest/init.pp
$var1 = 'test1'
$var2 ='test2'
$ ........


i would like to use "share_module/mainfest/init.pp "

from another module , lets say apache module/template/ssl.erb file
witch some of the faribles there are the same in all my servers and some deffer and i would the answer file to be centralized place i can manage the vars  .

any pointers is much appreciated .


Thank You
Yaniv Fine 

 

llo...@oreillyauto.com

unread,
Jul 17, 2012, 10:37:06 AM7/17/12
to puppet...@googlegroups.com

Comments in line.

This can be done if you do something like

scope.lookupvar(share_module::varname)

in the ERB.
 

Honestly though, I would use hiera for this, it's cleaner.

In the .pp calling the template you can do something like:

$myvar = hiera('test1')

then just reference myvar in the template as a normal variable.

Yaniv Fine

unread,
Jul 17, 2012, 11:16:56 AM7/17/12
to puppet...@googlegroups.com
thank you for the answer .
i tried to use the "
scope.lookupvar(share_module::varname)" in the erb file 
but got a blank line (empty value)
i will have a look on
hiera
also ,
thank you

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/xbGfZaxVlPoJ.

To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

llo...@oreillyauto.com

unread,
Jul 17, 2012, 11:34:26 AM7/17/12
to puppet...@googlegroups.com


On Tuesday, July 17, 2012 10:16:56 AM UTC-5, Yaniv Fine wrote:
thank you for the answer .
i tried to use the "
scope.lookupvar(share_module::varname)" in the erb file 

Did you use '<%' or '<%=' in the template?

You also may need to add include 'share_module'  to the .pp


Yaniv Fine

unread,
Jul 17, 2012, 12:03:08 PM7/17/12
to puppet...@googlegroups.com

I have used the <% and <%= but didn't use the share_module

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/J7wTxTL3j58J.

Felix Frank

unread,
Jul 17, 2012, 12:06:41 PM7/17/12
to puppet...@googlegroups.com
Hi,

On 07/17/2012 05:16 PM, Yaniv Fine wrote:
> thank you for the answer .
> i tried to use the "
>
> |scope.lookupvar(share_module::varname)" in the erb file
> but got a blank line (empty value)
> i will have a look on |hiera
>
> also ,
> thank you

I'm fairly certain you need to quote the name of the variable you want
to look up.

scope.lookupvar("share_module::varname")

HTH,
Felix

Yaniv Fine

unread,
Jul 17, 2012, 12:09:52 PM7/17/12
to puppet...@googlegroups.com

Sorry  .
My mistake . I have used the share_module:varname

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.

Yaniv Fine

unread,
Jul 18, 2012, 3:10:13 AM7/18/12
to puppet...@googlegroups.com
i have tested this again today when i arrived to the office , with no luck .
when calling another module (sharedvar)/mainfest/generic.pp file from within my template file .
******************
class sharedvar {
$vartest = 'test1'
}
******************
i use in my erb file in module/mymodule/templates/bla.erb
<%= scope.lookupvar('sharedvar::vartest') %>


i get a blank line without my test1 parameter
what am i doing wrong here .

i can workaround this by declaring the vars in the same module that i want to use them but i want it to be cross module vars .

Erik Dalén

unread,
Jul 18, 2012, 3:47:51 AM7/18/12
to puppet...@googlegroups.com
Make sure you include sharedvar class in your sharedvar::generic class
before the template is called.
--
Erik Dalén

Felix Frank

unread,
Jul 18, 2012, 4:08:58 AM7/18/12
to puppet...@googlegroups.com
On 07/18/2012 09:47 AM, Erik Dal�n wrote:
> Make sure you include sharedvar class in your sharedvar::generic class
> before the template is called.

Right, but more succinctly:

sharedvar::generic does not appear to be the caller, but rather some
class in mymodule.

The point is: The class that uses the template must include the
sharedvar class.

Cheers,
Felix

Yaniv Fine

unread,
Jul 18, 2012, 4:31:17 AM7/18/12
to puppet...@googlegroups.com
i am missing something here .
those classes are from the same module .
i am calling sharedvar::vartest
class sharedvar {
$vartest = 'blabla
}'
from template file that reside on module "newmodule"

where do i put the include class and what syntax i use to call it ?

thank you :)

Felix Frank

unread,
Jul 18, 2012, 4:39:39 AM7/18/12
to puppet...@googlegroups.com
On 07/18/2012 10:31 AM, Yaniv Fine wrote:
> i am missing something here .
> those classes are from the same module .
> i am calling sharedvar::vartest
> class sharedvar {
> $vartest = 'blabla
> }'
> from template file that reside on module "newmodule"
>
> where do i put the include class and what syntax i use to call it ?
>
> thank you :)

I presume you have a file { } somewhere that uses content => template(...).

Where is that file declared?

Yaniv Fine

unread,
Jul 18, 2012, 4:44:50 AM7/18/12
to puppet...@googlegroups.com
in "newmodule" pp file

Felix Frank

unread,
Jul 18, 2012, 4:46:07 AM7/18/12
to puppet...@googlegroups.com
On 07/18/2012 10:44 AM, Yaniv Fine wrote:
> in "newmodule" pp file
>
> I presume you have a file { } somewhere that uses content => template(...).
>
> Where is that file declared?

Let me rephrase that: What is the name of the class in which the file is
defined? If it's not inside a class, put it in one. Also "include
sharedvar" in that class.

Yaniv Fine

unread,
Jul 18, 2012, 5:03:01 AM7/18/12
to puppet...@googlegroups.com
Frank .
let me put it all together :)

i have a module name "sharevar"
i have another module "newmodule"
inside "sharevar"/manifest/genreic.pp
class sharevar::generec {
$vartest = 'infooramtion'
}

i have newmodule/manifest/men.pp
that include the content => template path erb file
and i have the newmodule/templates/new.erb file
that i want to use value from ("sharevar"/manifest/genreic.pp)
the $vartest value

hope this helps

thank you for your effort .


Felix Frank

unread,
Jul 18, 2012, 5:11:16 AM7/18/12
to puppet...@googlegroups.com


On 07/18/2012 11:03 AM, Yaniv Fine wrote:
> Frank .
> let me put it all together :)
>
> i have a module name "sharevar"
> i have another module "newmodule"
> inside "sharevar"/manifest/genreic.pp
> class sharevar::generec {
> $vartest = 'infooramtion'
> }
>
> i have newmodule/manifest/men.pp
> that include the content => template path erb file
> and i have the newmodule/templates/new.erb file
> that i want to use value from ("sharevar"/manifest/genreic.pp)
> the $vartest value
>
> hope this helps

Not really, sorry.

Please share the content of newmodule/manifest/men.pp

R.I.Pienaar

unread,
Jul 18, 2012, 5:12:18 AM7/18/12
to puppet...@googlegroups.com


----- Original Message -----
> From: "Yaniv Fine" <yani...@gmail.com>
> To: puppet...@googlegroups.com
> Sent: Wednesday, July 18, 2012 10:03:01 AM
> Subject: Re: [Puppet Users] Re: Out-of-Scope Variables
>
>
> Frank .
> let me put it all together :)
>
> i have a module name "sharevar"
> i have another module "newmodule"
> inside "sharevar"/manifest/genreic.pp
> class sharevar::generec {
> $vartest = 'infooramtion'
> }
>
> i have newmodule/manifest/men.pp
> that include the content => template path erb file
> and i have the newmodule/templates/new.erb file
> that i want to use value from ("sharevar"/manifest/genreic.pp)
> the $vartest value
>
> hope this helps
>
> thank you for your effort .

class sharevar {
$vartest = "info"
}

class newmodule {
# you have to include sharevar somewhere, can be in a node or anywhere else
include sharevar

# access the variable in another module in a .pp file
notify{"vartest is ${sharevar::vartest}": }

# access it in a template
file{"/tmp/test": content => template("new.erb")}
}


new.erb:

<%= scope.lookupvar("sharevar::vartest") %>

Yaniv Fine

unread,
Jul 18, 2012, 5:14:04 AM7/18/12
to puppet...@googlegroups.com
class newmodule::men {
      file {'/tmp/bootstrap.properties':
      mode  => '644',
      owner => 'root',
      group => 'root',
      content => template('newmodule/bootstrap.erb')
  }
}


Yaniv Fine

unread,
Jul 18, 2012, 5:16:57 AM7/18/12
to puppet...@googlegroups.com
i'll try that .

Yaniv Fine

unread,
Jul 18, 2012, 5:26:02 AM7/18/12
to puppet...@googlegroups.com
i am getting

Error 400 on SERVER: Could not find class sharevar::generic in namespaces newmodule::men

Felix Frank

unread,
Jul 18, 2012, 5:52:07 AM7/18/12
to puppet...@googlegroups.com
On 07/18/2012 11:26 AM, Yaniv Fine wrote:
> i am getting
>
> Error 400 on SERVER: Could not find class sharevar::generic in
> namespaces newmodule::men

Hardly surprising.

> inside "sharevar"/manifest/genreic.pp
>> class sharevar::generec {

Both file and class name are horribly typoed. Please fix.

Yaniv Fine

unread,
Jul 18, 2012, 6:22:52 AM7/18/12
to puppet...@googlegroups.com
...
still getting it even after fixing .

Could not find class sharedvar::generic in namespaces newmodule::men

i am doing copy paste .
damm

Felix Frank

unread,
Jul 18, 2012, 7:03:29 AM7/18/12
to puppet...@googlegroups.com
On 07/18/2012 12:22 PM, Yaniv Fine wrote:
> ...
> still getting it even after fixing .
>
> Could not find class sharedvar::generic in namespaces newmodule::men
>
> i am doing copy paste .
> damm

Is the module name "sharevar" or "sharedvar"?

The easiest way about this was for you to supply the full paths to all
manifest files and the manifest contents.

Yaniv Fine

unread,
Jul 18, 2012, 7:32:05 AM7/18/12
to puppet...@googlegroups.com
this time i got it !!!!.
i have made a newbie error by creating the mentifest/pp file inside a directory name share_var instead of sharevar !!

thank you very much for you kind help ...


Reply all
Reply to author
Forward
0 new messages