To understand how to modify an existing ServerTemplate to use cookbooks and recipes from another Chef cookbook repository.
This tutorial assumes that you have a basic understanding of Chef and RightScale. It's recommended that you complete the Introduction to Chef Tutorial prior to starting this tutorial.
In this tutorial you will perform the following tasks:
- Clone and update a RepoPath
- Clone and update a ServerTemplate to use a custom RepoPath
- Run Chef recipes from the new cookbook repository on a running server
Upon completion of this tutorial you will have a basic understanding of how Chef cookbooks and recipes can be used in the RightScale platform to launch and configure servers using ServerTemplates.
Create a Deployment
Create a new deployment that you will use to launch a server later in this tutorial (e.g. Beginner Chef Tutorial). See Create a New Deployment.
The next step is to import a new cookbook that you want to use in a ServerTemplate.
- Go to Design > Repositories > New.
- Add the following sample repository that contains a few prebuilt cookbooks that you can use for this tutorial.
Name - Provide a user-friendly name. (e.g. RS Example Cookbooks)
Description - Provide a brief description about the repository.
Type - Select 'Git' from the drop-down menu.
Tag/Branch - Enter 'master' for this value.
URL - Enter 'git://github.com/rightscale/rs_chef_training.git'
Git SSH Key - Ignore this field because the cookbooks are in a publicly visible repository. Note: Git SSH Keys are only required for retrieving cookbooks from private repositories.
Cookbook Paths - Enter 'cookbooks' for this value and click Add.
- Since you want to use the cookbooks in this repository, keep the checkbox checked (default) to automatically import all of the repository's cookbooks to the Primary Namespace.
- Click OK. You should have imported the following cookbooks into the RightScale account.
Import and Clone a ServerTemplate
Find the ServerTemplate that you want to customize. For this tutorial you will use the "Base" ServerTemplate published by RightScale, which is recommended if you are building a new custom ServerTemplate from scratch. However, the same customization steps will apply if you want to modify any existing ServerTemplate so that it can use Chef cookbooks from a specific repository.
- Go to the MultiCloudMarketplace (Design > MultiCloud Marketplace > ServerTemplates) and import the latest revision of the following ServerTemplate:
- Clone the ServerTemplate and rename it accordingly. (e.g. My Base)
- Commit the ServerTemplate, so that the first committed revision of the ServerTemplate matches the original version that it was cloned from, which will make it easier to perform a differential and track your changes to the ServerTemplate. Leave a simple commit message. (e.g. original version; no changes)
Attach a Cookbook to a ServerTemplate
- Go the Scripts tab of the editable HEAD version of the ServerTemplate.
- Click Modify.
- Click Attach Cookbooks.
- Find and select the 'hello_world' cookbook, which you imported into the RightScale account in a previous step. If more than one version of the cookbook is listed, select the version that is in the "primary" namespace and click Attach Selected.
Add a Recipe to the ServerTemplate
Now that the 'hello_world' cookbook is attached to the ServerTemplate, you can add any of its Chef recipes into one of the boot/operational/decomission sections of the ServerTemplate.
- Under the Scripts tab of the HEAD version of the cloned ServerTemplate (e.g. My Base), find the 'hello_world' cookbook in the right-hand pane.
- Expand the cookbook to see its recipes. Drag and drop the cookbook's "default" recipe ('hello_world::default') to the bottom of the ServerTemplate's Boot Sequence.
Create and Launch a Server
You are now ready to create and launch a server using the modified ServerTemplate.
- Go to the the HEAD version of the cloned ServerTemplate (e.g. My Base).
- Click the Add Server button to add a server to your deployment (e.g. Beginner Chef Tutorial). Be sure to use the HEAD version (not rev 1) of the ServerTemplate because you want to be able to make changes to the ServerTemplate's Scripts tab so that you can test them on the running server. See the Add Server Assistant page if you need help adding a server to a deployment. Select or create any required cloud resources (e.g. SSH Key, Security Group, etc.). Name the server accordingly. (e.g. My Beginner)
To complete all exercises in this tutorial, you must launch the server using an Ubuntu RightImage. The default RightImage for the ServerTemplate is a CentOS 5.6 RightImage, so in the "Hardware" section of the Server Details tab of the "Add Server Assistant" wizard, you must select an Ubuntu RightImage from the "MultiCloud Image" drop-down menu.
- Launch the server. If you see a warning message that some cookbooks are missing, you can ignore this message because the missing cookbooks are not used by any of the recipes in the ServerTemplate. All of the inputs for any required scripts in the Boot Sequence should already be predefined at the ServerTemplate level so you shouldn't have to set any values. Click the Launch button.
- Once it becomes operational, go to the server's Audit Entries tab. The last script that was run in the boot phase was the "Hello World" script. Verify that the script was executed successfully.
Tip: To find the specific audit entry click on the link as shown above in the Summary column, expand on the Details in the Converging section, then use your browser Search (^F) feature.
Run an Operational Script
Now you're going to add a couple other Chef recipes and run them as operational scripts. Remember, a server's list of scripts always reflects what is defined by its ServerTemplate. Since the running server was launched with an editable HEAD version of a ServerTemplate, we can easily add a couple operational scripts to the ServerTemplate and execute them on the running server. (Note: The 'Any Script' option only exists for RightScripts. An equivalent feature is currently not supported for Chef recipes.)
Note: You can also safely add a RightScript to a ServerTemplate that uses Chef Recipes. For a more detailed example, see Using RightScripts with Chef Recipes.
- Go to the cloned ServerTemplate that you used to launch the running server.
- Go to the Scripts tab of the editable HEAD version of the ServerTemplate. Click Modify.
- Click Attach Cookbooks.
- Find and select the 'httpd' and 'notifies' cookbooks (which you previously imported to your RightScale account in an earlier step along with the 'hello_world' cookbook.
- Under the Scripts tab, add the following Chef recipes as operational scripts. The scripts you are going to add are in the httpd cookbook. Since these are operational scripts that will be executed on-demand, the order of the scripts is not important.
- Go the running server's Scripts tab and run the install_httpd operational script. To view the actual code that is executed on the server, you can view the script on GitHub. (view recipe)
- Once install_httpd has completed, go to the running server's Info tab and click the Public DNS name link or copy and paste the server's public IP address into a browser window to view the basic Apache package that was installed on the server by the Chef recipe. (Note: If you tried to view the server's public IP address prior to running the install_httpd script you would have received an "Unable to connect" message in your browser.)
- Now you'll run the other operational script to replace the default index page with a customized version that will display your name. Go back to the server's Scripts tab and run the setup_index_page operational script. This particular script has a Chef attribute.
View the script on GitHub. (view recipe)
Chef attributes are treated as inputs. For this particular script, you will need to provide your name before the script can be run. Enter your name and click Continue. (Tip: Remember to change the drop-down from "Unset" to "Text" and then enter your name.)
- Once the script is successfully run, refresh the preview page in your other browser window to view the new, custom index page.
Congratulations! You have successfully completed this tutorial. You learned how to modify an existing ServerTemplate by attaching additional cookbooks and running new recipes.
Post Tutorial Steps
Terminate the Server
Don't forget to terminate your server when through using it to avoid any unwanted costs.
Intermediate Chef Tutorial
To learn how to set up a basic Chef development environment and learn how to create a repository of your own cookbooks, see the Intermediate 1 Chef Tutorial.