Tuesday 18 October 2016

Configure a resource

1. Set up your working directory

From your terminal window, create the chef-repo directory under your home directory, ~/.

Terminal: ~

 
skytap@host-1:~$ mkdir ~/chef-repo
Now cd there.

Terminal: ~

 
skytap@host-1:~$ cd ~/chef-repo
  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 /tmpdirectory.
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

                      
skytap@host-1:~$ chef-client --local-mode hello.rb
[2016-10-19T19:04:37+00:00] WARN: No config file found or specified on command line, using command line options.[2016-10-19T19:04:37+00:00] WARN: No cookbooks directory found at or above current directory.  Assuming /root/chef-repo.Starting Chef Client, version 12.14.89resolving cookbooks for run list: []Synchronizing Cookbooks:Installing Cookbook Gems:Compiling Cookbooks...[2016-10-19T19:04:41+00:00] WARN: Node ubuntu-1404 has an empty run list.Converging 1 resourcesRecipe: @recipe_files::/root/chef-repo/hello.rb  * file[/tmp/motd] action create    - create new file /tmp/motd    - update content in file /tmp/motd from none to b94d27    --- /tmp/motd   2016-10-19 19:04:41.501156760 +0000    +++ /tmp/.chef-motd20161019-2441-1fo9v5r    2016-10-19 19:04:41.501156760 +0000    @@ -1 +1,2 @@    +hello world Running handlers:Running handlers completeChef Client finished, 1/1 resources updated in 03 seconds
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

   
skytap@host-1:~$ more /tmp/motd
  /tmp/motd  hello world

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

                
skytap@host-1:~$ chef-client --local-mode hello.rb
[2016-10-19T19:04:45+00:00] WARN: No config file found or specified on command line, using command line options.[2016-10-19T19:04:45+00:00] WARN: No cookbooks directory found at or above current directory.  Assuming /root/chef-repo.Starting Chef Client, version 12.14.89resolving cookbooks for run list: []Synchronizing Cookbooks:Installing Cookbook Gems:Compiling Cookbooks...[2016-10-19T19:04:47+00:00] WARN: Node ubuntu-1404 has an empty run list.Converging 1 resourcesRecipe: @recipe_files::/root/chef-repo/hello.rb  * file[/tmp/motd] action create (up to date) Running handlers:Running handlers completeChef Client finished, 0/1 resources updated in 01 seconds
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

                      
skytap@host-1:~$ chef-client --local-mode hello.rb
[2016-10-19T19:04:50+00:00] WARN: No config file found or specified on command line, using command line options.[2016-10-19T19:04:50+00:00] WARN: No cookbooks directory found at or above current directory.  Assuming /root/chef-repo.Starting Chef Client, version 12.14.89resolving cookbooks for run list: []Synchronizing Cookbooks:Installing Cookbook Gems:Compiling Cookbooks...[2016-10-19T19:04:52+00:00] WARN: Node ubuntu-1404 has an empty run list.Converging 1 resourcesRecipe: @recipe_files::/root/chef-repo/hello.rb  * file[/tmp/motd] action create    - update content in file /tmp/motd from b94d27 to c38c60    --- /tmp/motd   2016-10-19 19:04:41.501156760 +0000    +++ /tmp/.chef-motd20161019-2922-aor7u4 2016-10-19 19:04:52.329156760 +0000    @@ -1,2 +1,2 @@    -hello world    +hello chef Running handlers:Running handlers completeChef Client finished, 1/1 resources updated in 01 seconds
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

 
skytap@host-1:~$ echo 'hello robots' > /tmp/motd
Now run chef-client using the options as shown below.

Terminal: ~/chef-repo

                      
skytap@host-1:~$ chef-client --local-mode hello.rb
[2016-10-19T19:04:55+00:00] WARN: No config file found or specified on command line, using command line options.[2016-10-19T19:04:55+00:00] WARN: No cookbooks directory found at or above current directory.  Assuming /root/chef-repo.Starting Chef Client, version 12.14.89resolving cookbooks for run list: []Synchronizing Cookbooks:Installing Cookbook Gems:Compiling Cookbooks...[2016-10-19T19:04:58+00:00] WARN: Node ubuntu-1404 has an empty run list.Converging 1 resourcesRecipe: @recipe_files::/root/chef-repo/hello.rb  * file[/tmp/motd] action create    - update content in file /tmp/motd from 548078 to c38c60    --- /tmp/motd   2016-10-19 19:04:52.949156760 +0000    +++ /tmp/.chef-motd20161019-3164-deebcn 2016-10-19 19:04:58.685156760 +0000    @@ -1,2 +1,2 @@    -hello robots    +hello chef Running handlers:Running handlers completeChef Client finished, 1/1 resources updated in 02 seconds
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

                 
skytap@host-1:~$ chef-client --local-mode goodbye.rb
[2016-10-19T19:05:01+00:00] WARN: No config file found or specified on command line, using command line options.[2016-10-19T19:05:01+00:00] WARN: No cookbooks directory found at or above current directory.  Assuming /root/chef-repo.Starting Chef Client, version 12.14.89resolving cookbooks for run list: []Synchronizing Cookbooks:Installing Cookbook Gems:Compiling Cookbooks...[2016-10-19T19:05:04+00:00] WARN: Node ubuntu-1404 has an empty run list.Converging 1 resourcesRecipe: @recipe_files::/root/chef-repo/goodbye.rb  * file[/tmp/motd] action delete    - delete file /tmp/motd Running handlers:Running handlers completeChef Client finished, 1/1 resources updated in 02 seconds
The output shows that /tmp/motd is now gone, but let's prove it.

Terminal: ~/chef-repo

  
skytap@host-1:~$ more /tmp/motd
/tmp/motd: No such file or directory

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