1. Set up your working directory
From your terminal window, create the
chef-repo
directory under your home directory, ~/
.Terminal: ~
Now
cd
there.Terminal: ~
We believe typing in the code and commands is the best way to learn. If you prefer to copy and paste things and are using our free trial VM, you may find it easier to continue this tutorial by opening this page in a browser from your VM. You can also copy and paste the text into your remote session. Use the Clipboard command in the menu bar to copy text to your session's clipboard.
2. Create the MOTD file
In this step, you'll first create the file and set the initial MOTD. To keep things basic, you'll configure the file in the
/tmp
directory.
Next, you'll write what's called a recipe to describe the desired state of the MOTD file. Then you'll run chef-client, the program that applies your Chef code to place your system in the desired state. Typically,
chef-client
downloads and runs the latest Chef code from the Chef server, but in this lesson, you'll run chef-client
in what's called local mode to apply Chef code that exists locally on your server.
From your
~/chef-repo
directory, create a file named hello.rb
, add these contents, and then save the file.Editor: ~/chef-repo/hello.rb
1 2 3 | file '/tmp/motd' do content 'hello world' end |
From your terminal window, run the following
chef-client
command to apply what you've written.Terminal: ~/chef-repo
The output tells us that a new file,
/tmp/motd
, was created. (The warnings you see relate to concepts we haven't introduced yet, and can be safely ignored for now.)
Now verify that the file was written. Run the
more
command, which prints a file to the console.Terminal: ~/chef-repo
Run the command a second time
Now, let's see now what happens when you run the same
chef-client
command again.Terminal: ~/chef-repo
This time you get a different response – the file is already up to date. This is because Chef applies the configuration only when it needs to.
Chef looks at the current configuration state and applies the action only if the current state doesn't match the desired state. Here, Chef doesn't create or modify
/tmp/motd
because it already exists and its contents didn't change. We call this approach test and repair.3. Update the MOTD file's contents
Now you're going to change the MOTD.
Modify
hello.rb
like this ('hello world' becomes 'hello chef'.)Editor: ~/chef-repo/hello.rb
1 2 3 | file '/tmp/motd' do content 'hello chef' end |
Run
chef-client
using the options as shown below.Terminal: ~/chef-repo
This time Chef applies the action because you've changed the desired state of the file.
4. Ensure the MOTD file's contents are not changed by anyone else
You need to make sure that no other process can change the MOTD.
Imagine that a co-worker manually changes
/tmp/motd
by replacing 'hello chef' with 'hello robots'. Go ahead and change your copy of /tmp/motd
through your text editor. Or you can change the file from the command line like this.Terminal: ~/chef-repo
Now run
chef-client
using the options as shown below.Terminal: ~/chef-repo
Chef restored the original configuration. This is actually a really good thing because Chef ensures that the actual state of your resource matches what you specify, even if it is altered by some outside process. Chef enables you to both apply a new configuration state as well as ensure that the current state stays how you want it.
In practice, it's common to configure
chef-client
to act as a service that runs periodically or as part of a continuous delivery system such as Chef Automate. Running Chef through automation helps to ensure that your servers remain configured as you expect and also enables them to be reconfigured when you need them to be.5. Delete the MOTD file
OK, you're done experimenting with the MOTD, so let's clean up. From your
~/chef-repo
directory, create a new file namedgoodbye.rb
and save the following content to it.Editor: ~/chef-repo/goodbye.rb
1 2 3 | file '/tmp/motd' do action :delete end |
Now apply
goodbye.rb
to delete the file.Terminal: ~/chef-repo
The output shows that
/tmp/motd
is now gone, but let's prove it.Terminal: ~/chef-repo
Summary
You ran a few basic Chef commands and got a flavor of what Chef can do. You learned that a resource describes one part of the system and its desired state. You worked with a file, which is one kind of resource.
Resources describe the what, not the how
A recipe is a file that holds one or more resources. Each resource declares what state a part of the system should be in, but not how to get there. Chef handles these complexities for you.
In this lesson, you declared that the file
/tmp/motd
must exist and what its contents are, but you didn't specify how to create or write to the file. This layer of abstraction can not only make you more productive, but it can also make your work more portable across platforms.Resources have actions
When you deleted the file, you saw the
:delete
action.
Think of an action as the process that achieves the desired configuration state. Every resource in Chef has a default action, and it's often the most common affirmative one – for example, create a file, install a package, and start a service.
When we created the file we didn't specify the
:create
action because :create
is the default. But of course you can specify it if you want.
The documentation for each resource type, file for example, explains the type's default action.
Recipes organize resources
In Chef,
hello.rb
is an example of a recipe, or an ordered series of configuration states. A recipe typically contains related states, such as everything needed to configure a web server, database server, or a load balancer.
Our recipe states everything we need to manage the MOTD file. You used
chef-client
in local mode to apply that recipe from the command line.
No comments:
Post a Comment