Introduction
Knife is a command-line program that acts as a conduit between a local chef repository and the Chef Infra Server. The knife command line tool needs to be set up to communicate with the Chef Infra Server and any other infrastructure within your company. It is used for managing other Chef features as well as uploading cookbooks. It offers a connection between the local machine's chefDK (Repo) and the Chef server.
This blog explains the details of Setting up Knife along with the details of Knife configuration, configuration file, and setting your text editor.
Without further ado, let's get started.

Configuration
A config.rb file was suggested for setting up knives in previous Chef Infra configurations. Run the Knife configure to create a Chef Infra credentials file at ~/.chef/credentials to set up a knife to communicate with Chef Infra Server for the first time. In order to work with a single Chef Infra Server and a single Chef Infra Server organisation, the configuration of knives using config.rb is still valid.
Command:
New-Item -Path "c:\" -Name ".chef" -ItemType "directory"
New-Item -ItemType "file" -Path "c:\.chef\config.rb"
Any Ruby code can be included in the configuration file's config.rb file to go beyond static values. Environmental variables from the workstation can be loaded using this. As a result, you may create a single config.rb file that all users in your company can use. Additionally, you may check in this single file to your chef-repo, which will enable users to load various config.rb files depending on which chef-repo they run commands from. When each chef-repo leads to a distinct chef server or company, this can be extremely helpful.
Example:
current_dir = File.dirname(__FILE__)
user = ENV['CHEF_USER'] || ENV['USER']
node_name user
client_key "#{ENV['HOME']}/chef-repo/.chef/#{user}.pem"
chef_server_url "https://api.opscode.com/organizations/#{ENV['ORGNAME']}"
syntax_check_cache_path "#{ENV['HOME']}/chef-repo/.chef/syntax_check_cache"
cookbook_path ["#{current_dir}/../cookbooks"]
cookbook_copyright "Your Company, Inc."
cookbook_license "Apache-2.0"
cookbook_email "cookbooks@yourcompany.com"
# Amazon AWS
knife[:aws_access_key_id] = ENV['AWS_ACCESS_KEY_ID']
knife[:aws_secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY']Profile Support since Chef 13.7
Knife profiles make it simpler to switch knives between organisations on the same Chef Infra Server or between Chef Infra Servers. There is an alternative to config.rb called knife profiles; you cannot use both. Create your knife profiles by including them in the .chef/credentials file located in your home directory on your workstation. There is a TOML format in the credentials file. Your choice of "table" name is listed after each profile, followed by key=value pairs. The keys match any setting that is allowed in the config.rb file.
Unless you specify an absolute path, file names like client_key or validator_key are relative to ~/.chef. Use client name (recommended) or node name to identify clients. Keep a distinct profile of credentials in the credentials file for use with target mode (chef-client —target switch.example.org). When the name contains a period, use the DNS name of the target as the profile name and enclose it in single quotes. Keys that are legitimate configuration choices, like port, are sent to the train.
Code:
[default]
client_name = "barney"
client_key = "barney_rubble.pem"
chef_server_url = "https://api.chef.io/organizations/bedrock"
[default.knife]
ssh_user = 'ubuntu'
aws_profile = 'engineering'
use_sudo = true
# client key
[dev]
client_name = "admin"
client_key = """
-----BEGIN RSA PRIVATE KEY-----
#RSA private key
MIICXAIBAAKBgQCqGKukO1De7zhZj6EXAMPLEKEY
...ABC123=
-----END RSA PRIVATE KEY-----
"""
#validator text
validator_key = "test-validator.pem"
#server url
chef_server_url = "https://api.chef-server.dev/organizations/test"
#Web Production
['web.preprod']
client_name = "ninja"
client_key = "preprod-brubble.pem"
chef_server_url = "https://preprod.chef-server.dev/organizations/preprod"
['switch.example.org']
user = "cisco"
password = "cisco"
enable_password = "cisco"
In order of priority, there are four options to choose which profile to use:
-
Knife should be given the —profile option, for example, knife node list —profile dev.
-
In the CHEF PROFILE environment variable, enter the name of the profile.
-
To the /.chef/context file, add the profile name.
-
If not, Knife use the "default" profile.
Let's look at the details of knife configuration.






