Table of contents
1.
Introduction
2.
About Knife
3.
knife node Command
4.
bulk delete Argument
4.1.
Syntax 
4.2.
Options
4.3.
Examples
5.
create Argument
5.1.
Syntax 
5.2.
Options
5.3.
Examples
6.
delete Argument
6.1.
Syntax 
6.2.
Options
6.3.
Examples
7.
edit Argument
7.1.
Syntax 
7.2.
Options
7.3.
Examples
8.
environment set Argument
8.1.
Syntax 
8.2.
Options
9.
from file Argument 
9.1.
Syntax 
9.2.
Options
9.3.
Examples
10.
list Argument
10.1.
Syntax 
10.2.
Options
10.3.
Examples
11.
policy set Argument
11.1.
Syntax 
11.2.
Examples
12.
run_list add Argument
12.1.
Syntax 
12.2.
Options
12.3.
Examples
13.
run_list remove Argument
13.1.
Syntax 
13.2.
Options
13.3.
Examples
14.
run_list set Argument
14.1.
Syntax 
14.2.
Options
15.
Show Argument
15.1.
Syntax 
15.2.
Options
15.3.
Examples
16.
Frequently Asked Questions
16.1.
Does Chef have a Graphical User Interface?
16.2.
What do you understand about a node in Chef?
16.3.
What do you mean by an attribute in Chef?
16.4.
What are the three main components of Chef?
16.5.
What do you mean by a client in Chef?
17.
Conclusion
Last Updated: Mar 27, 2024
Medium

About Knife Node Subcommand

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

Introduction

Have you ever tried to automate the management of your organization's servers and network equipment using any software or tool?

logo

This article is focused on the Command Line tool of one of the automation software, which is known as Chef. This command-line tool is known as the knife in detail. We will study the knife node subcommand in detail. We will learn about syntax and properties as well. Let's see all these things in more detail.

About Knife

knife

The knife is an integral part of the Chef Workstation. It is a command-line tool.

 It acts as an interface between the Chef Infra Server and a local chef-repo.

The knife has many functionalities. It allows the user to manage the following things:

  • The Nodes,
     
  • Roles, Environments, and Data Bags,
     
  • Cookbooks and recipes,
     
  • Resources within different cloud environments,
     
  • Finding indexed data on the Chef Infra Server,
     
  • The installation of Chef Infra Client on Nodes.

knife node Command

A node is any device, whether physical or virtual, under management by Chef Infra. The device can be a cloud, network device, etc. You can use the "knife node" subcommand to manage the currently present nodes on the Chef Infra Server.

Let’s check out different arguments that you can use with “knife node.”

bulk delete Argument

You can use the "bulk delete" argument to simultaneously delete one or more nodes. You can use a regular expression or regex to match the nodes as per a pattern. You must put the regular expression within quotes. You should not surround them with forward slashes (/).

Syntax 

You can use the "bulk delete" command by understanding the following syntax:

knife node bulk delete REGEX

Options

The "bulk delete" command has no options for any specific purpose.

Examples

Let's check out an example of the "bulk delete" subcommand.

Bulk deleting nodes:

You can use a regular expression that defines a pattern. The "bulk delete" will be done as per the pattern:

knife node bulk delete "^[0-9]{3}$"

Finally, you can type "Y" to confirm the deletion.

create Argument

You can use the "create" argument to add a node on the Chef Infra Server. The data of the node is present in the form of JSON. Also, the data is present on the Chef Infra Server.

Syntax 

You can use the "create" command by understanding the following syntax:

knife node create NAME_OF_THE_NODE

Options

The "create" command has no options for any specific purpose.

Examples

Let's check out an example of the "create" subcommand.

Creating a node:

Let's add a node named "temp_node." We can add the node by entering the following command.

knife node create temp_node

You can enter the data of the node in the $EDITOR. The data is present in the JSON.

{
   "normal": {
   },
   "name": "temp_node",
   "override": {
   },
   "default": {
   },
   "json_class": "Chef::Node",
   "automatic": {
   },
   "run_list": [
      "recipe[zsh]",
      "role[webserver]"
   ],
   "chef_type": "node"
}

When you have completed the task, save it.

delete Argument

You can use the "delete" argument to delete or remove a node. It will remove the node from the Chef Infra Server. You can even delete multiple nodes using the command on the Chef Infra Client 12.17 or later versions.

Syntax 

You can use the "delete" command by understanding the following syntax:

knife node delete NAME_OF_THE_NODE

Options

The "delete" command has no options for any specific purpose.

Examples

Let's check out an example of the "delete" command.

Deleting a Node:

Let's delete a node named "temp_node." We can delete the node by entering the following command.

knife node delete temp_node

