Node scope variables

27 views
Skip to first unread message

Robin Y.

unread,
Mar 4, 2014, 7:13:32 PM3/4/14
to puppet...@googlegroups.com
Greetings,

I could use some clarity on node scope, class scope, and best practices.  I have the following:

# Node definition
if $hostname == 'mynode-dev' {
    $app_version = '1.0'
    include robin_dev
}

# Class definition 
class robin_dev {
    if $app_version == undef {
        $app_version = '0.5'
    }
    include robin_dev::install
}

class robin_dev::install {
    package {
        "app_name":                       ensure => $robin_dev::app_version;
    }
}

My intent is to set $app_version within my node definition.  If $app_version is not set there, I 'd like it to default to a version that is set within class robin_dev itself, which is 0.5 in the above example.

While the above example works as I just described, i'm not understanding how $robin_dev::app_version referenced in class robin_dev::install returns "1.0" when I its assigned in the node definition.  I initially assumed that class robin_dev would need to re-assign the variable within the class itself like this:

# Class definition 
class robin_dev {
    if $app_version == undef {
        $version = '0.5'
    } else {
                    $version = $app_version
    include robin_dev::install
}

 class robin_dev::install {
    package {
        "app_name":                       ensure => $robin_dev::version;
    }
}

 Any insights would be appreciated.  Thank you.

Craig Dunn

unread,
Mar 5, 2014, 2:35:18 AM3/5/14
to puppet...@googlegroups.com

You seem to have a couple of misunderstandings here - Firstly what you call a node definition isn't a node definition, that would look something like

node 'mynode-dev' {
include robin_dev
}

Secondly, you want to make your robin_dev class parameterized to achieve what you want, e.g.:

class robin_dev (
$app_version = '0.5'
) {
include robin_dev::install
}

This parameter can now be overridden when you declare the class in the node definition….

class { 'robin_dev':
app_version => '1.0',
}

Or even better, use Hiera and override it there and include the class without parameters

eg: hosts/mynode-dev.yaml
---
robin_dev::app_version: 1.0

Regards
Craig

--
Enviatics | Automation and Configuration Management
http://www.enviatics.com | @Enviatics
Puppet Training Courses http://www.enviatics.com/training/
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com (mailto:puppet-users...@googlegroups.com).
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c51f4240-200f-425e-b6b5-abb3347bc9cf%40googlegroups.com (https://groups.google.com/d/msgid/puppet-users/c51f4240-200f-425e-b6b5-abb3347bc9cf%40googlegroups.com?utm_medium=email&utm_source=footer).
> For more options, visit https://groups.google.com/groups/opt_out.



Reply all
Reply to author
Forward
0 new messages