Chef Crash Course: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Azolotar (talk | contribs)
Azolotar (talk | contribs)
 
(40 intermediate revisions by the same user not shown)
Line 4: Line 4:
It can be adapted for use for a wide range of infrastructure, and can control such machines as Windows, Linux, Mac OS X as well as more specialized devices like Cisco ASA and Juniper network devices, and additionally to manage cloud based resources.
It can be adapted for use for a wide range of infrastructure, and can control such machines as Windows, Linux, Mac OS X as well as more specialized devices like Cisco ASA and Juniper network devices, and additionally to manage cloud based resources.
Tools like Chef are becoming increasingly popular for managing computer infrastructure nowadays, and its rivals are Puppet and Ansible, as well as numerous others.
Tools like Chef are becoming increasingly popular for managing computer infrastructure nowadays, and its rivals are Puppet and Ansible, as well as numerous others.
Below is a quick tutorial on how to install chef server, configure it to to control a Windows node, and get Chef to add a specific file to be displayed when a user logs in to the computer.
Below is a quick tutorial on how to install chef server, configure it to to control a Windows node, and get Chef to add a specific file to be displayed when a user logs in to the computer. <ref>https://learn.chef.io/learn-the-basics/windows/configure-a-resource/</ref>
Glossary
===Glossary===
Node – a client machine, i.e. machine to be configured.
Node – a client machine, i.e. machine to be configured.
Recipe – a configuration element, used to define what to configure and how.
Recipe – a configuration element, used to define what to configure and how.
Line 12: Line 12:
The workflow for getting a working Chef set up is:
The workflow for getting a working Chef set up is:
*Install the Chef server
*Install the Chef server
*Install the Chef Workstation
*Install the Chef clients on machines to be controlled
*Install the Chef clients on machines to be controlled
*Install the Chef Workstation
 
 
[[File:Start chef-3.png|800px]]
 
This article assumes that the reader is experienced with the command line of Linux.
 
== Install the Chef server ==
== Install the Chef server ==
At the moment, the Chef server can be installed only 64-bit Linux machines, as well as on Amazon Web Services and Microsoft Azure, which are outside the scope of this article.
At the moment, the Chef server can be installed only 64-bit Linux machines, as well as on Amazon Web Services and Microsoft Azure, which are outside the scope of this article.
Line 27: Line 33:
As a root user, install the Chef server package on the server, using the name of the package provided by Chef.  
As a root user, install the Chef server package on the server, using the name of the package provided by Chef.  


<code>$ dpkg -i /tmp/chef-server-core-<version>.deb</code>
<code>dpkg -i /tmp/chef-server-core-<version>.deb</code>


After a few minutes, the Chef server will be installed.
After a few minutes, the Chef server will be installed.
Line 33: Line 39:
Run the following to start all of the services:
Run the following to start all of the services:


<code>$ chef-server-ctl reconfigure</code>
<code>chef-server-ctl reconfigure</code>


Because the Chef server is composed of many different services that work together to create a functioning system, this step may take a few minutes to complete.
Because the Chef server is composed of many different services that work together to create a functioning system, this step may take a few minutes to complete.
Line 39: Line 45:
Run the following command to create an administrator:
Run the following command to create an administrator:


<code>$ chef-server-ctl user-create USER_NAME FIRST_NAME LAST_NAME EMAIL 'PASSWORD' --filename FILE_NAME</code>
<code>chef-server-ctl user-create USER_NAME FIRST_NAME LAST_NAME EMAIL 'PASSWORD' --filename FILE_NAME</code>


An RSA private key is generated automatically. This is the user’s private key and should be saved to a safe location. The --filename option will save the RSA private key to a specified path.
An RSA private key is generated automatically. This is the user’s private key and should be saved to a safe location. The --filename option will save the RSA private key to a specified path.
Line 45: Line 51:
For example:
For example:


<code>$ chef-server-ctl user-create tonymctony Tony McTony tony@mctony.ee 'hodor' --filename /path/to/tonymctony.pem</code>
<code>chef-server-ctl user-create tonymctony Tony McTony tony@mctony.ee 'hodor' --filename /path/to/tonymctony.pem</code>