edit Argument

You can use the "edit" argument to edit the details of the node. It will edit the details of the node on the Chef Infra Server. The data of the node is stored in JSON form on the Chef Infra Server.

Syntax 

You can use the "edit" command by understanding the following syntax:

knife node edit NAME_OF_THE_NODE (options)

Options

The following option is also available in the "edit" argument:

-a, --all

It displays the node in the $EDITOR. Default, automatic, or override arguments are not visible by default.

Examples

Let's check out an example of the "edit" command.

Editing a node:

Let's edit the data of a node named "temp_node." We can edit the node by entering the following command.

knife node edit temp_node

You will also need to update the node's data in JSON.

{
   "normal": {
   },
   "name": "temp_node",
   "override": {
   },
   "default": {
   },
   "json_class": "Chef::Node",
   "automatic": {
   },
   "run_list": [
      "recipe[devops]",
      "role[webserver]"
   ],
   "chef_type": "node"
}

When you have completed the step, save it.

environment set Argument

You can use the "environment set" argument in case you want to set the environment for a node. You can set the environment without editing the object of the node.

Syntax 

You can use the "environment set" command by understanding the following syntax:

knife node environment_set NAME_OF_THE_NODE NAME_OF_THE_ENVIRONMENT (options)

Options

The "environment_set" command has no options for any specific purpose.

from file Argument 

Using a template, you can use the "from file" argument to create a node. The template must be in JSON.

Syntax 

You can use the "from file" argument by understanding the following syntax:

knife node from file FILENAME

Options

The "from file" command does not have any options for any specific purpose. 

Examples

Let's check out an example of the "from file" command.

Creating a Node using JSON data:

You can add the node as per the values present in the JSON file:

knife node from file "path to JSON file"

list Argument

You can use the "list" argument to view currently present nodes. It will show you the nodes available on the Chef Infra Server. 

Syntax 

You can use the "list" command by understanding the following syntax:

knife node list (options)

Options

The following option is also available in the "list" argument: 

-w, --with-uri

This option shows the corresponding URIs.

Examples

Let's check out an example of the "list" command.

View a list of the nodes:

You can see or view a list of all the nodes on the Chef Infra Server by the following command.

knife node list 

policy set Argument

You can use the "policy set" argument to set the policy name and the policy group for a specific node.

Syntax 

You can use the "policy set" command by understanding the following syntax:

knife node policy set NODE_NAME POLICY_GROUP NAME_OF_THE_POLICY

Examples

Let's check out an example of the "policy set" subcommand. We will set the policy name and the policy group for a node named temp_node:

knife node policy set temp_node 'temp_group' 'temp_name'

run_list add Argument

A "run list" is the collection of all the necessary information required for the configuration of a  node into the required state. The following are the qualities of a run-list:

  • It is an ordered list of recipes and roles that are run in exact order.
     
  • It is dependent on the node it runs on. 
     
  • It is stored as the object of the node on the Chef Server.
     
  • You can maintain it using the knife and then upload it to the Chef Infra Server. It can be maintained using Chef Automate.  
     

You can use the "run-list" argument to add one run-lists to the node. A run-list must be in either fully qualified or cookbook or default format. Both the roles and recipes must be in quotes. For example,

"role[ROLE_NAME]"

OR,

"recipe[COOKBOOK::RECIPE]"

You can use the comma to separate recipes and roles in case of more than one item in the "run list." 

"recipe[COOKBOOK::RECIPE],COOKBOOK::RECIPE,role[ROLE_NAME]"

Syntax 

You can use the "run_list add" command by understanding the following syntax:

knife node run_list add NAME_OF_THE_NODE RUN_LIST_ITEM (options)

Options

The following option is also available in the "run_list add" argument:

-a ITEM, --after ITEM

It adds an item of the "run-list" after another specified item of a run-list.

-b ITEM, --before ITEM

It adds an item of the "run-list" before another specified item of a run-list.

Examples

Let's check out an example of the "run_list add" subcommand.

Adding a role:

If you want to add a role to the "run list," you can use the following command:

knife node run_list add NAME_OF_THE_NODE 'role[ROLE_NAME]'

Adding roles and recipes:

If you want to add roles and recipes to a "run list," you can use the following command:

knife node run_list add NAME_OF_THE_NODE 'recipe[COOKBOOK::RECIPE_NAME],recipe[COOKBOOK::RECIPE_NAME],role[NAME_OF_THE_ROLE]'

Adding a recipe with an FQDN:

If you want to add a recipe to a "run list" via the "fully qualified format," you can use the following command:

knife node run_list add NAME_OF_THE_NODE 'recipe[COOKBOOK::RECIPE_NAME]'

Adding a recipe with a cookbook:

