Chef : Writing cookbook to configure apache webserver

Share At:

Chef is infrastructure automation tool which can be used to build infra as code. This is quite useful tool which lets you to do numerous tasks simultaneously on hundreds of servers with just few commands…e.g. configuring application , install packages, create users…etc and much more…

Here, we’ll see how to write cookbook to configure apache server on centos and upload the cookbook on “Chef Server”.

We will also learn how to bootstrap client server and configure apache on client server.

Pre-Requisites:

  1. You should have a chef account created(go to https://www.chef.io/ and sign up for the account if you don’t have it).
  2. You have already downloaded “Chef Starter kit” from chef website
  3. You have already installed “Chef development kit” on chef workstation
  4. latest version of chef Development Kit can be downloaded for centos7 from below url:

wget https://packages.chef.io/files/stable/chefdk/4.13.3/el/7/chefdk-4.13.3-1.el7.x86_64.rpm.

  1. Generate a apache cookbook:

2. Now you will see a directory called apache. Please go inside apache/recipes:

3. Open the default.rb file in your favourite test editor and update it with below contents:

4. Now cd to “apache” directory once again and create “files/default” directories. Chef will look for index.html in this location.

5. Create index.html with below contents:

6. Upload the cookbook to Chef Server using Knife utility:

7. Now visit the Chef server portal and you will see your cookbook listed there

8. Here you can see the content of your recipe:

Your cookbook is ready to use.

Bootstrapping chef client:

Bootstrapping chef client from chef workstation would install chef client package in the chef client server and also list the chef client in the chef server.

command:

knife bootstrap 192.168.33.21 — sudo -U vagrant -N chefnode1

where 192.168.33.21 is the client node ip (chefnode1) in our case.

Showing Client node Ip
Image showing bootstrapping the client node from chef workstation

You will see chef node entry similar to this in the chef server:

Now add the runlist “apache” to the chef client — “chefnode1” .

command: knife node run_list add chefnode1 “recipe[apache]”

Now go to the chef client — chefnode1 and run below command:

Verify that httpd is installed and running in the chefnode1:

Now open your browser and type the ip of chefnode1, you will see the “Hello World” webpage.

Well, you have just created a webpage.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us