Run the following command to create an organization:
Run the following command to create an organization:


<code>$ chef-server-ctl org-create short_name 'full_organization_name' --association_user user_name --filename ORGANIZATION-validator.pem</code>
<code>chef-server-ctl org-create short_name 'full_organization_name' --association_user user_name --filename ORGANIZATION-validator.pem</code>


The name must begin with a lower-case letter or digit, may only contain lower-case letters, digits, hyphens, and underscores, and must be between 1 and 255 characters. For example: familyinc.
The name must begin with a lower-case letter or digit, may only contain lower-case letters, digits, hyphens, and underscores, and must be between 1 and 255 characters. For example: familyinc.
Line 61: Line 67:
For example:
For example:


<code>$ chef-server-ctl org-create familyinc 'Family, Inc.' --association_user tonymctony --filename /path/to/familyinc-validator.pem</code>
<code>chef-server-ctl org-create familyinc 'Family, Inc.' --association_user tonymctony --filename /path/to/familyinc-validator.pem</code><ref>https://docs.chef.io/install_server.html#standalone</ref>
 
==Install the Chef Workstation==
==Install the Chef Workstation==
Now that our Chef server is up and running, our next action is to configure a workstation. The actual infrastructure coordination and configuration does not take place on the Chef server. This work is done on a workstation which then uploads the data to the server to influence the Chef environment. Once again, we assume that the machine in question is running Ubuntu.
Now that our Chef server is up and running, our next action is to configure a workstation. The actual infrastructure coordination and configuration does not take place on the Chef server. This work is done on a workstation which then uploads the data to the server to influence the Chef environment. Once again, we assume that the machine in question is running Ubuntu.
Line 98: Line 105:
5) When finished, open a command window and enter the following:
5) When finished, open a command window and enter the following:


<code>$ chef verify</code>
<code>chef verify</code>
This will verify the main components of the Chef development kit: the chef-client, the Chef development kit library, and the tools that are built into the Chef development kit. The output should be similar to:
This will verify the main components of the Chef development kit: the chef-client, the Chef development kit library, and the tools that are built into the Chef development kit. The output should be similar to:


Line 109: Line 116:
We will use the scp utility to download the user key and the organization validator key that we created on the Chef server. Before doing so, we will create the hidden directory where we will store these files:
We will use the scp utility to download the user key and the organization validator key that we created on the Chef server. Before doing so, we will create the hidden directory where we will store these files:


mkdir ~/chef-repo/.chef
<code>mkdir ~/chef-repo/.chef</code>
 
The method that you use to connect to the Chef server will determine how exactly we go about downloading the keys. Follow the method below that matches your setup:
The method that you use to connect to the Chef server will determine how exactly we go about downloading the keys. Follow the method below that matches your setup:


If you connect to your Chef server through SSH using password-based authentication, the scp command will work without significant modification.
If you connect to your Chef server through SSH using password-based authentication, the scp command will work without significant modification.


On your workstation, specify the username and domain name or IP address used to connect to the Chef server. Follow this immediately with a colon (:) and the path to the file you wish to download. After adding a space, indicate the directory on the local computer where you wish the download the files to be placed (~/chef-repo/.chef in our case).
On your workstation, specify the username and domain name or IP address used to connect to the Chef server. Follow this immediately with a colon (:) and the path to the file you wish to download. After adding a space, indicate the directory on the local computer where you wish the download the files to be placed (~/chef-repo/.chef in our case):
 
<code>scp username@server_domain_or_IP:/path/to/tonymctony.pem ~/chef-repo/.chef</code>
 
<code>scp username@server_domain_or_IP:/path/to/familyinc-validator.pem ~/chef-repo/.chef</code>
 
===Configuring Knife to Manage your Chef Environment===
 
Now that you have your Chef credentials available on your workstation, we can configure the knife command with the information it needs to connect to and control your Chef infrastructure. This is done through a knife.rb file that we will place in the ~/chef-repo/.chef directory along with our keys.
 
