How to configure Chef Knife, Upload Cookbooks and Run a recipe on Chef Client Nodes

In our last article, we looked at how to How to install Chef Automation Server on Ubuntu 18.04 LTS. I’ll try to make an installation guide of Chef Server on CentOS 7 as well. This blog post on How to configure Chef Knife, write a test cookbook and upload it to Chef Server, and finally run this cookbook on a Server(Node) is meant to be a continuation – Delivery as promised.

By installing Chef Workstation, you’ll get knife command. Ensure you have installed Chef Server and Configured Chef Workstation before proceeding with this guide. Once all is set and ready, we can start exploring Knife setup and usage.

Introduction to knife

Knife is a command-line tool that provides an interface between your workstation and the Chef server. The knife enables you to upload your cookbooks to the Chef server and interact with nodes, the servers that you manage.

In summary, knife enables you to manage:

Nodes – Servers managed by Chef

Cookbooks and recipes

Roles, Environments, and Data Bags

Resources within various cloud environments

The installation of the chef-client onto nodes

Searching for indexed data on the Chef server

knife requires two files to authenticate with the Chef server.

An RSA private key:

Every request to the Chef server is authenticated through an RSA public key pair.

The Chef server holds the public part; you hold the private part.

2. A knife configuration file

The configuration file is typically named knife.rb.

This configuration file contains information such as the Chef server’s URL, the location of your RSA private key, and the default location of your cookbooks.

Both of these files are typically located in a directory named .chef

By default, every time knife runs, it looks in the current working directory for the .chef directory

If the .chef directory does not exist, knife searches up the directory tree for a .chef directory

Configure Knife environment ( On Workstation Machine)

In this section, we’ll configure Knife to be able to communicate with the Chef Server.

Also, edit your metadata file to specify cookbook version and Git repository URL for the same.

vim install_apache/metadata.rb

Mine has the following contents:

################################
name 'install_apache'
maintainer 'Josphat Mutai'
maintainer_email '[email protected]'
license 'All Rights Reserved'
description 'Installs/Configures install_apache'
long_description 'Installs/Configures Apache Web Server'
version '0.1.0'
chef_version '>= 13.0'
# The `issues_url` points to the location where issues for this cookbook are
# tracked. A `View Issues` link will be displayed on this cookbook's page when
# uploaded to a Supermarket.
#
# issues_url 'https://github.com/<insert_org_here>/install_apache/issues'
# The `source_url` points to the development repository for this cookbook. A
# `View Source` link will be displayed on this cookbook's page when uploaded to
# a Supermarket.
#
# source_url 'https://github.com/<insert_org_here>/install_apache'

Bootstrapping a Node

The knife bootstrap is the command you use to bootstrap a node. When using this command, you specify arguments depending on how you would normally connect to your node over SSH.

You can connect to the Node via:

Key-based authentication

Password authentication

Key-based authentication is typically recommended over password authentication because it is more secure, but you can bootstrap your node using either method. In either method, the --node-name argument uniquely identifies the node with the Chef server and its value can be whatever you want.