How to setup a Cookbook to configure Apache Webserver
Here in this article we will try to setup a very basic Chef Cookbook to install Apache HTTPD service and validate it.
Test Environment
Fedora Server
Chef DK
What is Cookbook
A cookbook is the fundamental unit of configuration and policy distribution in Chef Infra. A cookbook defines a scenario and contains everything that is required to support that scenario.
Procedure
Step1: Generate a Cookbook
As a first step let’s try to generate a template cookbook named “apache_httpd” as shown below.
[user@host-1 chefspace]$ mkdir cookbooks
[user@host-1 chefspace]$ cd cookbooks/
[user@host-1 cookbooks]$ chef generate cookbook apache_httpd
Generating cookbook apache_httpd
- Ensuring correct cookbook file content
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
Your cookbook is ready. Type `cd apache_httpd` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.
Why not start by writing a test? Tests for the default recipe are stored at:
test/recipes/default_test.rb
If you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb
Here is the tree structure of the cookbook that we just generated.
[user@host-1 cookbooks]$ tree apache_httpd/
apache_httpd/
|-- Berksfile
|-- chefignore
|-- metadata.rb
|-- README.md
|-- recipes
| `-- default.rb
|-- spec
| |-- spec_helper.rb
| `-- unit
| `-- recipes
| `-- default_spec.rb
`-- test
`-- recipes
`-- default_test.rb
6 directories, 8 files
Step2: Generate template for HTML file
Now let’s create an html file template for the cookbook that we just created as shown below.
[user@host-1 cookbooks]$ chef generate template apache_httpd index.html
Recipe: code_generator::template
* directory[./apache_httpd/templates/default] action create
- create new directory ./apache_httpd/templates/default
- restore selinux security context
* template[./apache_httpd/templates/index.html.erb] action create
- create new file ./apache_httpd/templates/index.html.erb
- update content in file ./apache_httpd/templates/index.html.erb from none to e3b0c4
(diff output suppressed by config)
- restore selinux security context
Step3: Update HTML template file
Here we are going to update the template html file “index.html.erb” with some content as shown below.
[user@host-1 templates]$ pwd
/home/user/chefspace/cookbooks/apache_httpd/templates
[user@host-1 templates]$ vi index.html.erb
[user@host-1 templates]$ cat index.html.erb
<html>
<head>
<title> This is simple webpage </title>
</head>
<body>
<h1> This is a grooming website from Cookbook </h1>
</body>
</html>
Step4: Update recipe file
Here we are going to update the default reciple file “default.rb” to install apache httpd service and copy the template html file that was created.
[user@host-1 recipes]$ pwd
/home/user/chefspace/cookbooks/apache_httpd/recipes
[user@host-1 recipes]$ cat default.rb
#
# Cookbook Name:: apache_httpd
# Recipe:: default
#
# Copyright (c) 2017 The Authors, All Rights Reserved.
package 'Install Apache' do
case node[:platform]
when 'redhat','centos'
package_name 'httpd'
action :install
when 'ubuntu','debian'
package_name 'apache2'
action :install
end
end
service 'httpd' do
action [:enable, :start]
end
template '/var/www/html/index.html' do
source 'index.html.erb'
end
Step5: Run the Cookbook using chef-client
Now we are ready to execute our cookbook to setup our apache httpd service as shown below.
[user@host-1 recipes]$ sudo chef-client --local-mode --runlist 'recipe[apache_httpd]'
[2017-06-03T03:04:30-07:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: ["apache_httpd"]
Synchronizing Cookbooks:
- apache_httpd (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 3 resources
Recipe: apache_httpd::default
* yum_package[Install Apache] action install (up to date)
* service[httpd] action enable (up to date)
* service[httpd] action start (up to date)
* template[/var/www/html/index.html] action create
- update content in file /var/www/html/index.html from eea344 to 5ff101
--- /var/www/html/index.html 2017-06-02 20:05:07.795943412 -0700
+++ /var/www/html/.chef-index.html20170603-3853-wyjz1z 2017-06-03 03:05:11.707685428 -0700
@@ -3,7 +3,7 @@
<title> This is simple webpage </title>
</head>
<body>
- <h1> This is a grooming website </h1>
+ <h1> This is a grooming website from Cookbook </h1>
</body>
</html>
- restore selinux security context
Running handlers:
Running handlers complete
Chef Client finished, 1/4 resources updated in 41 seconds
Step6: Validate HTTPD service
Now let’s try to access our html content that we just deployed as a part of cookbook execution as shown below.
[user@host-1 recipes]$ curl http://localhost/index.html
<html>
<head>
<title> This is simple webpage </title>
</head>
<body>
<h1> This is a grooming website from Cookbook </h1>
</body>
</html>
Hope you enjoyed reading this article. Thank you..
Leave a Reply
You must be logged in to post a comment.