My First CHEF Cookbook !

[The cookbook works only for debian based OS]

Login To Chef Workstation:

ssh -i key.pem ubuntu@chef-workstation

Chef Workstation, @chef-repo directory
Creating My Apache Cookbook

#knife cookbook create myapache
#vim cookbooks/myapache/recipes/default.rb

package "apache2"  do
        action :install
end

#To delete default VirtualHost
cookbook_file "/etc/apache2/sites-enabled/000-default" do
  action :delete
end

template "/etc/apache2/sites-enabled/default" do
        source "default.erb"
        mode "0644"
end

#To delete default index.html
cookbook_file "/var/www/index.html" do
  action :delete
end

cookbook_file "/var/www/index.html" do
  action :create
end

service "apache2" do
        action [:enable,:start,:reload]
end

Back To Terminal

#echo "This is myapache cookbook" > cookbooks/myapache/files/default/index.html
#vim cookbooks/myapache/templates/default/default.erb
<VirtualHost *:80>
DocumentRoot /var/www/
</VirtualHost>

Back to Terminal, good to practice to update the cookbook version

#vim cookbooks/myapache/metadata.rb
version '0.1.1'

Upload the Cookbook to Chef-Server:

#knife cookbook upload myapache

Go To Chef Manage Console to 

  • Create a New Role called testrole2
  • Place the myapache cookbook in RunList of testrole2

Starting a new node to try out!

#knife ec2 server create -r "role[testrole2]" -I ami-8e987ef9 --flavor t1.micro -G unni-test-sg -x ubuntu -N MyChefPOC --region eu-west-1 -i /pathtosshkeyofNode.pem
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s