Basics
Basic Concepts for using Chef
Updated: 03 September 2023
Basics of Chef
Note About Environment
I am running a VM with access to a Shared Drive using VirtualBox, this can be found at root/media/sf_name
on the Ubuntu VM
Set Up a Docker Container to Manage
We’ll make use of a Docker container with Ubuntu to work with Chef - generally though Docker containers are treated as immutable infrastructure
Before starting we will need to ensure that Docker is installed on our system so that we can run chef in the container
Make a Working Directory
Make a new directory in which we can work
Start the Docker Container
Download the Ubuntu 14.04 image from Docker hub and start the container
From the shared learn-chef
directory, run the following command
Running the container as above will expose also give our docker container access to our system ~ /chef-repo
directory so that we can edit our chef code directly from there
From the Container
Update the container package list and install curl
Install Chef in Container
Next we can run Chef Workstation as follows:
Set Up the Working Directory
From the container cd
into the ~/chef-repo
directory that we initialized previously
In this directory (the same as your local learn-chef
) directory, create the initial MOTD file by using chef-client
in local mode (usually chef-client
will download the latest code from a server though)
Inside of the chef-repo
directory create a file called hello.rb
with the following content
And then run the above file with the following command
This will create a new file /tmp/motd
which contains the text hello world
We can view the contents of this file using more
or cat
If we run the chef-client
command again we will see that no resources were updated
We can update the hello.rb
file contents to contain the following
And then update the resource with
And we will see that the file was updated with the following
If we manually change the /tmp/motd
file, running chef-client
will restore the correct configuration
You can test this by running the following command to modify the file
And then having chef restore it
Chef endsures that the actual state of a resource matches the state that was specified, even if it is altered by an external resource. Usually we configure chef-client
to run periodically or as part of a continuous automation system which helps our resources be correctly configured
Delete MOTD file
Create a file called goodbye.rb
with the following contents
Then use the chef-client
to run it
This will give us the following output
Summary
Resources describe the what, not the how. A recipe is a file that describes what state a part of the system should be in, but not how to get there - that is handled by Chef
Resources have actions, such as :delete
which is a process by which a desired state is reached. Every resource has a default action, such as create a file or install a package. :create
is the defult action for a file
resource
Recipes are an ordered list of configuration states and typically contain related states
Configure a Package and Service
Packages and services, like files, are also resource types
For this portion we will be managing an Apache HTTP Server Package and its associated Service
Update Apt Cache
We can run the apt-get update
command manually every time we bring up an instance, but chef provides us with an apt_update
resource to automate the process
Chef allows us to periodically carry out a specific task, in this case we can update our apt cache every 24 hours (86 400 seconds)
In the chef-repo
directory create a webserver.rb
file with the instructions to periodically update the cache as follows
Instead of :periodic
we can also use the :update
action to update each time chef runs
Install the Apache Package
Next we can install the apache2
package, modify the webserver.rb
package to do this
We don’t need to specify the :install
action as this is the default
Now run the recipe with
Typically (if not the root
user) we need to run Chef with sudo
Start and Enable the Apache Service
Update the webserver.rb
file to enable the Apache service when the server boots and then start the service, this is one by way of the action
list given in which the following actions on a resource will be carried out
Now re-run the recipe in order to start the service
Add a Home Page
We can use the file
resource to create a homepage for our site at /var/www/html/index.html
with a basic hello world message. This can be added to the webserver.rb
recipe as follows
And we can run chef-client
to apply it
If we do not see any errors we can continue and make an HTTP request with curl
inside the container, making a curl
to localhost
will by default hit port 80
, we can do this from the container as follows
Or
Furthermore we can view this on the host machine’s browser due to the port forwarding we initially set up for the container -p 8100:80
on which maps port 80
on the container to 8100
on the host. We can do this simply by visiting localhost:8100
from the host or making an HTTP request from the terminal
Summary
Chef allows us to automate and configure multiple resource types as well as carry out tasks periodically, manage installed packages, and specify actions for those packages
Making Recipes More Managable
The problem with the recipe we are currently using is that the HTML for the webpage was embedded in the recipe, this is not practical. In order to more easily reference external files we can make use of a Cookbook
Create a Cookbook
From the chef-repo
directory create a cookbooks
directory, in this run the use Chef to generate a Cookbook named learn_chef_apache2
The cookbooks/learn_chef_apache2
part tells chef to create a new Cookbook in the cookbooks
directory called learn_chef_apache2
Thereafter install tree
on the container so that we can view the directory structure and then look at the cookbooks
directory
The file structure can be seen to be:
The default recipe is in the recipes/default.rb
file, our recipe will be written in there
Create a Template
A new template file can be generated with the chef generate
command, generate a new template called index.html
as follows
Move the index.html
content we made previously to a template
file which will be added as templates/index.html.erb
into which we must add the following
We have added the content directly into the cookbook for the purpose of the tutorial, but realstically the application would be some set of build artifacts that will then be pulled from a build server to be deployed
Update the Recipe
Now update the recipe in the default.rb
file to once again update the apt cache, start the Apache Web Server, and reference the HTML template with the following
Run the Cookbook
chef-client
can be used to run the Cookbook, we will again use the --local-mode
flag and specify the required recipes with the --runlist
flag
Note the recipe[learn_chef_apache2]
which specifies that we want to run the learn_chef_apache2
’s default.rb
recipe. This is the same as recipe[learn_chef_apache2::default]
We can check that the file was updated with
And by visiting localhost:8100
on the Host