Execute Cookbook on Node and run Chef-Client

Last updated on Nov 12 2021
Yogesh Ajmera

Table of Contents

Execute Cookbook on Node and run Chef-Client

Chef – Nodes

Knife preflight shows details about all the nodes which uses a certain cookbook before uploading it to Chef server.

Getting Started

In order to get started, we need to have knife-preflight gem installed.

Step 1 − Define the path in the gem file.
vipin@laptop:~/chef-repo $ subl Gemfile
source 'https://rubygems.org'
gem 'knife-preflight'
Step 2 − Run bundler to install knife-preflight gem.
vipin@laptop:~/chef-repo $ bundle install
Fetching gem metadata from https://rubygems.org/
...TRUNCATED OUTPUT...
Installing knife-preflight (0.1.6)

Working Method

Run knife-preflight on the given cookbook.
We can run the preflight command to find out which nodes and roles have the given cookbook in their expanded run lists.

vipin@laptop:~/chef-repo $ knife preflight ntp
Searching for nodes containing ntp OR ntp::default in their
expanded run_list...
2 Nodes found
www-staging.example.com
cms-staging.example.com
Searching for roles containing ntp OR ntp::default in their
expanded run_list...
3 Roles found
your_cms_role
your_www_role
your_app_role

Found 6 nodes and 3 roles using the specified search
criteria
There are multiple ways for a cookbook to get executed on the node.
• You can assign the cookbook directly to a node by adding it to the node’s run list.
• You can add a cookbook to the role and add the role to the node’s run list.
• You can add the role to the run list of another role and add that other role to the node’s run list.
• A cookbook can be a dependency of another used cookbook.
No matter how a cookbook ends up in a node’s run list, the knife preflight command will catch it as Chef stores all expanded lists of roles and recipes in node attributes. The knife preflight command issues a search for exactly those node attributes.

Chef – Chef-Client Run

In order to test Chef-Client run, we need to have Chef-Client configured to use the hosted Chef or own hosted server.

Running Chef-Client in Debug Mode

vipin@server:~$ sudo chef-client -l debug
…TRUNCATED OUTPUT…
Hashed Path:A+WOcvvGu160cBO7IFKLYPhh9fI=
X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=
X-Ops-Timestamp:2012-12-27T11:14:07Z
X-Ops-UserId:vagrant'
Header hash: {"X-Ops-Sign"=>"algorithm=sha1;version=1.0;",
"X-Ops-Userid"=>"vagrant", "X-Ops-Timestamp"=>"2012-12-
27T11:14:07Z", "X-Ops-Content-
Hash"=>"2jmj7l5rSw0yVb/vlWAYkK/YBwk=", "X-Ops-
Authorization-
1"=>"HQmTt9U/
LJJVAJXWtyOu3GW8FbybxAIKp4rhiw9O9O3wtGYVHyVGuoilWDao",
"X-Ops-Authorization-
2"=>"2/uUBPWX+YAN0g1/
fD2854QAU2aUcnSaVM0cPNNrldoOocmA0U5HXkBJTKok",
"X-Ops-Authorization-
3"=>"6EXPrEJg5T+
ddWd5qHAN6zMqYc3untb41t+eBpigGHPhtn1LLInMkPeIYwBm",
"X-Ops-Authorization-
4"=>"B0Fwbwz2HVP3wEsYdBGu7yOatq7fZBXHfIpeOi0kn/
Vn0P7HrucnOpONmMgU", "X-Ops-Authorization-
5"=>"RBmmbetFSKCYsdg2v2mW/
ifLIVemhsHyOQjffPYPpNIB3U2n7vji37NxRnBY",
"X-Ops-Authorization-
6"=>"Pb3VM7FmY60xKvWfZyahM8y8WVV9xPWsD1vngihjFw=="}
[2012-12-27T11:14:07+00:00] DEBUG: Sending HTTP Request via
GET to api.opscode.com:443/organizations/agilewebops/
nodes/vagrant
[2012-12-27T11:14:09+00:00] DEBUG: ---- HTTP Status and
Header Data: ----
[2012-12-27T11:14:09+00:00] DEBUG: HTTP 1.1 200 OK
[2012-12-27T11:14:09+00:00] DEBUG: server: nginx/1.0.5
[2012-12-27T11:14:09+00:00] DEBUG: date: Thu, 27 Dec 2012

Inspecting the Result of the Last Chef-Client Run

In order to check the last Chef-Client run especially failure issues when we are developing a new cookbook, we need to know what exactly went wrong. Even though Chef prints everything in stdout, one might want to see the debug log again.
If we want to test, we need to have a broken cookbook which is failing on compilation.

user@server:~$ sudo chef-client
==================================================================
==============
Recipe Compile Error in /srv/chef/file_store/cookbooks/my_
cookbook/recipes/default.rb
==================================================================
==============
NoMethodError
-------------
undefined method `each' for nil:NilClass
Cookbook Trace:
---------------
/srv/chef/file_store/cookbooks/my_cookbook/recipes/default.
rb:9:in `from_file'

Relevant File Content:

----------------------
/srv/chef/file_store/cookbooks/my_cookbook/recipes/default.rb:
2: # Cookbook Name:: my_cookbook
3: # Recipe:: default
4: #
5: # Copyright 2013, YOUR_COMPANY_NAME
6: #
7: # All rights reserved - Do Not Redistribute
8: #
9≫ nil.each {}
10:
For more details, we can look into the stacktrace.
user@server:~$ less /srv/chef/file_store/chef-stacktrace.out
Generated at 2013-07-21 18:34:05 +0000
NoMethodError: undefined method `each' for nil:NilClass
/srv/chef/file_store/cookbooks/my_cookbook/recipes/default.rb:9:in
`from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/
mixin/from_file.rb:30:in `instance_eval'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/
mixin/from_file.rb:30:in `from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/
cookbook_version.rb:346:in `load_recipe'

So, this brings us to the end of blog. This Tecklearn ‘Execute Cookbook on Node and run Chef’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Chef and build a career in DevOps domain, then check out our interactive, Continuous Delivery using Chef Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:
https://www.tecklearn.com/course/continuous-delivery-using-chef/

Continuous Delivery using Chef Training

About the Course

Tecklearn has specially designed this Continuous Delivery using Chef Training Course to advance your skills for a successful career in this domain. The course will cover different components of Chef and how they are used in software development operations. The course consists of important concepts like Continuous Delivery (CD) using Chef, Chef Framework, How Chef Works, Chef Advantages and Chef Installation. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Chef.

Why Should you take Continuous Delivery using Chef Training?

• The average salary for a Senior Development Operations (DevOps) Engineer with Chef skills is $118, 435. – PayScale.com
• Airbnb, Facebook, Slack, Shopify, Digital Ocean & many other MNC’s worldwide use Chef across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps
• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation
Continuous Delivery using Chef
• Continuous Delivery
• What is Chef
• Chef Framework
• How Chef Works
• Chef Advantages
• Chef Installation
• Hands on

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Execute Cookbook on Node and run Chef-Client"

Leave a Message

Your email address will not be published. Required fields are marked *