Table of contents
1.
Introduction
2.
About Knife
3.
Knife Role Subcommand
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.
from file Argument 
8.1.
Syntax 
8.2.
Options
8.3.
Examples
9.
list Argument
9.1.
Syntax 
9.2.
Options
9.3.
Examples
10.
Show Argument
10.1.
Syntax 
10.2.
Options
10.3.
Examples
11.
Frequently Asked Questions
11.1.
Does Chef have a Graphical User Interface?
11.2.
What do you understand about a node in Chef?
11.3.
What do you mean by an attribute in Chef?
11.4.
What are the three main components of Chef?
11.5.
What do you mean by a client in Chef?
12.
Conclusion
Last Updated: Mar 27, 2024
Medium

About Knife Role 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 the servers and network equipment in your organization 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 role subcommand in detail. We will learn about syntax and properties as well. Let's see all these things in more detail.

About Knife

Knife in chef

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 Role Subcommand

A role is a means of defining specific patterns and procedures present across organizational nodes belonging to a particular job function. Each role has a "run list" and zero or more attributes. A node may have 0 or more roles assigned to it. The configuration information for a node is compared to the role's characteristics when a role runs against it. Then the contents of a role's "run list" are applied to the node's configuration information. When a Chef Infra Client executes, it combines its own "run lists" and attributes with those found in each assigned role.

You can use the "knife role subcommand” to manage the roles on the Chef Infra Server. These roles can be associated with one or more nodes on the Server.

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

bulk delete Argument

You can use the "bulk delete" argument to simultaneously delete one or more roles. You can use a regular expression or regex to match the roles 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 role 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 roles:

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

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

create Argument

You can use the "create" argument to add a role on the Chef Infra Server. The data of the role 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 role create NAME_OF_THE_ROLE (options)

Options

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

--description DESCRIPTION

 It is the description of the role. This information fills out the role's description field on the Chef Infra Server.

Examples

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

Creating a role:

Let's add a role named "temp_role." We can add the role by entering the following command.

knife role create temp_role

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

{
   "name": "temp_role",
   "default_attributes": {
   },
   "json_class": "Chef::Role",
   "run_list": ["recipe[name_of_the_cookbook::name_of_the_recipe]",
                 "role[name_of_the_role]"
   ],
   "description":"  ",
   "chef_type": "role",
   "override_attributes": {
   }
}

When you have completed the task, save it.

delete Argument

You can use the "delete" argument to delete or remove a role. It will remove the role from the Chef Infra Server. 

Syntax 

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

knife role delete NAME_OF_THE_ROLE

Options

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

Examples

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

Deleting a Role:

Let's delete a role named "temp_role." We can delete the role by entering the following command.

knife role delete temp_role

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

edit Argument

You can use the "edit" argument to edit the details of the role. It will edit the details of the role on the Chef Infra Server.

Syntax 

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

knife role edit NAME_OF_THE_ROLE

Options

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

Examples

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

Editing a role:

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

knife role edit temp_role

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

{
   "name": "temp_role",
   "description": "This is the temporary description for the temp_role role.",
   "json_class": "Chef::Role",
   "default_attributes": {
   },
   "override_attributes": {
   },
   "chef_type": "role",
   "run_list": ["recipe[name_of_the_cookbook::name_of_the_recipe]",
                "role[name_of_the_role]"
   ],
   "env_run_lists": {
   },
}

When you have completed the step, save it.

from file Argument 

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

Syntax 

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

knife role 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 Role using JSON data:

You can view the role details as per the values present in the JSON file:

knife role from file "path to JSON file"

list Argument

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

Syntax 

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

knife role list

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 all the roles:

You can see or view a list of all the roles on the Chef Infra Server. You will also see the URI for every role by the following command.

knife role list -w 

Show Argument

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

Syntax 

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

knife role show NAME_OF_THE_ROLE

Options

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

-a ATTR, --attribute ATTR

It is the attribute to show.

Examples

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

Showing as JSON data:

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

knife role show temp_role -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 role show -l -F json temp_role

You can also use the following command.

knife role show -l --format=json temp_role

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 Role 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