Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [22]

By Root 377 0
: checkhosts definition will be called, and the $hostname variable passed to it.

Variables

In some of the previous examples you encountered a new concept: variables. Variables are prefixed with a $ and allow you to specify data that can then be used in resources, for example, as the content of a file. There are also some default variables, like $title, that contain the title of the resource.

You should place your variable strings in double quotes, as single-quoted strings will not do any variable interpolation. You can reference other variables in your strings, and Puppet supports the use of brackets, { }, to help highlight variables in strings.

Variables are also used to pass in facts from the Facter application; for example, the variable $operatingsystem is equivalent to the value of the operatingsystem fact returned by Facter. We'll discuss some of these in the "Facts" section later in this chapter.

Variable Scoping

There is some important information you need to know about variables. First, because Puppet is a declarative rather than an imperative language, variables are scoped differently than in some other languages. The principal result of this is that you cannot redefine a variable inside the same scope it was defined in. So you couldn't redefine the $packagelist in the apache class as shown in Listing 3-8.

You can see in Listing 3-8 that we've tried to define the $packagelist variable twice. If we were to try to compile and apply this configuration, the Puppet client would return the following error:

The error also tells us the file and line number in the file where we've tried to redefine the variable.

So what's a scope? Each class, definition, or node introduces a new scope, and there is also a top scope for everything defined outside of those structures. Each structure represents a nonoverlapping scope; for example, class A is one scope, class B is another scope, and node C is yet another scope. You can see this in Listing 3-9.


The same variable can be used and defined in both the apache and apache2 classes without generating an error. The same scoping is true of different nodes and definitions.

Variables and Class Inheritance

Variable scope is also important when inheriting classes. If we have a variable of the same name in a class and a subclass, its behavior may not be what you expect. In Listing 3-10, we define two classes with the same variable in them, and then have the second class inherit the first.

In the first class, master, we define the value of the $server as "primary". The second class, slave, inherits the first class, and we try to redefine the value of the $server variable to "secondary". This redefinition will not work because the $server variable remains in the scope that it was created in when the master class is inherited, and hence remains "primary".

There is a workaround for this that makes use of the include function we introduced earlier in this chapter.


First, we define the $server variable in the top scope, outside the scope of both classes. This is because the include function includes the entire stanza of the master class in the slave class. If the variable was defined inside the master class, we'd get an error when the slave class was evaluated because you can't have two definitions of the same variable in a single class.

Next, we define the master class and then the slave class. The slave class, which is a new scope, changes the value of the $server variable and then uses the include function to incorporate the content of the master class. Now when the slave class is evaluated, the value of the $server variable will be "secondary".

Qualified Variables

We can also reference variables assigned in one class in another class. This allows us to use a previously defined variable again by qualifying it as you can see on the following lines:

You can see in the first class, master, we've defined the value of the variable $server as primary". In the second class, we've created a variable called $ms. The value of this variable is

Return Main Page Previous Page Next Page

®Online Book Reader