How to import variables from another manifests/file??

5,724 views
Skip to first unread message

Sans

unread,
Dec 2, 2011, 10:46:42 AM12/2/11
to Puppet Users
How can I globally declare a variable, so that it can be used across
the manifests and templates? I tried putting all my variables:

---------------------------------
$my_var1 = 'my 1st variable'
$my_var2 = 'my 2nd variable"
---------------------------------

in a file, e.g. called "settings.pp" and then try to accessing them
from another manifests after importing settings.pp, but that's
absolutely not working. I'm writing a costume module for our and group
and I want individual user only to edit the "settings.pp" to put their
own value to make this module work for them. How can I make that
happen? Cheers!!

Craig White

unread,
Dec 2, 2011, 11:07:58 AM12/2/11
to puppet...@googlegroups.com

----
presuming that you aren't using an ENC, you would probably want to simply put the variable in the node definition itself.

Craig

Nigel Kersten

unread,
Dec 2, 2011, 1:14:40 PM12/2/11
to puppet...@googlegroups.com
Assuming you did something like:

# modulepath/modules/settings/manifests/init.pp
class settings {
  $my_var1 = "myvar1"
}

and include the class "settings", from other classes you can refer to it as:

$settings::my_var1 
 
Does that help?

I noticed you used the word "import". The most frictionless way to use Puppet these days is to avoid import, and to instead structure your classes in modules, and use the autoloader and "include" classes rather than importing manifest files.

--
Nigel Kersten
Product Manager, Puppet Labs


Sans

unread,
Dec 2, 2011, 2:57:04 PM12/2/11
to Puppet Users
Hi Nigel,

It's actually like: "modulepath/modules/mymodule/manifests/
settings.pp" and I was trying to use those variables in the other
manifests in "mymodule". I know that autoloader and "include" classes
are the way of doing these day but as nothing worked so far, so I
tried "import" method as well. I didn't try "$settings::my_var1" thing
yet but I'm giving it a shot now.

Cheers,
Santanu

Nigel Kersten

unread,
Dec 2, 2011, 3:01:43 PM12/2/11
to puppet...@googlegroups.com
On Fri, Dec 2, 2011 at 11:57 AM, Sans <r.sant...@gmail.com> wrote:
Hi Nigel,

It's actually like: "modulepath/modules/mymodule/manifests/
settings.pp" and I was trying to use those variables in the other
manifests in "mymodule". I know that autoloader and "include" classes
are the way of doing these day but as nothing worked so far, so I
tried "import" method as well. I didn't try "$settings::my_var1" thing
yet but I'm giving it a shot now.

Thanks for the typo/thinko correction :)

Hopefully this documentation should make the right info apparent?

If not, please file bugs with us so we can make this easier to understand and discover.
 

Cheers,
Santanu


>
> Assuming you did something like:
>
> # modulepath/modules/settings/manifests/init.pp
> class settings {
>   $my_var1 = "myvar1"
>
> }
>
> and include the class "settings", from other classes you can refer to it as:
>
> $settings::my_var1
>
> Does that help?
>
> I noticed you used the word "import". The most frictionless way to use
> Puppet these days is to avoid import, and to instead structure your classes
> in modules, and use the autoloader and "include" classes rather than
> importing manifest files.
>
> --
> Nigel Kersten
> Product Manager, Puppet Labs

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
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.

Jo Rhett

unread,
Dec 2, 2011, 4:01:17 PM12/2/11
to puppet...@googlegroups.com
Don't confuse them.  import tells puppet to read a file it wouldn't normally.  Any .pp file in a module directory will be read, so import means nothing.

You might need to set require => Class['my module::settings'] to ensure that this module is evaluated first so that the variables are available.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
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.


-- 
Jo Rhett
Net Consonance : consonant endings by net philanthropy, open source and other randomness

Sans

unread,
Dec 2, 2011, 11:44:11 PM12/2/11
to Puppet Users
>
> Thanks for the typo/thinko correction :)
>
> Hopefully this documentation should make the right info apparent?http://docs.puppetlabs.com/learning/variables.html

>
> If not, please file bugs with us so we can make this easier to understand
> and discover.

I must be doing something really stupid - I still don't get it and and
trying to understand what I'm doing wrong. This is what I have so
far....

-------------------------------------------
# /modulepath/modules/zmfs/manifests/settings.pp:
class zmfs::settings {
$l_dir = '/zmfs/ldir.ac.uk'
.......
.......
}