Open up a file called knife.rb in that directory in your text editor:
 
<code>nano ~/chef-repo/.chef/knife.rb</code>
In this file, paste the following information:
 
<pre>current_dir = File.dirname(__FILE__)
 
log_level                :info
 
log_location            STDOUT
 
node_name                "name_for_workstation"
 
client_key              "#{current_dir}/tonymctony.pem"


If you log into the Chef server using the root user account, your commands will look something like this. Remember to change both the domain name or IP address and the name of the key files you are trying to download to match your environment:
validation_client_name  "familyinc-validator"


<code> $ scp root@server_domain_or_IP:/root/admin.pem ~/chef-repo/.chef</code>
validation_key          "#{current_dir}/familyinc-validator.pem"


<code> $ scp root@server_domain_or_IP:/root/digitalocean-validator.pem ~/chef-repo/.chef</code>
chef_server_url          "https://server_domain_or_IP/organizations/familyinc"


If you connect to your Chef server using a non-root user, the commands will look more like this:
syntax_check_cache_path  "#{ENV['HOME']}/.chef/syntaxcache"


<code>$ scp username@server_domain_or_IP:/home/username/admin.pem ~/chef-repo/.chef</code>
cookbook_path            ["#{current_dir}/../cookbooks"]
</pre>


<code>$ scp username@server_domain_or_IP:/home/username/digitalocean-validator.pem ~/chef-repo/.chef</code>
===Testing===
 
Before we can begin testing this, we need to fetch the SSL key from the server. This is done with the following command:
 
<code>knife ssl fetch</code>
 
After the SSL certificate has been fetched, the following command should now work:
 
<code>knife client list</code>
 
With the output being:
 
<code> familyinc-validator </code>
 
If the above command correctly returns, your workstation is now set up to control your Chef environment.<ref>https://www.digitalocean.com/community/tutorials/how-to-set-up-a-chef-12-configuration-management-system-on-ubuntu-14-04-servers</ref>


=Install Chef client=
=Install Chef client=
Line 148: Line 194:
To run the chef-client at periodic intervals (so that it can check in with the Chef server automatically), configure the chef-client to run as a service or as a scheduled task. This can be done via the MSI, by selecting the Chef Client Service option on the Custom Setup page or by running the following command in the command prompt after the chef-client is installed:
To run the chef-client at periodic intervals (so that it can check in with the Chef server automatically), configure the chef-client to run as a service or as a scheduled task. This can be done via the MSI, by selecting the Chef Client Service option on the Custom Setup page or by running the following command in the command prompt after the chef-client is installed:


<code>$ chef-service-manager -a install</code>
<code>chef-service-manager -a install</code>


and then start the chef-client as a service:
and then start the chef-client as a service:


<code>$ chef-service-manager -a start</code>
<code>chef-service-manager -a start</code>


After the chef-client is configured to run as a service, the default file path is:  
After the chef-client is configured to run as a service, the default file path is:  
<code>c:\chef\chef-client.log</code>
<code>c:\chef\chef-client.log</code>
<ref>https://docs.chef.io/install_windows.html</ref>


==Adding a specific file to to a computer==
=Adding a specific file to to a computer=
Now we'll proceed to add a simple notepad file to be displaying when a user logs in to the computer.
Now we'll proceed to add a simple notepad file to be displaying when a user logs in to the computer.
Some prior knowledge is required:
Some prior knowledge is required:
Files and applications to be launched automatically are placed in the startup folder of Windows, with the typical location being
files and applications to be launched automatically are placed in the startup folder of Windows, with the typical location being
<code>C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp</code>.
<code>C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp</code>.
So we will simply instruct Chef to put a simple text file in that location, with the contents ''This computer is now managed by Chef''.
So we will simply instruct Chef to put a simple text file in that location, with the contents ''This computer is now managed by Chef''.
===Create Cookbook===
First, we will instruct the Chef Workstation to create a new Cookbook:
First, we will instruct the Chef Workstation to create a new Cookbook:


