Saturday, October 8, 2016

First CHEF Solo Installation and 'Hello World'!

First CHEF Solo Installation and 'Hello World'!

Hello World! My First Cookbook.

CHEF is a platform of deployment automation. It is one of the important tools for DevOps engineers.
This is the first time I install Chef in my Mac, trying to understand how it works. Here are the installation processes I documented according to this tutorial.
Chef installation overview

Perequisites

First, make sure all listed below have been installed in my Mac.

Workstation installation

curl -L https://www.opscode.com/chef/install.sh | sudo bash

sudo gem install knife-solo

gem list

knife configure

Node Setup

Use Ubuntu virtual Box for virtual machine.
mkdir chef
cd chef
vagrant init ubuntu/trusty64
Edit Vagrantfile. Remove ‘#’ before the two lines below and save.
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.network :forwarded_port, guest: 80, host: 8080
Start the Vagrant virtual machine
vagrant up

SSH Setup

Official documentation
Extra reading
This step is to setup the host name of the virtual machine. It writes the host data into the ssh configuration file in the local machine (not the virtual machine).
vagrant ssh-config —host hogege >> ~/.ssh/config
Now we can use ssh hogege to connect to the vagrant virtual machine.
ssh hogege

Chef site-cookbooks creation

knife solo init chef-repo
cd chef-repo
knife solo prepare hogege
$ bootstrapping Chef...

Create cookbook

knife cookbook create hello -o site-cookbooks/

WARN: This command is being deprecated in favor of `chef generate cookbook` and will soon return an error.
Please use `chef generate cookbook` instead of this command.
 at /Users/chaoyee/.rvm/gems/ruby-2.3.1@global/gems/chef-12.14.89/lib/chef/knife.rb:430:in `block in run_with_pretty_exceptions'
** Creating cookbook hello in /Users/chaoyee/chef/chef-repo/sitecookbooks
** Creating README for cookbook: hello
** Creating CHANGELOG for cookbook: hello
** Creating metadata for cookbook: hello
Add the line below to /site-cookbooks/hello/recipts/default.rb file
log "Hello World"
Edit chef-repo/nodes/hogege.json
{
  “run_list”:[
    “recipe[hello]”
  ] 
}
Start cooking!
knife solo cook hogege
Result:
Recipe: hello::default
  * log[Hello World] action write
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 seconds
It shows the log has been written to the virtual machine ‘hogege’.
Finally, remember to shutdown the virtual machine by typing:
vagrant halt
:-)

1 comment: