19.06.2015 Views

ADMIN

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chef<br />

Management<br />

Figure 5: The client has picked up its assigned recipe from the server and executed it. This puts a preconfigured<br />

version of Emacs on its disk.<br />

generated dynamically in part. Start<br />

by creating a new cookbook called<br />

beispiel in chef-repo:<br />

rake new_cookbook COOKBOOK=beispiel<br />

The command creates a beispiel<br />

folder below cookbooks, populates it<br />

with the required subdirectories, then<br />

creates an empty recipe named default.rb.<br />

Before you start filling this with content,<br />

first create a template for the file<br />

you want to create, /​tmp/​thoughts.<br />

txt. This will later contain the sentence<br />

Thought for the day:<br />

and the recipe will append an ingenious<br />

thought on a daily basis. The<br />

complete template is thus:<br />

Thought for the day: <br />

The recipe will replace the wildcard<br />

with text later on. The new template<br />

needs to be in templates/​default/​;<br />

you can save it as thoughts.txt.erb.<br />

Most recipes use templates like this,<br />

or, to quote the developers: “We love<br />

templates.”<br />

Hand Mixer<br />

Now, compose a matching recipe that<br />

picks up the template and uses it to<br />

generate the /​tmp/​thoughts.txt file.<br />

To save work here, you can extend<br />

the existing, but empty, default.rb<br />

recipe in the recipes subdirectory.<br />

The recipe for this example looks like:<br />

template "/tmp/thoughts.txt" do<br />

source "thoughts.txt.erb"<br />

variables :thought => U<br />

node[:thought]<br />

action :create<br />

end<br />

This should be fairly self-explanatory<br />

for Ruby aficionados: It creates the<br />

/​tmp/​thoughts.txt file from the<br />

thoughts.txt.erb template and then<br />

replaces the wildcard with the content<br />

of the thought variable. Now<br />

you just need to think about what<br />

thoughts to use here.<br />

Spice<br />

In this example, thought will be an attribute.<br />

Attributes store node-specific<br />

settings in a cookbook for recipes to<br />

evaluate and use. A typical attribute<br />

would be, say, a command-line parameter<br />

for a program launched automatically<br />

by a recipe. The attributes<br />

are identical for each call to the recipe<br />

and, thus, no more than constants<br />

provided by the recipe author.<br />

In contrast to genuine Ruby constants,<br />

attributes can be modified via<br />

the web interface (in the window<br />

used to assign cookbooks to nodes).<br />

A cookbook groups all of the attributes<br />

in its attributes subdirectory.<br />

For the example here, you need to<br />

create a beispiel.rb file with the following<br />

content:<br />

thought "Silence is golden ..."<br />

Now you just need to register the new<br />

cookbook with the server<br />

rake upload_cookbooks<br />

and assign it to one or multiple nodes<br />

in the web front end. After running<br />

chef-client, the /​tmp/​thoughts.txt<br />

file should appear.<br />

This recipe leaves much scope for<br />

improvement. For example, you could<br />

randomly choose the thought of the<br />

day, which Ruby programmers should<br />

handle easily. Because recipes are<br />

full-fledged Ruby scripts, you can<br />

draw from the full scope of the language<br />

and on RubyGems. In the case<br />

of the latter, the recipe should first<br />

check to see whether the Gem exists<br />

on the client and, if not, install it.<br />

The beispiel/​metadata.json file<br />

stores metadata on the new cookbook.<br />

Before you roll out the cookbook<br />

in a production environment,<br />

you might want to add some details.<br />

As the file extension suggests, the file<br />

uses the JSON format.<br />

Conclusions<br />

Chef is a complex piece of software,<br />

and once you have it running and<br />

have finished modifying or creating<br />

your recipes, it does make the administrator’s<br />

life much easier – at least<br />

on Linux systems. Unfortunately, the<br />

learning curve is very hard going for<br />

newcomers. The online documentation<br />

for Chef is fairly chaotic and<br />

incomplete [7]. If you need to know<br />

more about writing cookbooks, it<br />

is a good idea to download prebuilt<br />

examples and investigate them. The<br />

cookbook for emacs shows you how<br />

to use action :upgrade to install a<br />

package for example.<br />

Additionally, it is hard to find help or<br />

how-tos, even on the web, if you have<br />

a problem. Your best option here is<br />

to post your questions on the mailing<br />

list [8].<br />

n<br />

Info<br />

[1] Installation guide: [http:// wiki. opscode.​<br />

com/ display/ chef/ Installation]<br />

[2] RabbitMQ:<br />

[http:// www. rabbitmq. com/ server. html]<br />

[3] openSUSE build service for RabbitMQ:<br />

[http:// software. opensuse.​<br />

org/ search? q=rabbitmq‐server&​<br />

baseproject=openSUSE%3A11. 2]<br />

[4] Information on full-text searches in Chef:<br />

[http:// wiki. opscode. com/ display/ chef/​<br />

Search]<br />

[5] Repository with prebuilt cookbooks:<br />

[http:// cookbooks. opscode. com/]<br />

[6] Cookbook quick start: [http:// wiki. opscode.​<br />

com/ display/ chef/ Cookbook+Quick+Start]<br />

[7] Chef wiki:<br />

[http:// wiki. opscode. com/ display/ chef/]<br />

[8] Chef mailing list: [http:// lists. opscode.​<br />

com/ sympa/ lists/ opensource/ chef]<br />

www.admin-magazine.com<br />

Admin 01<br />

73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!