<code>$ knife cookbook create fileToStartup </code>
<code>knife cookbook create fileToStartup </code>


Then browse to the created cookbook recipes folder:
Then browse to the created cookbook recipes folder:


<code>$ cd ~/chef-repo/fileToStartup/recipes</code>
<code>cd ~/chef-repo/fileToStartup/recipes</code>


Listing the contents of this folder reveals a default recipe:
Listing the contents of this folder reveals a default recipe:
Line 179: Line 227:
Replace the contents of that file with the following:
Replace the contents of that file with the following:


<syntaxhighlight lang="ruby">file 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp' do
<syntaxhighlight lang="ruby">file 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\notification.txt' do
   content 'This computer is now managed by Chef'
   content 'This computer is now managed by Chef'
end
end
</syntaxhighlight>
</syntaxhighlight>
To elaborate on what is prescribed above: we set the file location and using the content section fill the file with the specified string. If the file does not exist, it will be created automatically. If it exists, but has a different content, it's content will be overwritten.
<ref>https://learn.chef.io/learn-the-basics/windows/configure-a-resource/</ref>
===Upload Cookbook===
Now that our basic cookbook is complete, we can upload it to our chef server.
We can do that by typing:
<code>knife cookbook upload fileToStartup </code>
Or, we can upload everything by typing:
<code>knife cookbook upload -a</code>
Now, we can modify the run-list of our nodes. We can do this easily by typing:
<code>knife node edit ''name_of_node''</code>
<ref>http://gettingstartedwithchef.com/introducing-chef-server.html</ref>
=Conclusion=
That's it! The next time the Chef client checks for changes from the Chef server, it will see that a new cookbook has been assigned to it, and will apply the necessary changes.
While the procedure described here is certainly more lengthy than simply putting the file on the machine manually, this was just an example - Chef can be used to manage applications, specific machine settings, scheduled tasks, and many other infrastructure configuration items. Plus, it scales well to a large number of managed machines, both physical and virtual.
====Metadata====
Author: Anton Zolotarjov
Group: CSE 15
Date: 2016-06-12
==References==
{{reflist}}

Latest revision as of 01:48, 14 June 2016

Chef Crash Course

Introduction

Chef is a suite of tools that belong to the desired state configuration tools, and basically functions as a client-server type application, with the server continuously checking whether the clients meet certain criteria and adjusting them as necessary when they do not meet them. It can be adapted for use for a wide range of infrastructure, and can control such machines as Windows, Linux, Mac OS X as well as more specialized devices like Cisco ASA and Juniper network devices, and additionally to manage cloud based resources. Tools like Chef are becoming increasingly popular for managing computer infrastructure nowadays, and its rivals are Puppet and Ansible, as well as numerous others. Below is a quick tutorial on how to install chef server, configure it to to control a Windows node, and get Chef to add a specific file to be displayed when a user logs in to the computer. [1]

Glossary

Node – a client machine, i.e. machine to be configured. Recipe – a configuration element, used to define what to configure and how. Cookbook - a collection of recipes.

The workflow for getting a working Chef set up is:

  • Install the Chef server
  • Install the Chef Workstation
  • Install the Chef clients on machines to be controlled


This article assumes that the reader is experienced with the command line of Linux.

Install the Chef server

At the moment, the Chef server can be installed only 64-bit Linux machines, as well as on Amazon Web Services and Microsoft Azure, which are outside the scope of this article. For the purposes of this article, the Chef server will be installed on an machine running Ubuntu. The standalone installation of Chef server creates a working installation on a single server. This installation is also useful when you are installing Chef server in a virtual machine, for proof-of-concept deployments, or as a part of a development or testing loop.

To install Chef server 12:

Download the package from http://downloads.chef.io/chef-server/.

Upload the package to the machine that will run the Chef server, and then record its location on the file system. The rest of these steps assume this location is in the /tmp directory.

As a root user, install the Chef server package on the server, using the name of the package provided by Chef.

