Remove Policyfile.rb from your Chef cookbook

When you test your cookbook in Chef Test Kitchen and get the following error, delete the “Policyfile.rb” from your Chef cookbook directory.

$$$$$$ You must set your run_list in your policyfile instead of kitchen config. The run_list your config will be ignored.
$$$$$$ Ignored run_list: ["recipe[...::...]"]
       Preparing dna.json
       Exporting cookbook dependencies from Policyfile /tmp/...
       Error: Invalid lockfile data
       Reason: (ChefDK::DependencyConflict) Cookbook ... (...) has dependency constraints that cannot be met by the existing cookbook set:
       Cookbook ... isn't included in the existing cookbook set.

Unable to satisfy the following requirements error message in Chef Test Kitchen

When Berkshelf in the Chef Test Kitchen cannot resolve the cookbook dependencies, it displays the following error message during converge:

Unable to satisfy the following requirements

To help Berks to start a fresh calculation, delete the Berksfile.lock file and enter the following into the command window:

berks install

DevOps Engineering part 2. – Set up the DevOps development environment

In the first part of the series, Beginner’s Guide to DevOps Engineering Part 1. we have already installed the DevOps development tools.

Create and test your first cookbook in 5 minutes

 Set up the Chef working folder

  • Create a folder for the Chef development(C:\Chef)
  • In the Chef folder create a sub-folder cookbooks
  • Double click the cookbooks folder to navigate into it
  • In Windows Explorer right click the white area and select Git Bash Here

Create your first cookbook

  • Enter the following command into the Bash window to create a new cookbook
    chef generate cookbook test
  • In Windows Explorer navigate to the test\recipes folder
  • Double click the default.rb file and select Select a program from a list of installed programs.
    associate-01-rb-with-notepad
  • Click the Browse… button and navigate to C:\Program Files (x86)\Notepad++\notepad++.exe
    associate-02-rb-with-notepad
  • Enter the following to create your first recipe
    file 'C:\\Windows\\Temp\helloworld.txt' do
     content 'Hello world'
    end

     

  • Save the file
  • In the bash Window navigate to the test cookbook directory
    cd test
  • Execute your first recipe with the following in the Bash window
    chef-client --local-mode recipes/default.rb
  • Navigate to the C:\Windows\Temp directory and open the helloworld.txt file to see the result of your script.

Test your cookbook on a virtual machine

We will use Test Kitchen and Vagrant to launch virtual machines. Currently only Linux images are available, so we will modify our recipe to distinguish between Linux and Windows and act accordingly.

Open the default.rb recipe and update it to look like this

case node['os']
when 'linux'
  file "/tmp/helloworld.txt" do
    content 'This file was created by Chef!'
  end
when 'windows'
  file "C:\\Chef\\helloworld.txt" do
    content 'This file was created by Chef!'
  end
end

Enter the following commands into the Bash window

  • Display the available test configurations ( suites )
    kitchen list
  • Launch a virtual Linux machine and test your recipe
    kitchen converge ubuntu

    The first time you launch a virtual machine with an operating system you have never used on your workstation, Vagrant has to download the machine image from the Internet. It can take 5-10 minutes. When the virtual machine is created and launched, Test Kitchen will copy your cookbook to it and execute it there.
  • Test kitchen will display the —–> Kitchen is finished message when the cookbook has successfully executed.
  • Log into the virtual machine
    kichen login ubuntu
  • The root prompt appears in the Bash windows. Let’s check if the file has been created or not.
  • To destroy the virtual machine
    kitchen destroy ubuntu

Windows in Vagrant

To test your cookbook on a Windows virtual machine locally, create one for Vagrant. See Launch Windows instances locally with Chef Test Kitchen for the details.

Learn the basics, so you can ask questions

Chef has a steep learning curve. Chef is not just scripting or programming, but you have to understand how Chef works to be able to use it to configure servers. There are many ways to do the same thing and there is not much documentation to recommend the best way. If you search Google, the problems usually have multiple solutions, and many times the “best” answer is selected based on personal preference. To get started, you should familiarize yourself with the tools, because you will use most of them during the development process.

In this guide I will use a Windows computer as a workstation, but all tools work on Mac and Linux computers.

Vagrant

Learn Vagrant to understand how Test Kitchen manages the test servers on your local machine or at AWS. You will not use vagrant directly, but Test Kitchen uses it to launch servers.

Chef

Test Kitchen

Ruby

Terraform


Next:

Working with AWS in Beginner’s Guide to DevOps Engineering part 3.

Back:

to the Tutorials page

SSL certificate is missing after installing ChefDK

When you install a new version of ChefDK and try to run Test Kitchen converge you may get the following error message
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: Failed to complete #create action: [SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

To solve the problem add the following to the Windows environment variables and open a new Command or Bash window for the change to take effect:
SSL_CERT_FILE=C:\opscode\chefdk\embedded\ssl\certs\cacert.pem

Winrm is missing after installing ChefDK

When you install the new version of ChefDK you may get the following error message when you try to run Test Kitchen
!!!!!! The `winrm` gem is missing and must be installed or cannot be properly activated. Run `gem install winrm --version '["~> 1.6"]'` or add the following to your Gemfile if you are using Bundler: `gem 'winrm ', '["~> 1.6"]'`.

To install Winrm for the correct Ruby version execute
gem install winrm
gem install winrm-fs

Berkshelf is missing after installing ChefDK

When you install a new version of ChefDK and try to run Test Kitchen the following error message may appear
!!!!!! The `berkshelf' gem is missing and must be installed or cannot be properly activated. Run `gem install berkshelf` or add the following to your Gemfile if you are using Bundler: `gem 'berkshelf'`.

To reinstall Brekshelf in the correct version of Ruby execute
gem install berkshelf

EC2 driver is missing after installing ChefDK

After installing a new version of ChefDK you may get the following error message when you try to run Test Kitchen:

Message: Could not load the 'ec2' driver from the load path. Please ensure that your driver is installed as a gem or included in your Gemfile if using Bundler.

To install the EC2 driver in the correct Ruby version execute

gem install kitchen-ec2

Error in Test Kitchen after installing ChefDK

You may get the following error when you try to run Test Kitchen after installing the latest version of ChefDK:

$ kitchen
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- dl/import (LoadError)
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh/authentication/pageant.rb:1:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh/authentication/agent/socket.rb:5:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh/authentication/agent.rb:22:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh/authentication/key_manager.rb:4:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh/authentication/session.rb:4:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/net-ssh-2.9.2/lib/net/ssh.rb:11:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/test-kitchen-1.7.3/lib/kitchen/ssh.rb:20:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/test-kitchen-1.7.3/lib/kitchen.rb:46:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/test-kitchen-1.7.3/lib/kitchen/cli.rb:21:in `<top (required)>'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/test-kitchen-1.7.3/bin/kitchen:10:in `<top (required)>'
from C:/Ruby22/bin/kitchen:23:in `load'
from C:/Ruby22/bin/kitchen:23:in `<main>'

To make Test Kitchen work again execute the following:

gem install net-ssh -v 2.9.3.beta1