A cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure MySQL, and then it contains all of the components that are required to support that scenario, including:
The knife cookbook subcommand is used to interact with cookbooks that are located on the Chef server or the local chef-repo.
Note
Review the list of common options available to this (and all) Knife subcommands and plugins.
The bulk delete argument is used to delete cookbook files that match a pattern defined by a regular expression. The regular expression must be within quotes and not be surrounded by forward slashes (/).
This argument has the following options:
The following examples show how to use this Knife subcommand:
Bulk delete many cookbooks
Use a regular expression to define the pattern used to bulk delete cookbooks:
$ knife cookbook bulk delete "^[0-9]{3}$" -p
The create argument is used to create a new cookbook directory on the local machine, including the following directories and files:
- cookbook/attributes
- cookbook/CHANGELOG.md
- cookbook/definitions
- cookbook/files/default
- cookbook/libraries
- cookbook/metadata.rb
- cookbook/providers
- cookbook/README.md (or .rdoc)
- cookbook/recipes/default.rb
- cookbook/resources
- cookbook/templates/default
After the cookbook is created, it can be uploaded to the Chef server using the knife upload argument.
This argument has the following options:
The following examples show how to use this Knife subcommand:
Create a cookbook
To create a cookbook named “my_cookbook” with copyright, email, license, and readme format options specified, enter:
$ knife cookbook create my_cookbook -C "My Name" -m "my@email.com" -I apachev2 -r md
to return something like:
** Creating cookbook my_cookbook
** Creating README for cookbook: my_cookbook
** Creating metadata for cookbook: my_cookbook
The delete argument is used to delete a specified cookbook or cookbook version on the Chef server (and not locally).
This argument has the following syntax:
$ knife cookbook delete COOKBOOK_NAME [COOKBOOK_VERSION] (options)
This argument has the following options:
The following examples show how to use this Knife subcommand:
Delete a cookbook
$ knife cookbook delete cookbook_name version
For example:
$ knife cookbook delete smartmon 0.8
Type Y to confirm a deletion.
The download argument is used to download a cookbook from the Chef server to the current working directory.
This argument has the following syntax:
$ knife cookbook download COOKBOOK_NAME [COOKBOOK_VERSION] (options)
This argument has the following options:
The following examples show how to use this Knife subcommand:
Download a cookbook
To download a cookbook named “smartmon”, enter:
$ knife cookbook download smartmon
The list argument is used to view a list of cookbooks that are currently available on the Chef server. The list will contain only the most recent version for each cookbook by default.
This argument has the following options:
The following examples show how to use this Knife subcommand:
View a list of cookbooks
To view a list of cookbooks:
$ knife cookbook list
The metadata argument is used to generate the metadata for one or more cookbooks.
This argument has the following options:
The following examples show how to use this Knife subcommand:
Generate metadata
$ knife cookbook metadata -a
The metadata from file argument is used to load the metadata for a cookbook from a file.
This command does not have any specific options.
The following examples show how to use this Knife subcommand:
View metadata
$ knife cookbook metadta from file /path/to/file
The show argument is used to view information about a cookbook, parts of a cookbook (attributes, definitions, files, libraries, providers, recipes, resources, and templates), or a file that is associated with a cookbook (including attributes such as checksum or specificity).
This argument has the following syntax:
$ knife cookbook show COOKBOOK_NAME [COOKBOOK_VERSION] [PART...] [FILE_NAME] (options)
This argument has the following options:
The following examples show how to use this Knife subcommand:
Show cookbook data
To get the list of available versions of a cookbook named “getting-started”, enter:
$ knife cookbook show getting-started
to return something like:
getting-started 0.3.0 0.2.0
Show cookbook versions
To show a list of data about a cookbook using the name of the cookbook and the version, enter:
$ knife cookbook show getting-started 0.3.0
to return something like:
attributes:
checksum: fa0fc4abf3f6787aeb5c3c5c35de667c
name: default.rb
path: attributes/default.rb
specificity: default
url: https://somelongurlhere.com
chef_type: cookbook_version
cookbook_name: getting-started
definitions: []
files: []
frozen?: false
json_class: Chef::CookbookVersion
libraries: []
Show a cookbook version
To only view data about “templates”, enter:
$ knife cookbook show getting-started 0.3.0 templates
to return something like:
checksum: a29d6f254577b830091f140c3a78b1fe
name: chef-getting-started.txt.erb
path: templates/default/chef-getting-started.txt.erb
specificity: default
url: https://someurlhere.com
Show cookbook data as JSON
To view information in JSON format, use the -F common option as part of the command like this:
$ knife role show devops -F json
Other formats available include text, yaml, and pp.
The test argument is used to test a cookbook for syntax errors. This argument uses Ruby syntax checking to verify every file in a cookbook that ends in .rb and Embedded Ruby (ERB).
This argument has the following options:
The following examples show how to use this Knife subcommand:
Test a cookbook
$ knife cookbook test cookbook_name
The upload argument is used to upload one or more cookbooks (and any files that are associated with those cookbooks) from a local repository to the Chef server. Only files that do not already exist on the Chef server will be uploaded.
Note
Use a .chefignore file to prevent the upload of specific files and file types, such as temporary files or files placed in folders by version control systems. The .chefignore file must be located in the root of the cookbook repository and must use rules similar to filename globbing (as defined by the Ruby File.fnmatch syntax).
Note
Empty directories are not uploaded to the Chef server. To upload an empty directory, create a “dot” file—e.g. .keep—in that directory to ensure that the directory itself is not empty.
This argument has the following syntax:
$ knife cookbook upload [COOKBOOK_NAME...] (options)
This argument has the following options:
The following examples show how to use this Knife subcommand:
Upload a cookbook
$ knife cookbook upload cookbook_name
Freeze a cookbook
To upload a cookbook, and then prevent other users from being able to make changes to it, enter:
$ knife cookbook upload redis --freeze
to return something like:
Uploading redis...
Upload completed
If a cookbook is frozen and the --force option is not specified, Knife will return an error message similar to the following:
Uploading redis...
ERROR: Version 0.1.6 of cookbook redis is frozen. Use --force to override.