Master the knife commands for managing infrastructure from the workstation, from knife node list, knife cookbook upload, knife data bag create, knife ssh, to knife bootstrap, and learn to use environments with attribute overrides and cookbook version freezing.

In episode 7 you learned about ERB templates and the three levels of server-side data storage: data bags, encrypted data bags, and chef-vault. You know how to render dynamic configuration files and how to read secrets from within recipes. All those operations happen through cookbook files, but nothing yet connects the cookbook to the actual nodes.
That connector is the job of knife, Chef's built-in CLI that runs on the workstation. With knife you can register new nodes, upload cookbooks to the server, create data bags, run SSH commands across many nodes at once, and manage environments and cookbook versions.
Episode 8 will cover the knife commands most commonly used day-to-day, then move into environments with attribute overrides and cookbook version freezing for tighter release control.
Knife is a command-line utility that talks to the chef-server through the REST API. All of knife's configuration lives in ~/.chef/knife.rb or ~/.chef/config.rb, which contains the chef-server_url address, the workstation node name, and the location of the caller's private key.
Verify the connection to the server with the simplest commands:
knife client list
knife node listknife client list shows all registered clients, while knife node list shows nodes that are registered and have attributes. A regular node is usually also a client, so the two often appear side by side.
A finished cookbook must be uploaded to the server before nodes can run it. Without an upload, the recipe only exists on the workstation and will never be executed by chef-client on another machine.
knife cookbook upload web_server
knife cookbook upload --allknife cookbook upload web_server uploads the cookbook named web_server along with its dependencies automatically. The --all option uploads every cookbook in the working directory. To see the versions already on the server, use knife cookbook list:
knife cookbook list -aThe output shows each cookbook's name and version, for example web_server 0.1.0. If you upload again with the same version, knife will reject it because that version is already used by a run list, unless you use --force.
The data bags you learned about in episode 7 can be created directly from knife without writing a JSON file first:
knife data bag create users
knife data bag create users alice -c '{"department":"engineering"}'The second command writes an item with the id alice and inline JSON content. To add an item from a file, use knife data bag from file users bob.json as learned in episode 7.
Tip
Use knife data bag list to see all data bags, and knife data bag show users alice to view an item's contents. Both are handy for verifying that data reached the server before a cookbook uses it.
Bootstrap is the process of registering a new node on the chef-server while also installing chef-client on that machine. It's the fastest way to bring a new server under Chef management.
knife bootstrap 203.0.113.10 \
--ssh-user deploy \
--ssh-password 'S3cure' \
--sudo \
--node-name web01 \
--run-list 'recipe[apt],recipe[web_server]'The bootstrap process connects to the target machine over SSH, installs chef-client, registers it as a node and client on the server, then immediately runs the provided run list. Once bootstrapping finishes, the web01 node can be seen with knife node list and its attributes explored with knife node show web01.
To run chef-client manually on an existing node, use knife ssh:
knife ssh 'name:web*' 'sudo chef-client'
knife ssh 'role:app_server' 'sudo chef-client' --manual-list '192.0.2.10,192.0.2.11'knife ssh accepts a node search query as its first argument. The query name:web* selects all nodes whose name starts with web. The second command uses a role search, and the --manual-list option forces execution against a list of IPs without referencing node data on the server.
| Aspect | knife bootstrap | knife ssh |
|---|---|---|
| Main purpose | Register a new node | Run commands on existing nodes |
| chef-client installation | Yes, automatic | No |
| First run list | Set at command time | Doesn't change configuration |
| Target | One node | Many nodes via query |
Important
Knife node search uses the same query syntax as node search inside recipes. Learn the basic operators such as name:*, role:, and chef_environment: because these queries are also used to rearrange environments with knife.
The environments you learned about in episode 6 serve to differentiate deployment stages. Through knife, environments can be created and managed without writing Ruby files:
knife environment create development
knife environment create staging
knife environment create production
knife node environment set web01 productionEnvironment attributes are written as JSON. Environments govern attributes of the override type, whose level is higher than the default from cookbooks and roles, but lower than node attributes:
knife environment edit productionThe editor that opens contains empty JSON. You can fill it with the following block:
{
"name": "production",
"description": "Production",
"override_attributes": {
"web_server": {
"worker_processes": 16,
"port": 443
}
},
"cookbook_versions": {
"web_server": "~> 0.1.0"
}
}The override_attributes block overrides the default cookbook values for all nodes in the production environment. The cookbook_versions block limits which cookbook versions nodes may run, so production nodes don't suddenly change behavior when a new cookbook is uploaded.
Chef applies semver to cookbooks via the version field in metadata.rb. Every time you change a cookbook's behavior, bump the version with chef exec berks or manually in metadata.rb, then upload again.
Freeze prevents a cookbook at a specific version from being changed after upload. When a cookbook is frozen, the server rejects uploading the same version again:
knife cookbook upload web_server --freeze
knife cookbook upload web_server --force--freeze marks the cookbook as immutable on the server. If you forget to bump the version and try to upload again, knife will reject it with a message that the version is frozen. The --force option bypasses the freeze, but should only be used when you're really sure, because it can make nodes use an inconsistent version.
Warning
Version freezing is a safety net, not a replacement for version discipline. Always bump the cookbook version for every behavior-changing update, then freeze on upload. Combine it with cookbook_versions in the environment to guarantee consistent releases.
The recommended order for releasing changes:
knife cookbook upload web_server --freeze.staging environment by changing a test node's run list.cookbook_versions in the production environment.knife ssh 'chef_environment:production' 'sudo chef-client'.In episode 8 you mastered the most commonly used knife commands, from knife node list, knife cookbook upload, knife data bag create, knife ssh, to knife bootstrap. You also understood how environments use attribute overrides and cookbook_versions to differentiate deployment stages, and how --freeze keeps cookbook versions consistent.
Key takeaways:
override_attributes and cookbook_versions to differentiate dev, staging, and production.In the next episode, episode 9, we shift focus from the workstation to the server: Chef Infra Server. You'll learn to install chef-server-core, run chef-server-ctl reconfigure, create organizations and users, configure SSL, and handle backups with knife-ec-backup and high availability strategies. See you there.