Puppet Language Essentials
The course is part of this learning path
Puppet is an IT automation system. If you need to install, configure, and update servers, then Puppet can help you tremendously. Instead of doing all of these tasks manually, you can tell Puppet to configure your servers for you.
Not only will Puppet free you from the drudgery of repetitive tasks, but you will also gain major benefits, such as consistency, reliability, speed of deployment, ease of recovery, and scalability.
Do you often have slightly different configurations on servers that are supposed to be identical? With Puppet, you’ll no longer have to figure out why something works on one server but doesn’t work on another one because Puppet will configure them in the same way. You’ll also have less downtime because there is less that can go wrong when everything is configured the way it’s supposed to be.
Do you always seem to be adding more servers? Provisioning servers is a breeze when Puppet already knows how to configure them.
This course will get you started on bringing these benefits to your network. It’s a hands-on course with exercises every step of the way to give you experience using Puppet. First, I will show you how to install Puppet on a virtual machine on your own desktop. Then you will use it as a test environment to learn how to write Puppet code to automate server configuration.
- Install Puppet server
- Use pre-built Puppet modules
- Use manifests, classes, resources, facts, nodes, and templates
- Create your own Puppet modules
Sometimes you may need to make your templates more flexible. For example, suppose that some of your Apache servers are configured to use HTTPS and some of them use only HTTP, but the rest of their settings are basically the same. You could have two different Apache templates, but it would be cleaner and more maintainable to have one template that you pass an HTTPS parameter to.
One of the great things about EPP templates is that you can pass parameters into them, which is not something you can do with ERB templates. The ERB approach to using external variables is much messier and less reliable.
To try out using a parameter, let's modify our motd template so that you can pass a parameter saying whether or not you want to include Apache's location information. That is its server route and document route. Let's go back into the template.
If you have any parameters in the template then you need to add them to the beginning of the template. You can't put any content before your parameters.
First we start with the non-printing tag. Then we put one or more parameters inside pipe characters.
After the opening pipe, you need to put the data type of the first parameter.
Here are the core data types in Puppet. There are other data types, but these are the ones you will use most often. As you can see, Puppet includes the typical data types that you would find in other languages.
The data type we need for our parameter is Boolean because it will be either true or false that we want to print the Apache directory locations.
So we type Boolean and then the name of the parameter. Let's call it print_paths. Then you can put in a default value if you want. That's usually a good idea, so you don't force people to set a parameter when using the template. To set a default you just put the equal sign and the default value, which we'll set to true. Finally you need to put in the closing pipe and the closing tag, which must have a dash so that it doesn't print a new line.
Now we need to use the parameter. We'll just put in an if statement between the two lines about Apache directory locations.
Of course you could leave out the equals equals true part and it would still work.
Then we just need to end the if statement with a closing brace and non-printing tags after the two lines about Apache locations.
We're all done so let's try it. It didn't do anything because when the print paths parameter is true, the template does exactly what it did before. So we need to set the parameter to false to see if it works.
To pass a parameter we add a comma after the template name argument, then a curvy brace, then the name of the parameter in quotes and without the dollar sign. Then a hash rocket, then the value we want the parameter to be, then a closing brace.
That's it, so let's try it again. It worked. It didn't print the two lines about Apache directories, but it still printed the other lines.
Before we leave this, let's be responsible programmers and document the parameter. Comment tags in EPP are probably what you would expect. They start with less than, percent, and then have a pound sign. I'll skip ahead after typing the comment. Now we end with a closing tag that starts with a dash, so it doesn't print a new line.
You might be wondering if this is a violation of the rule that parameters need to come first in the template. Well comment tags are the one exception to the rule and if you don't end them with a new line stripping tag, then you'll get a syntax error. So some EPP purists even put comments after the parameters, but I think that makes it less readable, so I put them first.
And that's it for this lesson.
About the Author
Guy launched his first training website in 1995 and he's been helping people learn IT technologies ever since. He has been a sysadmin, instructor, sales engineer, IT manager, and entrepreneur. In his most recent venture, he founded and led a cloud-based training infrastructure company that provided virtual labs for some of the largest software vendors in the world. Guy’s passion is making complex technology easy to understand. His activities outside of work have included riding an elephant and skydiving (although not at the same time).