Puppet variables

In Puppet, variables start with a $ followed by a name that must begin with a lowercase letter or an underscore. The rest of the name can contain uppercase or numbers.

Variables store values so that those values can be accessed in code later.

After we assigned a variable a value, we cannot reassign it. Variables depend on the order of evaluation: we must assign a variable a value before it can be resolved.

To illustrate the basic use of variables, let’s create a new module by executing the following from the modules folder:

#pdk new module my_variables

And the executing the following from the modules/my_variables folder:

#pdk new class local_variables

We update modules/my_variables/manifests/local_variables.pp:

# @summary Variables in the local scope

#

# Variables in the local scope

#

# @example

#   include my_variables::local_variables

class my_variables::local_variables {

  $my_content = 'Hello World'

  file { '/tmp/local_variables_0':

    ensure  => 'present',

    content => $my_content,

    path    => '/tmp/local_variables_0',

  }

  file { '/tmp/local_variables_1':

    ensure  => 'present',

    content => "Another ${my_content}",

    path    => '/tmp/local_variables_1',

  }

}

Here we follow Puppet’s variable naming constraints; seems like the convention is to also stick with lower case letters

By switching to using double-quotes in defining strings, we can include variables as shown (referred to as interpolation)

So far we have only used locally (to class) scoped variables; let’s change that by first creating a new class by executing the following from the modules/my_variables folder.

#pdk new class scoped_variables

We update modules/my_variables/manifests/scoped_variables.pp as shown:

# @summary Variables and scope

#

# Variables and scope

#

# @example

#   include my_variables::scoped_variables

class my_variables::scoped_variables {

  $my_content = 'Hello Scoped'

  file { '/tmp/scoped_variables_0':

    ensure  => 'present',

    content => $my_content,

    path    => '/tmp/scoped_variables_0',

  }

  file { '/tmp/scoped_variables_1':

    ensure  => 'present',

    content => $my_variables::local_variables::my_content,

    path    => '/tmp/scoped_variables_1',

  }

  file { '/tmp/scoped_variables_2':

    ensure  => 'present',

    content => $my_variables::my_content,

    path    => '/tmp/scoped_variables_2',

  }

  file { '/tmp/scoped_variables_3':

    ensure  => 'present',

    content => $::my_content,

    path    => '/tmp/scoped_variables_3',

  }

}

*Notice that we can reuse the same variable name, $my_content, in a different class

*(/tmp/scoped_variables_1 resource) Notice that we can reference the variable, $my_content, in a different class, local_variables, by using a fully qualified reference

*(/tmp/scoped_variables_2 resource) While we need to create it (which we will do shortly), we can reference module variables

*(/tmp/scoped_variables_3 resource) While we also need to create it (we will also do this shortly), we can reference global variables

Let’s define that module variable, by creating the module’s main class by executing the following from the modules/my_variables folder.

#pdk new class my_variables

And updating modules/my_variables/manifest/init.pp:

# @summary Using variables

#

# Using variables

#

# @example

#   include my_variables

class my_variables {

  $my_content = 'Hello Module'

  include my_variables::local_variables

  include my_variables::scoped_variables

}

Let’s define the global variable by updating manifest/site.pp:

$my_content = 'Hello Top'

node default {

  include my_module

  include my_variables

}

Recent Comments

No comments

Leave a Comment