Chef - Testing cookbooks

Chef is a powerful configuration management tool that turns infrastructure into code. With the help of Chef, Users can easily manage, configure and deploy the resources across the network from the central location of the environment (cloud, on-premises, or hybrid).

Write a test Chef Cookbook

Let's generate a cookbook for motd,

#chef generate cookbook motd 

Generating cookbook motd

- Ensuring correct cookbook file content

- Committing cookbook files to git

- Ensuring delivery configuration

- Ensuring correct delivery build cookbook content

- Adding delivery configuration to feature branch

- Adding build cookbook to feature branch

- Merging delivery content feature branch to master

Your cookbook is ready. Type `cd motd` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.

test/smoke/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

Navigate into the recipes directory & edit the default.rb file as shown below:

#ls

chefignore motd starter

#cd motd/

# ls

Berksfile chefignore LICENSE metadata.rb README.md recipes spec test

#cd recipes/

#ls

default.rb

#cat default.rb

template "/etc/motd" do

source "motd.erb"

mode "0644"

end

create a directory for templates and attributes.

#cd ..

#ls

Berksfile chefignore LICENSE metadata.rb README.md recipes spec test

#mkdir templates

#mkdir attributes

Inside the template directory, create default directory along with the motd.erb file and add the following content as follows

#cd templates

#mkdir default

#cd default/

#cat motd.erb

This is my MOTD created by <%= node["motd"]["author"] %>

Now move to the attributes directory & create a default.rb file with the following content:

# ls

attributes Berksfile chefignore LICENSE metadata.rb README.md recipes spec templates test

#cd attributes/

#cat default.rb

default["motd"]["author"] = "zippyops"

The cookbook can be uploaded from anywhere within the cookbooks directory & execute the run_list command to add the cookbook to the run list.

Syntax: knife node run_list add linuxnode ‘recipe [recipe_name]'

# knife cookbook upload motd

Uploading motd [0.1.0]

Uploaded 1 cookbook.

# knife node run_list add centosnode 'recipe[motd]' 

Centosnode:

run_list: recipe[motd]

click on the “Policy” tab & navigate to the “content” option which are shown below

Recent Comments

No comments

Leave a Comment