If you want to add a recipe to a "run list" via the cookbook format, you can use the following command:

knife node run_list add NAME_OF_THE_NODE 'COOKBOOK::NAME_OF_THE_RECIPE'

Adding the default recipe:

If you want to add the default recipe of any cookbook to the "run list," you can use the following command:

knife node run_list add NAME_OF_THE_NODE 'COOKBOOK'

run_list remove Argument

You can use the "run_list remove" argument to remove run_list items from a node. The items can be roles or recipes.

Syntax 

You can use the "run_list remove" command by understanding the following syntax:

knife node run_list remove NAME_OF_THE_NODE RUN_LIST_ITEM

Options

The "run_list remove" command has no options for any specific purpose.

Examples

Let's check out an example of the "run_list remove" subcommand.

Removing a role:

If you want to remove or delete a role from a "run list," you can use the following command:

knife node run_list remove NODE_NAME 'role[ROLE_NAME]'

Removing a run-list:

If you want to remove a recipe from a "run list" via a fully qualified format, you can use the following command:

knife node run_list remove NAME_OF_THE_NODE 'recipe[COOKBOOK::RECIPE_NAME]'

run_list set Argument

You can use the "run_list set" argument to set the run_list for a node. 

Syntax 

You can use the "run_list set" command by understanding the following syntax:

knife node run_list set NAME_OF_THE_NODE RUN_LIST_ITEM

Options

The "run_list set" command has no options for any specific purpose.

Show Argument

You can use the "show" argument to view a node's details. It shows the details of the node on the Chef Infra Server.

Syntax 

You can use the "show" command by understanding the following syntax:

knife node show NAME_OF_THE_NODE (options)

Options

The following option is also available in the "show" argument:

-a ATTR, --attribute ATTR

It is the attribute to show.

-F json, --format=json

It displays the output in JSON format.

-l, --long

It displays all the attributes in the output.

-m, --medium

It displays the normal attributes in the output.

-r, --run-list

It shows only the “run list”.

Examples

Let's check out an example of the "show" command.

Showing all data about nodes:

If you want to see all the data for the node named “build”, you can use the following command:

knife node show build

It will return you the following values:

Node Name: build
Environment: _default
FQDN:
IP:
Run List:
Roles:
Recipes:
Platform:

Showing basic information about nodes:

If you want to see basic truncated and formatted information about a node, you can use the following command:

knife node show NAME_OF_THE_NODE

Show all data about nodes, truncated:

If you want to show all formatted information about a node, you can use the following command:

knife node show -l NAME_OF_THE_NODE

Showing attributes:

If you want to list a single node attribute, you can use the following command:

knife node show NAME_OF_THE_NODE -a NAME_OF_THE_ATTRIBUTE

The NAME_OF_THE_ATTRIBUTE is a kernel or the platform.

To list a nested attribute:

knife node show NAME_OF_THE_NODE -a ATTRIBUTE_NAME.NESTED_ATTRIBUTE_NAME

The ATTRIBUTE_NAME is a kernel.

The NESTED_ATTRIBUTE_NAME is a machine.

Show the FQDN

If you want to see the FQDN for a node “i-12345678”, you can use the following command:

knife node show i-12345678 -a fqdn

Showing a run-list:

If you want to see the “run list” for a node “dev”, you can use the following command:

knife node show dev -r

Showing as JSON data:

You can view the information in JSON format using the "-F" common option in the command.

knife node show temp_node -F json

You can also use other formats like text, pp, and YAML. 

Showing as raw JSON data: 

You can also view the node information in the raw JSON using the "-l" or "-long" option in the command.

knife node show -l -F json temp_node

You can also use the following command.

knife node show -l --format=json temp_node

Frequently Asked Questions

Does Chef have a Graphical User Interface?

Chef is written in Ruby and Erlang. It uses a pure-Ruby DSL in the Knife CLI. It also includes a nice GUI for easy management.

What do you understand about a node in Chef?

A node is a machine or system managed by the Chef server. It can be attached to any network that can send and receive information.

What do you mean by an attribute in Chef?

An attribute is a piece of basic information about a specific node. It shows the state of the node.

What are the three main components of Chef?

Workstations, servers, and nodes are the three main components of Chef.

What do you mean by a client in Chef?

On each node that Chef is responsible for managing, an agent called a chef-client runs locally.

Conclusion

In this article, we have studied about the Knife Node Subcommand in detail. 

We hope that this article has provided you with the help to enhance your knowledge regarding the knife of the Chef Workstation.

We hope that this article has provided you with the help to enhance your knowledge regarding Chef Workstation and if you would like to learn more, check out our articles on installation of Chef Workstation and Chef Desktop.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass