Variables & Parameters

Getting Started

Like any other programming language, Puppet DSL have variables and parameters, but they are different somehow. Puppet DSL is extended from Ruby, but the concept of variables are not the same with Ruby, you should be aware of this. Parameters are not the same with parameters in some other programming language, in ruby, parameters are for methods, or code block, but in Puppet DSL, only class has parameters, no methods in Puppet DSL.

Variables

In Puppet, variable names are prefixed with a $, and a value is assigned with the = operator.

Assigning a short string to a variable, for example, would look like this:

$string = 'this is a variable'

Once you have defined a variable you can use it anywhere in your manifest.

The basics of variables will seem familiar if you know another scripting or programming language. However, there are pretty different in Puppet:

  • Unlike resource declarations, variable assignments are parse-order dependent. This means that you must assign a variable in your manifest before you can use it.

  • If you try to use a variable that has not been defined, the Puppet parser won't complain. Instead, Puppet will treat the variable as having the special undef value, not nil in Ruby. Though this may cause an error later in the compilation process, in some cases it will pass through and cause unexpected results.

  • You can only assign a variable once within a single scope. Once it's assigned, the value cannot be changed. The value of a Puppet variable may vary across different systems in your infrastructure, but not within them.

Variable interpolation lets you insert the value of a variable into a string. For instance, if you wanted Puppet to manage several files in the user home directory, you could assign this directory path to a variable:

$home_dir = '/home/ryan'

Once the variable is set, you can avoid repeating the same directory path by inserting the $home_dir variable into the beginning of any string.

For example, you might use it in the title of a file resource:

file { "${home_dir}/index.html":
  ...
}

Class Parameters

In Puppet, parameters only serve for class, there is no methods in Puppet, so no parameters for methods invocation. Class parameter provides a way to set variables for using rather than hard-coding in the class definition. In the previous section we've discussed about class, we know how to create new class like this:

class classname {
  ...
}

But actually we could add class parameters right after the classname:

class classname ( $parameter = 'default' ) {
  ...
}

All the class parameter are defined in an opening curly brace, and parameter can have it's default value, otherwise, the = operate flag can be omitted.

class classname ( $var1, $var2 ) {
  ...
}

The invocation for class has and not has parameters are different, if a class has no parameters, then we can invoke it simply as:

include classname

While if a class has parameters, then we should invoke it like this:

class {'classname':
    var1 => 'the first variable',
    var2 => 'the second variable',

}

Learning by Doing

Let's refactor the previous user home index page, this time we need to display user's name with a welcome phase.

First, edit previously created class compose_user_index via vim compose_user_index/manifests/init.pp, and replace the content with below:

class compose_user_index($username) {
  file {'/root/index.html':
        ensure => file,
        content => "Welcome ${username}'s web page!"
  }

  exec {'python -m SimpleHTTPServer 7777 &' :
        cwd     => '/root',
        path    => ['/usr/bin', '/usr/sbin',],
  }
}

And then update the class invocation manifest, via vim compose_user_index/examples/test.pp:

class {'compose_user_index':
        username => 'Ryan'
}

Validate you new updated manifest, and take a dry run, if everything okay, puppet apply:

puppet apply compose_user_index/examples/test.pp

And then check http://<host>:7777/, you will see the display content has been changed.

Even through, there are still a couple of details you should be aware of about class parameters.

  • Parameters should not relies on the value of other parameters, because all the parameters are binding parallelly, not sequentially. so you can not assign a parameter like this: "$var1 = ${var2}", but $title is an exception, because $title is assigned prior to the parameters binding.

  • Default value and undef value, default value and undef values are pretty important, if you didn't specify a default value or undef, when invoke the class, a explicit value must be specified, otherwise, you will earn an error. But default value is different from undef value.

    • Default value, parameter will get a default value if you didn't explicitly assign one.
    • Unlike default value, undef value is a different concept, which will tell puppet that this parameter can be omitted, this corresponds to some optional config on linux system, such as user resource type, password and home is optional, and in user class definition, these two parameter is assigned with undef value.

results matching ""

    No results matching ""