Table of contents
1.
Introduction
2.
chef-run (executable)
3.
Specifying resource properties and actions
4.
Running a Recipe
5.
Configuring Cookbook Dependencies and Sources
6.
Frequently Asked Questions
6.1.
What characteristics does Chef Compliance have?
6.2.
How does the DevOps team use Chef Infra for infrastructure management?
6.3.
Describe the characteristics of Chef Habitat.
7.
Conclusion
Last Updated: Mar 27, 2024

chef-run (executable) - Tool

Author Shivani Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Chef allows you to automate processes by writing scripts. The chef is employed by a large number of infrastructure companies. Using it, infrastructure can be converted into code, providing an effective automation platform. In order to automate infrastructure, Chef is used, which is a powerful platform for converting infrastructure into code.

chef

But what does infrastructure as code actually mean? Let's say you need to install a Java program on a single machine. It is not necessary to automate the task; you can complete it manually.

But what happens when you need to deploy your application on 10, 50, or 100 additional machines? This is so because one machine is unable to handle the load. The chef can help with this. You can create code that deploys your application automatically rather than having to do it by hand on each and every computer.

Let us dig deeper into chef-run (executable).

chef-run (executable)

Utilizing the Chef Infra Client, ad hoc tasks can be carried out on one or more target nodes by using the tool chef-run. Start by running a chef-run -h to become acquainted with the arguments and flags. 

Chef-run targets a single machine and runs a single resource on it in its most basic form:

chef-run ssh://my_user@host1:2222 directory /tmp/foo --identity-file ~/.ssh/id_rsa

To apply a Resource to a Single Node over SSH

In its most basic form, chef-run targets a single computer and runs just one resource on it:

chef-run ssh://my_user@host1:2222 directory /tmp/foo --identity-file ~/.ssh/id_rsa


The default protocol used here is SSH. Chef-run tries to read defaults from your /.ssh/config file while using SSH. Using the aforementioned SSH configuration:

Host host1
  IdentityFile /Users/me/.ssh/id_rsa
  User my_user
  Port 2222


The chef-run command may be defined as:

chef-run host1 directory /tmp/foo

To apply a Resource to a Single Node over WinRM

You must include the winrm protocol in the connection metadata if you want to target WinRM:

chef-run 'winrm://my_user:my_p4ssword!@host' directory /tmp/foo


Only password authentication is supported for WinRM connections. Use the —user and —password parameters to provide the username and password, or enter them like in the example. By including the —SSL flag, HTTPS connections are supported. cooking run over WinRM does not support target host authentication using certificates.

Specifying resource properties and actions

In the command line, you can specify any of the Chef Infra resources. The resource type, the resource name, and the chef-run command should all be entered in that order. 

Specifying resource properties and actions

Consider this:

chef-run host1 group awesome_group


This command designates the awesome group resource for the group. Use a key=value syntax to specify actions and properties:

chef-run host1 group awesome_group gid=1001
chef-run host1 user super_person gid=1001 'password=complex=p@ssword!!'
chef-run host1 user super_person action=remove

Running a Recipe

Specify a recipe using its path in order to run it completely:

chef-run host1 /path/to/recipe.rb
chef-run host1 recipe.rb


You can also mention the cookbook if your recipe is in one:

chef-run host1 /cookbooks/my_cookbook/recipes/default.rb
chef-run host1 /cookbooks/my_cookbook


Chef-run will run the default recipe from the cookbook on the target node if the cookbook's path is specified.

Chef-run offers the option to look up your cookbook in a nearby library. Assuming your cookbook repository is located at /cookbooks, run: cd /cookbooks

chef-run host1 my_cookbook
chef-run host1 my_cookbook::non_default_recipe


Chef-run is instructed to use an alternative recipe via the command::recipe name. /.chef-workstation/config.toml and /.chef/config.rb are the local Chef Workstation and Chef configuration files that are read by chef-run. It searches the paths listed in both files for cookbooks.

Configuring Cookbook Dependencies and Sources

Configuring Cookbook Dependencies and Sources

Chef-run generates a Policyfile bundle that contains the specified cookbook when a target node is convergent. This will be recognized if the cookbook you mentioned contains a separate Policyfile. rb.

Your metadata.rb file contains: name "really_complicated"

…
depends "pretty_simple"
In your Policyfile. rb file:
name "really_complicated"
default_source :supermarket
default_source :chef_repo, "../"
run_list "really_complicated::first"
cookbook "pretty_simple"
In your recipes/first.rb
log "lets include some stuff"
include_recipe "pretty_simple::second"


In order to prepare the target node for convergence, chef-run host1 really complicated::first gathers all the cookbook requirements (pretty simple) first. The first recipe launches its second recipe after determining its local dependency on the pretty simple cookbook when it is executing on that node.

Frequently Asked Questions

What characteristics does Chef Compliance have?

By using standardized audit and remediation material across heterogeneous estates to enable visibility and control across hybrid and multi-cloud environments, Chef Compliance helps enforce and manage compliances and avoid security incidents.

How does the DevOps team use Chef Infra for infrastructure management?

Chef Infra automates infrastructure configuration, ensures that configuration policy is consistent, accurate, adaptable, testable, versionable, and human-readable, and ensures that every configuration change will be applied uniformly throughout the whole infrastructure.

Describe the characteristics of Chef Habitat.

Regardless of the deployment platform or operating system, it delivers automation for defining, packaging, and delivering applications to any environment. Without reworking or rewriting, it produces deployable artifacts for virtual machines or containers. Additionally, it facilitates the widespread adoption of agile delivery techniques in both operations and development.

Conclusion

In this blog, we discussed the chef run executable. We also saw Specifying resource properties and actions, applying a Resource to a Single Node over SSH and over WinRM, and running a recipe. We also discussed Configuring Cookbook Dependencies and Sources. 

For more content, Refer to our guided paths on Coding Ninjas Studio to upskill yourself.
Check out this problem - Largest BST In Binary Tree

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thankyou
Live masterclass