# /modulepath/modules/zmfs/manifests/envcheck.pp:
class zmfs::envcheck {
define env_check($file, $string, $swdir, $refreshonly = 'true') {
......
......
}
}

# /modulepath/modules/zmfs/manifests/config.pp:
include zmfs::settings
include zmfs::envcheck

class zmfs::config {
env_check { vo_lhcb:
file => '/etc/profile.d/zfs-env.sh',
string => "$settings::l_dir",
.......
.......
}
}
-----------------------------------------------------------

But all I get an empty string on the target node. Is this what I'm
supposed to do? Or I'm still missing something? Cheers!!

Nigel Kersten

unread,
Dec 3, 2011, 12:56:19 PM12/3/11
to puppet...@googlegroups.com
You want to use $zmfs::settings::l_dir

You're fully qualifying the variable, so you need the "full path" to it, including the name of the module it lives in.
 

Sans

unread,
Dec 5, 2011, 6:31:49 AM12/5/11
to Puppet Users
Thanks Nigel! That works like a charm. But leaves me with one question
and another problem. First the question: If I always need to specify
the varable like "$zmfs::settings::l_dir", then waht the significance
of the "include" statement (e.g. include zmfs::settings ) in the
beginning of the manifest?

Now the problem is: If I define "env_check" inside a
'class' (envcheck.pp in my example code above) and specify "include
zmfs::envcheck" in the config.pp, I get this error:

-------------------------
err: Could not retrieve catalog from remote server: Error 400 on
SERVER: Puppet::Parser::AST::Resource failed with error ArgumentError:
Invalid resource type line_check at /etc/puppet/modules/zmfs/manifests/
config.pp:94 on node farm002.. .. ..ac.uk
-------------------------

It only works if the envcheck.pp like this way:


------------------------
# /modulepath/modules/zmfs/manifests/envcheck.pp:

define env_check($file, $string, $swdir, $refreshonly = 'true') {
......
......
}

------------------------

(i.e. without having a class) and use "import envcheck.pp", instead
of: include zmfs::envcheck in the manifest. Does any one know what's
going wrong?

Cheers,
San

Sans

unread,
Dec 5, 2011, 6:45:33 AM12/5/11
to Puppet Users
Sorry for the noise: using "cvmfs::settings::env_check" fixed that.
Which makes me thinking again, what's the importance of the include
statement if one always needs to use the fully qualified variable
names. Cheers!!

Nigel Kersten

unread,
Dec 5, 2011, 12:26:15 PM12/5/11
to puppet...@googlegroups.com
On Mon, Dec 5, 2011 at 3:31 AM, Sans <r.sant...@gmail.com> wrote:
Thanks Nigel! That works like a charm. But leaves me with one question
and another problem. First the question: If I always need to specify
the varable like "$zmfs::settings::l_dir", then waht the significance
of the "include" statement (e.g. include zmfs::settings ) in the
beginning of the manifest?

Including the class is what will cause the manifests in that class to be evaluated. Does that help?
 

Now the problem is: If I define "env_check" inside a
'class' (envcheck.pp in my example code above) and specify "include
zmfs::envcheck" in the config.pp, I get this error:

Make sure the names are the same.

e.g. a define called "zmfs::envcheck" should be in

$modulepath/modules/zmfs/manifests/envcheck.pp

It looks like you're mixing an extra underscore in there.

If you're doing this with a *define*, then you don't need to import or include that specific file.  Just declare it in that location, and not inside a class.

When you use that define, so long as the define is found in a path like the one above where the autoloader will find it, you don't need to import or include "zmfs::envcheck" anywhere.


 

-------------------------
err: Could not retrieve catalog from remote server: Error 400 on
SERVER: Puppet::Parser::AST::Resource failed with error ArgumentError:
Invalid resource type line_check at /etc/puppet/modules/zmfs/manifests/
config.pp:94 on node farm002.. .. ..ac.uk
-------------------------

It only works if the envcheck.pp like this way:


------------------------
  # /modulepath/modules/zmfs/manifests/envcheck.pp:

   define env_check($file, $string, $swdir, $refreshonly = 'true') {
       ......
       ......
   }
------------------------

(i.e. without having a class) and use "import  envcheck.pp", instead
of: include zmfs::envcheck in the manifest. Does any one know what's
going wrong?

Cheers,
San


>
> You want to use $zmfs::settings::l_dir
>
> You're fully qualifying the variable, so you need the "full path" to it,
> including the name of the module it lives in.
>
> --
> Nigel Kersten
> Product Manager, Puppet Labs

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
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.

Reply all
Reply to author
Forward
0 new messages