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.
- 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).
- You have already downloaded “Chef Starter kit” from chef website
- You have already installed “Chef development kit” on chef workstation
- latest version of chef Development Kit can be downloaded for centos7 from below url:
- 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.
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.
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 !!!