How to use Chef Executables

How to use Chef Executables

Chef Executable

  • Generates applications, cookbooks, recipes, attributes, templates, files and custom resources
  • Ensures RubyGems are downloaded properly for ChefDk environment
  • Verifies all components are installed and configured correctly


Pre-requisites

Linux machine with ChefDk installed

Below are some useful chef commands

  1. Checking the chef environment variables that are set for chefDK, RubyGems, Ruby and PATH variables for the same


[skytap@host-1 ~]$ chef env

Chef Development Kit:

ChefDK:

ChefDK Version: 0.16.28

ChefDK Home: “/home/skytap/.chefdk”

ChefDK Install Directory: “/opt/chefdk”

Policyfile Config:

Cache Path: “/home/skytap/.chefdk/cache”

Storage Path: “/home/skytap/.chefdk/cache/cookbooks”

Ruby:

Ruby Executable: “/opt/chefdk/embedded/bin/ruby”

Ruby Version: 2.1.8

RubyGems:

RubyGems Version: 2.6.6

RubyGems Platforms:

– ruby

– x86_64-linux

Gem Environment:

GEM ROOT: “/opt/chefdk/embedded/lib/ruby/gems/2.1.0”

GEM HOME: “/home/skytap/.chefdk/gem/ruby/2.1.0”

GEM PATHS:

– “/home/skytap/.chefdk/gem/ruby/2.1.0”

– “/opt/chefdk/embedded/lib/ruby/gems/2.1.0”

Path:

– “/opt/chefdk/bin”

– “/home/skytap/.chefdk/gem/ruby/2.1.0/bin”

– “/opt/chefdk/embedded/bin”

– “/usr/local/bin”

– “/usr/local/sbin”

– “/usr/bin”

– “/usr/sbin”

– “/bin”

– “/sbin”

– “/home/skytap/.local/bin”

– “/home/skytap/bin”

– “/opt/chefdk/gitbin”


  1. Executing shell commands using chef


[skytap@host-1 ~]$ chef exec hostname

host-1

[skytap@host-1 ~]$ chef exec date +%d%m%y_%T

110617_22:06:23


  1. Installing gem files and ruby applications which are not part of ChefDK environment using chef gem command


[skytap@host-1 ~]$ chef gem install knife-config

Fetching: knife-config-1.1.0.gem (100%)

WARNING: You don’t have /home/skytap/.chefdk/gem/ruby/2.1.0/bin in your PATH,

gem executables will not run.

Successfully installed knife-config-1.1.0

1 gem installed


  1. Generate application structure which can support multiple cookbooks within it


[skytap@host-1 ~]$ chef generate app chefspace

[skytap@host-1 ~]$ tree chefspace

chefspace

|– cookbooks

| `– chefspace

| |– Berksfile

| |– chefignore

| |– metadata.rb

| |– recipes

| | `– default.rb

| `– spec

| |– spec_helper.rb

| `– unit

| `– recipes

| `– default_spec.rb

|– README.md

`– test

`– recipes

`– default_test.rb


8 directories, 8 files


  1. Create an attributes file within cookbook


[skytap@host-1 chefspace]$ chef generate attribute cookbooks/chefspace attributes

Recipe: code_generator::attribute

* directory[cookbooks/chefspace/attributes] action create

– create new directory cookbooks/chefspace/attributes

– restore selinux security context

* template[cookbooks/chefspace/attributes/attributes.rb] action create

– create new file cookbooks/chefspace/attributes/attributes.rb

– update content in file cookbooks/chefspace/attributes/attributes.rb from none to e3b0c4

(diff output suppressed by config)

– restore selinux security context



  1. Generate an addition cookbook with the chef application


[skytap@host-1 chefspace]$ chef generate cookbook cookbooks/testcookbook

[skytap@host-1 chefspace]$ tree cookbooks/

cookbooks/

|– chefspace

| |– attributes

| | `– attributes.rb

| |– Berksfile

| |– chefignore

| |– metadata.rb

| |– recipes

| | `– default.rb

| `– spec

| |– spec_helper.rb

| `– unit

| `– recipes

| `– default_spec.rb

`– testcookbook

|– Berksfile

|– chefignore

|– metadata.rb

|– README.md

|– recipes

| `– default.rb

|– spec

| |– spec_helper.rb

| `– unit

| `– recipes

| `– default_spec.rb

`– test

`– recipes

`– default_test.rb


13 directories, 15 files



  1. Generating lightweight resource and provider (LWRP)


[skytap@host-1 chefspace]$ chef generate lwrp cookbooks/testcookbook lwrp_test

Recipe: code_generator::lwrp

* directory[cookbooks/testcookbook/resources] action create

– create new directory cookbooks/testcookbook/resources

– restore selinux security context

* template[cookbooks/testcookbook/resources/lwrp_test.rb] action create

– create new file cookbooks/testcookbook/resources/lwrp_test.rb

– update content in file cookbooks/testcookbook/resources/lwrp_test.rb from none to e3b0c4

(diff output suppressed by config)

– restore selinux security context

* directory[cookbooks/testcookbook/providers] action create

– create new directory cookbooks/testcookbook/providers

– restore selinux security context

* template[cookbooks/testcookbook/providers/lwrp_test.rb] action create

– create new file cookbooks/testcookbook/providers/lwrp_test.rb

– update content in file cookbooks/testcookbook/providers/lwrp_test.rb from none to e3b0c4

(diff output suppressed by config)

– restore selinux security context


  1. Generating recipe for testcookbook


[skytap@host-1 chefspace]$ chef generate recipe cookbooks/testcookbook test_recipe

Recipe: code_generator::recipe

* directory[cookbooks/testcookbook/spec/unit/recipes] action create (up to date)

* cookbook_file[cookbooks/testcookbook/spec/spec_helper.rb] action create_if_missing (up to date)

* template[cookbooks/testcookbook/spec/unit/recipes/test_recipe_spec.rb] action create_if_missing

– create new file cookbooks/testcookbook/spec/unit/recipes/test_recipe_spec.rb

– update content in file cookbooks/testcookbook/spec/unit/recipes/test_recipe_spec.rb from none to 9a46cc

(diff output suppressed by config)

– restore selinux security context

* directory[cookbooks/testcookbook/test/recipes] action create (up to date)

* template[cookbooks/testcookbook/test/recipes/test_recipe.rb] action create_if_missing

– create new file cookbooks/testcookbook/test/recipes/test_recipe.rb

– update content in file cookbooks/testcookbook/test/recipes/test_recipe.rb from none to 10ae17

(diff output suppressed by config)

– restore selinux security context

* template[cookbooks/testcookbook/recipes/test_recipe.rb] action create

– create new file cookbooks/testcookbook/recipes/test_recipe.rb

– update content in file cookbooks/testcookbook/recipes/test_recipe.rb from none to 63aecc

(diff output suppressed by config)

– restore selinux security context


  1. Generating template files for testcookbook


[skytap@host-1 chefspace]$ chef generate template cookbooks/testcookbook test_template.txt

Recipe: code_generator::template

* directory[cookbooks/testcookbook/templates/default] action create

– create new directory cookbooks/testcookbook/templates/default

– restore selinux security context

* template[cookbooks/testcookbook/templates/test_template.txt.erb] action create

– create new file cookbooks/testcookbook/templates/test_template.txt.erb

– update content in file cookbooks/testcookbook/templates/test_template.txt.erb from none to e3b0c4

(diff output suppressed by config)

– restore selinux security context


For more information visit here.

Hope you enjoyed reading this article. Thank you.