Getting Variables out of define in site.pp

56 views
Skip to first unread message

em...@benjaminmertens.de

unread,
Jul 24, 2014, 6:42:54 AM7/24/14
to puppet...@googlegroups.com
Hi,

my servers have some kind of "role" like "app1" or "app1,app2" which im getting out of a selfmade global facter variable.
Im then going through this list and include classes depending on the content of this variable.

# site.pp

define iterateSystemRole
{
       
case $name {
               
'app1':   {
                        include app1
                        $defineVariable = 'content'
               
}
               
'app2':   {
                        include app2               
                }
               
default:  { }
       
}
}

$cur_system_role = split($::system_role, ',')
iterateSystemRole { $cur_system_role: }

node default {
        inlude xyz
}

Now i want to work with variables too. Like checking in class 'app1' if variable $defineVariable is set to "content".
I would do this like:

# app1.pp

class app1 {
  if $::
iterateSystemRole::defineVariable == 'content' {
     doSomething
  }
}

But the variable $::iterateSystemRole::defineVariable is always undefined. So i cant get the content from $defineVariable.

How can i get this working?
I googled a lot but did not find a way... I think its an issue with scoping but i hope there is a way to get this working...

Best regards!


Werner Flamme

unread,
Jul 24, 2014, 8:28:06 AM7/24/14
to puppet...@googlegroups.com
em...@benjaminmertens.de [24.07.2014 12:42]:
>
> # site.pp
>
> define iterateSystemRole {
> case $name {
> 'app1': {
> include app1
> $defineVariable = 'content'
> }
> 'app2': {
> include app2
> }
> default: { }
> }
> }
>
> But the variable $::iterateSystemRole::defineVariable is always undefined.
> So i cant get the content from $defineVariable.
>
> How can i get this working?
> I googled a lot but did not find a way... I think its an issue with scoping
> but i hope there is a way to get this working...

Did you try to swap the lines, so that they read

$defineVariable = 'content'
include app1

Does this help?

--


Henrik Lindberg

unread,
Jul 24, 2014, 10:24:48 AM7/24/14
to puppet...@googlegroups.com
That does not work because the variables set in a define are local and
private, there is no way that variable can be accessed from within an
included class.

Depending on puppet version, the scoping rules are slightly different
wrt. what can be accessed. In older versions you could use dynamic
scoping by changing your define to a class, but that does not work in
newer versions (dynamic scoping has been deprecated and removed because
of its many bad side effects).

You may instead want to use parameterized classes, and instead of an
include do this:

class { app1: defined_variable => 'content' }

and define the class like this:

class app1($defined_variable) {
# ...
}

This also has the advantage that you can control the configuration using
external data lookup via hiera instead of having your own logic
in puppet for this.

- henrik
--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Reply all
Reply to author
Forward
0 new messages