dpkg -i /tmp/chef-server-core-<version>.deb

After a few minutes, the Chef server will be installed.

Run the following to start all of the services:

chef-server-ctl reconfigure

Because the Chef server is composed of many different services that work together to create a functioning system, this step may take a few minutes to complete.

Run the following command to create an administrator:

chef-server-ctl user-create USER_NAME FIRST_NAME LAST_NAME EMAIL 'PASSWORD' --filename FILE_NAME

An RSA private key is generated automatically. This is the user’s private key and should be saved to a safe location. The --filename option will save the RSA private key to a specified path.

For example:

chef-server-ctl user-create tonymctony Tony McTony tony@mctony.ee 'hodor' --filename /path/to/tonymctony.pem

Run the following command to create an organization:

chef-server-ctl org-create short_name 'full_organization_name' --association_user user_name --filename ORGANIZATION-validator.pem

The name must begin with a lower-case letter or digit, may only contain lower-case letters, digits, hyphens, and underscores, and must be between 1 and 255 characters. For example: familyinc.

The full name must begin with a non-white space character and must be between 1 and 1023 characters. For example: 'Family, Inc.'.

The --association_user option will associate the user_name with the admins security group on the Chef server.

An RSA private key is generated automatically. This is the chef-validator key and should be saved to a safe location. The --filename option will save the RSA private key to a specified path.

For example:

chef-server-ctl org-create familyinc 'Family, Inc.' --association_user tonymctony --filename /path/to/familyinc-validator.pem[2]

Install the Chef Workstation

Now that our Chef server is up and running, our next action is to configure a workstation. The actual infrastructure coordination and configuration does not take place on the Chef server. This work is done on a workstation which then uploads the data to the server to influence the Chef environment. Once again, we assume that the machine in question is running Ubuntu.

Clone the Chef Repo

The Chef configuration for your infrastructure is maintained in a hierarchical file structure known collectively as a Chef repo. The general structure of this can be found in a GitHub repository provided by the Chef team. We will use git to clone this repo onto our workstation to work as a basis for our infrastructure's Chef repository.

First, we need to install git through the apt packaging tools. Update your packaging index and install the tool by typing:

sudo apt-get update

sudo apt-get install git

Once you have git installed, you can clone the Chef repository onto your machine. For this guide, we will simply clone it to our home directory:

cd ~

git clone https://github.com/chef/chef-repo.git

This will pull down the basic Chef repo structure into a directory called chef-repo in your home directory. Then, download the actual Chef Development Kit:

1) Visit this page: http://downloads.chef.io/chef-dk/.

2) Select a platform, and then a package.

3) Click the download button.

4) Follow the steps in the installer and install the Chef development kit to your machine. The Chef development kit is installed to /opt/chefdk/ on UNIX and Linux systems.

5) When finished, open a command window and enter the following:

chef verify This will verify the main components of the Chef development kit: the chef-client, the Chef development kit library, and the tools that are built into the Chef development kit. The output should be similar to:

Verification of component '...' succeeded.

Download the Authentication Keys to the Workstation

At this point, your workstation has all of the software needed to interact with a Chef server and compose infrastructure configurations. However, it is not yet configured to interact with your Chef server and your environment. In this section, we'll download the credentials we created on the Chef server.

We will use the scp utility to download the user key and the organization validator key that we created on the Chef server. Before doing so, we will create the hidden directory where we will store these files:

mkdir ~/chef-repo/.chef

The method that you use to connect to the Chef server will determine how exactly we go about downloading the keys. Follow the method below that matches your setup:

If you connect to your Chef server through SSH using password-based authentication, the scp command will work without significant modification.

On your workstation, specify the username and domain name or IP address used to connect to the Chef server. Follow this immediately with a colon (:) and the path to the file you wish to download. After adding a space, indicate the directory on the local computer where you wish the download the files to be placed (~/chef-repo/.chef in our case):

scp username@server_domain_or_IP:/path/to/tonymctony.pem ~/chef-repo/.chef

