prep for 2.8:  scopes and variables: nodes are not classes

I have been preparing for 2.8 by
experimenting with 2.7.X and seeing
what it takes to get rid of the
warnings.  I would like to share my
results in a number of upcoming  posts.

Begin with the simple question of
hard-wiring scopes.  the 'shocking'
revelation is that variables defined
in nodes are no longer visible outside
the node.  So, if you rely on that for
configuration, you should rewrite the
node as a class, and then include that
class in a trivial node.  You can then
get at the variables by explicitly
mentioning the scope of the class.

This post doesn't talk about default
values, inheritance, or specialization.
I will talk about that later.

Attached are three scripts

01_config.pp

        old style
        defines variables in nodes
        relies on dynamic scope

02_config.pp

        broken
        failed attempt to qualify variables
        nodes are not classes
        nodes cannot be named as a scope

03_config.pp

        fixed, new style
        all variables defined in classes
        nodes as containers for classes
        no variables defined in nodes

Of course it is still possible
to define resources and variables
in nodes.  But the variables will
not be visible in any class scope and
cannot be used to implicitly configure
classes.

Comments and corrections welcome.

--vagn

results of running the three scripts:

vagn@nika:~/puppet-nika/patterns$ ./01_config.pp
notice: Scope(Class[main]): puppet version: 2.7.2

notice: Scope(Class[Legacy]): var1_global is global
warning: Dynamic lookup of $var2_base_node at /u0/home/vagn/puppet-nika/patterns/01_config.pp:21 is deprecated. Support will be removed in Puppet 2.8. Use a fully-qualified variable name (e.g., $classname::variable) or parameterized classes.
notice: Scope(Class[Legacy]): var2 defined in base node
warning: Dynamic lookup of $var3_node at /u0/home/vagn/puppet-nika/patterns/01_config.pp:22 is deprecated. Support will be removed in Puppet 2.8. Use a fully-qualified variable name (e.g., $classname::variable) or parameterized classes.
notice: Scope(Class[Legacy]): var3 defined in default (aka host) node
notice: Finished catalog run in 0.15 seconds
vagn@nika:~/puppet-nika/patterns$ ./02_config.pp
notice: Scope(Class[main]): puppet version: 2.7.2

notice: Scope(Class[Legacy]): var1_global is global
warning: Scope(Class[Legacy]): Could not look up qualified variable 'base::var2_base_node'; class base could not be found at /u0/home/vagn/puppet-nika/patterns/02_config.pp:21
notice: Scope(Class[Legacy]):
warning: Scope(Class[Legacy]): Could not look up qualified variable 'default::var3_node'; class default could not be found at /u0/home/vagn/puppet-nika/patterns/02_config.pp:22
notice: Scope(Class[Legacy]):
notice: Finished catalog run in 0.15 seconds
vagn@nika:~/puppet-nika/patterns$ ./03_config.pp
notice: Scope(Class[main]): puppet version: 2.7.2

notice: Scope(Class[Foo]): var1_global is global
notice: Scope(Class[Foo]): var2 defined in base node
notice: Scope(Class[Foo]): var3 defined in default (aka host) node
notice: Finished catalog run in 0.15 seconds
vagn@nika:~/puppet-nika/patterns$


--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

#! /usr/bin/puppet apply

$version = inline_template("<%= `puppet --version` %>")
notice("puppet version: ${version}")

$var1_global = "var1_global is global"

node base {
        $var2_base_node = "var2 defined in base node"
}

node default inherits base {
        $var3_node = "var3 defined in default (aka host) node"

        include legacy
}


class legacy {
        notice("${::var1_global}")
        notice("${var2_base_node}")     # warn
        notice("${var3_node}")          # warn
}

#! /usr/bin/puppet apply

$version = inline_template("<%= `puppet --version` %>")
notice("puppet version: ${version}")

$var1_global = "var1_global is global"

node base {
        $var2_base_node = "var2 defined in base node"
}

node default inherits base {
        $var3_node = "var3 defined in default (aka host) node"

        include legacy
}


class legacy {
        notice("${::var1_global}")
        notice("${base::var2_base_node}")       # fail
        notice("${default::var3_node}")         # fail
}

# both failures above are because nodes cannot be
# named as a class scope
#! /usr/bin/puppet apply

$version = inline_template("<%= `puppet --version` %>")
notice("puppet version: ${version}")

$var1_global = "var1_global is global"

class base {
        $var2_base_node = "var2 defined in base node"
}

class default_class inherits base {
        $var3_node = "var3 defined in default (aka host) node"

        include foo
}

class foo {
        notice("${::var1_global}")
        notice("${base::var2_base_node}")
        notice("${default_class::var3_node}")
}

# nodes are pure local scope:
#       nodes are not global scope
#       nodes are not classes, and cannot be scoped as classes
#       variables in nodes are not visible elsewhere
#
node default {
        include default_class
}

Reply via email to