You use Embedded Ruby (ERB) to create configuration file templates, which allows variables and logic to be used inside files on a node for host-specific information.
- [Instructor] In the last section we updated the Apache Cookbook's server recipe with information about the IP address and host name of this node. We added that information to the server recipe in the file resource. So remember what we did here. We used string interpolation to insert the node attributes for IP address and host name. Now if you're thinking ahead at all this might bother you. For example, I'm pasting all of the content for an HTML page inside of that string, what happens whenever I have a real web application that's very large? Am I going to paste the entire HTML code into that content string? That would be very impractical in the long-run.
However, I can't just go through and use a static HTML file here. Do you see why I couldn't just copy an HTML file into that directory? I'm inserting those node attributes for IP address and host name into the index.html page. If I have a static HTML page, how am I going to insert those node attributes into it? What we actually need here is the ability to build a real HTML file without using the file resource, but insert Ruby code like those node attributes into it.
Fortunately we have the template resource. The great thing about Ruby is it has a very elegant templating language called Embedded Ruby. Embedded Ruby is a way for us to actually insert Ruby code into any other file that's in a different file format. Templates can contain any Ruby code you'd like. Expressions or statements. A template is going to live in the templates directory of a cookbook.
To use a template we use it in the same way I use the file resource. Meaning I'm still going to provide the path to the file I'd like to create, but instead of pasting the content of that file directly into my recipe, I store it inside of an ERB file in the templates directory. So to use a template a couple things need to happen. First, I must add a template resource to our recipe instead of the file resource itself.
Second, I need to actually go through and build my Embedded Ruby template. So to add a template to our cookbook we'll use the chef generate command just like you saw me do with the recipe earlier. I'll need to provide the path to the cookbook and the name of my template. What do you think I'm going to use for the name? I'll run chef generate template on the Apache Cookbook and I'll name my template index.html.
This is actually going to add a new directory to my cookbook called templates. Inside of which I'll find a new file index.html.erb. This is our Embedded Ruby template. So how does ERB work? Embedded Ruby code can be placed inside of an ERB file inside of specifically formatted tags, think HTML, HTML code goes inside of HTML tags similarly Embedded Ruby code, any Ruby code you'd like, can go inside of specifically formatted tags within ERB files.
To give you a quick demo of how this works take a look at the following example. I have an if, else, in statement. So you'll notice the less than percent, percent greater than signs that mirror one another around your Ruby code. The code you see on the first, third and fifth line here means that I want to execute the Ruby code inside of the brackets, but do not print the results to the page.
In particular this type of tag is really great for Ruby logic, the second tag is a little bit different. Can you see why? You'll see that the opening tag actually is followed by an equal sign. This means evaluate the Ruby code in the brackets and print the results directly to the page and that's it for ERB. Two types of tags. One, do not print the results, the second with an equal sign, print the results.
What does this mean for our HTML code? Inside of index.html.erb I'm going to actually copy the content that I was previously using the file resource to create. Notice I'm inside of the templates directory index.html.erb. Can you see what's wrong here? That's right, we're not using string interpolation anymore. We need to replace those interpolation values, the pound, curly brace, with ERB tags.
Remember, inside of ERB templates we use ERB tags. I'm going to use the second type of tag that evaluates the node attributes for IP address and host name and prints those results directly to my index.html. With that in place I'll also need to adjust the recipe. No longer are we using a file resource, we'll replace this with a template resource. I'm still going to generate that template in the same location, however the source is now my new ERB file.
Now heading over to my terminal, I've already set all this up. I've run chef generate template and I've also scaffolded out my ERB code inside of the index.html.erb file. Finally, I've also adjusted my Apache Cookbook's server recipe. It contains the template resource now instead of this file resource which I have commented out. Can you see the difference between the two different ways of generating that file? I'm going to run it now.
Sudo chef-client--local-mode-r recipe for Apache. What should change? If you guessed nothing, you're right. If I run curl local host you'll see that nothing has actually changed. The results are the same, but the method by which I'm creating the index.html file is very different. Templates are a much more practical way to actually go about building and configuring files using chef.
- Configuration management
- Using Chef
- Installing the Chef development kit (ChefDK)
- Provisioning a CentOS instance
- Using recipes and the Apache cookbook
- Working with nodes and node objects
- Using templates and embedded Ruby
- Hosting a Chef server
- Provisioning nodes with AWS
- Testing deployments with Kitchen
- Exploring the Chef Supermarket
- Resolving dependencies with Berkshelf
- Working with server roles, environments, and data bags