scp username@server_domain_or_IP:/path/to/familyinc-validator.pem ~/chef-repo/.chef

Configuring Knife to Manage your Chef Environment

Now that you have your Chef credentials available on your workstation, we can configure the knife command with the information it needs to connect to and control your Chef infrastructure. This is done through a knife.rb file that we will place in the ~/chef-repo/.chef directory along with our keys.

Open up a file called knife.rb in that directory in your text editor:

nano ~/chef-repo/.chef/knife.rb In this file, paste the following information:

current_dir = File.dirname(__FILE__)

log_level                :info

log_location             STDOUT

node_name                "name_for_workstation"

client_key               "#{current_dir}/tonymctony.pem"

validation_client_name   "familyinc-validator"

validation_key           "#{current_dir}/familyinc-validator.pem"

chef_server_url          "https://server_domain_or_IP/organizations/familyinc"

syntax_check_cache_path  "#{ENV['HOME']}/.chef/syntaxcache"

cookbook_path            ["#{current_dir}/../cookbooks"]

Testing

Before we can begin testing this, we need to fetch the SSL key from the server. This is done with the following command:

knife ssl fetch

After the SSL certificate has been fetched, the following command should now work:

knife client list

With the output being:

familyinc-validator

If the above command correctly returns, your workstation is now set up to control your Chef environment.[3]

Install Chef client

A Microsoft Installer Package (MSI) is available for installing the chef-client on a Microsoft Windows machine.

To install the chef-client on Microsoft Windows, do the following:

Go to http://www.chef.io/chef/install.

Click the Chef Client tab.

Select Windows, a version, and an architecture.

Under Downloads, select the version of the chef-client to download, and then click the link that appears below to download the package.

Ensure that the MSI is on the target node.

Run the MSI package and use all the default options, clicking "Next" at each installation step.

Run Chef Client as a service

To run the chef-client at periodic intervals (so that it can check in with the Chef server automatically), configure the chef-client to run as a service or as a scheduled task. This can be done via the MSI, by selecting the Chef Client Service option on the Custom Setup page or by running the following command in the command prompt after the chef-client is installed:

chef-service-manager -a install

and then start the chef-client as a service:

chef-service-manager -a start

After the chef-client is configured to run as a service, the default file path is: c:\chef\chef-client.log [4]

Adding a specific file to to a computer

Now we'll proceed to add a simple notepad file to be displaying when a user logs in to the computer. Some prior knowledge is required: files and applications to be launched automatically are placed in the startup folder of Windows, with the typical location being C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp. So we will simply instruct Chef to put a simple text file in that location, with the contents This computer is now managed by Chef.

Create Cookbook

First, we will instruct the Chef Workstation to create a new Cookbook:

knife cookbook create fileToStartup

Then browse to the created cookbook recipes folder:

cd ~/chef-repo/fileToStartup/recipes

Listing the contents of this folder reveals a default recipe:

ls

default.rb

Replace the contents of that file with the following:

file 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\notification.txt' do
  content 'This computer is now managed by Chef'
end

To elaborate on what is prescribed above: we set the file location and using the content section fill the file with the specified string. If the file does not exist, it will be created automatically. If it exists, but has a different content, it's content will be overwritten. [5]

Upload Cookbook

Now that our basic cookbook is complete, we can upload it to our chef server.

We can do that by typing:

knife cookbook upload fileToStartup

Or, we can upload everything by typing:

knife cookbook upload -a

Now, we can modify the run-list of our nodes. We can do this easily by typing:

knife node edit name_of_node [6]

Conclusion

That's it! The next time the Chef client checks for changes from the Chef server, it will see that a new cookbook has been assigned to it, and will apply the necessary changes. While the procedure described here is certainly more lengthy than simply putting the file on the machine manually, this was just an example - Chef can be used to manage applications, specific machine settings, scheduled tasks, and many other infrastructure configuration items. Plus, it scales well to a large number of managed machines, both physical and virtual.

Metadata

Author: Anton Zolotarjov

Group: CSE 15

Date: 2016-06-12

References