A node is any machine—physical, virtual, cloud, network device, etc.—that is under management by Chef.
The knife node
subcommand is used to manage the nodes that exist on a Chef server.
Note
Review the list of common options available to this (and all) knife subcommands and plugins.
Use the bulk delete
argument to delete one or more nodes 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 syntax:
$ knife node bulk delete REGEX
This command does not have any specific options.
The following examples show how to use this knife subcommand:
Bulk delete nodes
Use a regular expression to define the pattern used to bulk delete nodes:
$ knife node bulk delete "^[0-9]{3}$"
Type Y
to confirm a deletion.
Use the create
argument to add a node to the Chef server. Node data is stored as JSON on the Chef server.
This argument has the following syntax:
$ knife node create NODE_NAME
This command does not have any specific options.
The following examples show how to use this knife subcommand:
Create a node
To add a node named node1
, enter:
$ knife node create node1
In the $EDITOR enter the node data in JSON:
{ "normal": { }, "name": "foobar", "override": { }, "default": { }, "json_class": "Chef::Node", "automatic": { }, "run_list": [ "recipe[zsh]", "role[webserver]" ], "chef_type": "node" }
When finished, save it.
Use the delete
argument to delete a node from the Chef server.
Note
Deleting a node will not delete any corresponding API clients.
This argument has the following syntax:
$ knife node delete NODE_NAME
This command does not have any specific options.
The following examples show how to use this knife subcommand:
Delete a node
To delete a node named node1
, enter:
$ knife node delete node1
Use the edit
argument to edit the details of a node on a Chef server. Node data is stored as JSON on the Chef server.
This argument has the following syntax:
$ knife node edit NODE_NAME (options)
This argument has the following options:
-a
, --all
The following examples show how to use this knife subcommand:
Edit a node
To edit the data for a node named node1
, enter:
$ knife node edit node1 -a
Update the role data in JSON:
{ "normal": { }, "name": "node1", "override": { }, "default": { }, "json_class": "Chef::Node", "automatic": { }, "run_list": [ "recipe[devops]", "role[webserver]" ], "chef_type": "node" }
When finished, save it.
Use the environment set
argument to set the environment for a node without editing the node object.
This argument has the following syntax:
$ knife node environment_set NODE_NAME ENVIRONMENT_NAME (options)
This command does not have any specific options.
None.
Use the from file
argument to create a node using existing node data as a template.
This argument has the following syntax:
$ knife node from file FILE
This command does not have any specific options.
The following examples show how to use this knife subcommand:
Create a node using a JSON file
To add a node using data contained in a JSON file:
$ knife node from file "PATH_TO_JSON_FILE"
Use the list
argument to view all of the nodes that exist on a Chef server.
This argument has the following syntax:
$ knife node list (options)
This argument has the following options:
-w
, --with-uri
Note
See knife.rb for more information about how to add certain knife options as settings in the knife.rb file.
The following examples show how to use this knife subcommand:
View a list of nodes
To verify the list of nodes that are registered with the Chef server, enter:
$ knife node list
to return something similar to:
i-12345678 rs-123456
A run-list defines all of the information necessary for Chef to configure a node into the desired state. A run-list is:
Use the run_list add
argument to add run-list items (roles or recipes) to a node.
A run-list must be in one of the following formats: fully qualified, cookbook, or default. Both roles and recipes must be in quotes, for example:
'role[NAME]'
or
'recipe[COOKBOOK::RECIPE]'
Use a comma to separate roles and recipes when adding more than one item the run-list:
'recipe[COOKBOOK::RECIPE],COOKBOOK::RECIPE,role[NAME]'
This argument has the following syntax:
$ knife node run_list add NODE_NAME RUN_LIST_ITEM (options)
Warning
When running knife in Microsoft Windows, a string may be interpreted as a wildcard pattern when quotes are not present in the command. The number of quotes to use depends on the shell from which the command is being run.
When running knife from the command prompt, a string should be surrounded by single quotes (' '
). For example:
$ knife node run_list set test-node 'recipe[iptables]'
When running knife from Windows PowerShell, a string should be surrounded by triple single quotes (''' '''
). For example:
$ knife node run_list set test-node '''recipe[iptables]'''
Note
The chef-client version 12.4 release adds an optional feature to the Microsoft Installer Package (MSI) for Chef. This feature enables the ability to pass quoted strings from the Windows PowerShell command line without the need for triple single quotes (''' '''
). This feature installs a Windows PowerShell module (typically in C:\opscode\chef\modules
) that is also appended to the PSModulePath
environment variable. This feature is not enabled by default. To activate this feature, run the following command from within Windows PowerShell:
$ Import-Module chef
or add Import-Module chef
to the profile for Windows PowerShell located at:
~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This module exports cmdlets that have the same name as the command-line tools—chef-client, knife, chef-apply—that are built into Chef.
For example:
$ knife exec -E 'puts ARGV' """&s0meth1ng"""
is now:
$ knife exec -E 'puts ARGV' '&s0meth1ng'
and:
$ knife node run_list set test-node '''role[ssssssomething]'''
is now:
$ knife node run_list set test-node 'role[ssssssomething]'
To remove this feature, run the following command from within Windows PowerShell:
$ Remove-Module chef
This argument has the following options:
-a ITEM
, --after ITEM
-b ITEM
, --before ITEM
Note
See knife.rb for more information about how to add certain knife options as settings in the knife.rb file.
The following examples show how to use this knife subcommand:
Add a role
To add a role to a run-list, enter:
$ knife node run_list add NODE_NAME 'role[ROLE_NAME]'
Add roles and recipes
To add roles and recipes to a run-list, enter:
$ knife node run_list add NODE_NAME 'recipe[COOKBOOK::RECIPE_NAME],recipe[COOKBOOK::RECIPE_NAME],role[ROLE_NAME]'
Add a recipe with a FQDN
To add a recipe to a run-list using the fully qualified format, enter:
$ knife node run_list add NODE_NAME 'recipe[COOKBOOK::RECIPE_NAME]'
Add a recipe with a cookbook
To add a recipe to a run-list using the cookbook format, enter:
$ knife node run_list add NODE_NAME 'COOKBOOK::RECIPE_NAME'
Add the default recipe
To add the default recipe of a cookbook to a run-list, enter:
$ knife node run_list add NODE_NAME 'COOKBOOK'
Use the run_list remove
argument to remove run-list items (roles or recipes) from a node. A recipe must be in one of the following formats: fully qualified, cookbook, or default. Both roles and recipes must be in quotes, for example: 'role[ROLE_NAME]'
or 'recipe[COOKBOOK::RECIPE_NAME]'
. Use a comma to separate roles and recipes when removing more than one, like this: 'recipe[COOKBOOK::RECIPE_NAME],COOKBOOK::RECIPE_NAME,role[ROLE_NAME]'
.
This argument has the following syntax:
$ knife node run_list remove NODE_NAME RUN_LIST_ITEM
This command does not have any specific options.
Note
See knife.rb for more information about how to add certain knife options as settings in the knife.rb file.
The following examples show how to use this knife subcommand:
Remove a role
To remove a role from a run-list, enter:
$ knife node run_list remove NODE_NAME 'role[ROLE_NAME]'
Remove a run-list
To remove a recipe from a run-list using the fully qualified format, enter:
$ knife node run_list remove NODE_NAME 'recipe[COOKBOOK::RECIPE_NAME]'
Use the run_list set
argument to set the run-list for a node. A recipe must be in one of the following formats: fully qualified, cookbook, or default. Both roles and recipes must be in quotes, for example: 'role[ROLE_NAME]'
or 'recipe[COOKBOOK::RECIPE_NAME]'
. Use a comma to separate roles and recipes when setting more than one, like this: 'recipe[COOKBOOK::RECIPE_NAME],COOKBOOK::RECIPE_NAME,role[ROLE_NAME]'
.
This argument has the following syntax:
$ knife node run_list set NODE_NAME RUN_LIST_ITEM
Warning
When running knife in Microsoft Windows, a string may be interpreted as a wildcard pattern when quotes are not present in the command. The number of quotes to use depends on the shell from which the command is being run.
When running knife from the command prompt, a string should be surrounded by single quotes (' '
). For example:
$ knife node run_list set test-node 'recipe[iptables]'
When running knife from Windows PowerShell, a string should be surrounded by triple single quotes (''' '''
). For example:
$ knife node run_list set test-node '''recipe[iptables]'''
Note
The chef-client version 12.4 release adds an optional feature to the Microsoft Installer Package (MSI) for Chef. This feature enables the ability to pass quoted strings from the Windows PowerShell command line without the need for triple single quotes (''' '''
). This feature installs a Windows PowerShell module (typically in C:\opscode\chef\modules
) that is also appended to the PSModulePath
environment variable. This feature is not enabled by default. To activate this feature, run the following command from within Windows PowerShell:
$ Import-Module chef
or add Import-Module chef
to the profile for Windows PowerShell located at:
~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This module exports cmdlets that have the same name as the command-line tools—chef-client, knife, chef-apply—that are built into Chef.
For example:
$ knife exec -E 'puts ARGV' """&s0meth1ng"""
is now:
$ knife exec -E 'puts ARGV' '&s0meth1ng'
and:
$ knife node run_list set test-node '''role[ssssssomething]'''
is now:
$ knife node run_list set test-node 'role[ssssssomething]'
To remove this feature, run the following command from within Windows PowerShell:
$ Remove-Module chef
This command does not have any specific options.
None.
Use the show
argument to display information about a node.
This argument has the following syntax:
$ knife node show NODE_NAME (options)
This argument has the following options:
-a ATTR
, --attribute ATTR
-l
, --long
-m
, --medium
-r
, --run-list
The following examples show how to use this knife subcommand:
Show all data about nodes
To view all data for a node named build
, enter:
$ knife node show build
to return:
Node Name: build Environment: _default FQDN: IP: Run List: Roles: Recipes: Platform:
Show basic information about nodes
To show basic information about a node, truncated and nicely formatted:
knife node show NODE_NAME
Show all data about nodes, truncated
To show all information about a node, nicely formatted:
knife node show -l NODE_NAME
Show attributes
To list a single node attribute:
knife node show NODE_NAME -a ATTRIBUTE_NAME
where ATTRIBUTE_NAME
is something like kernel
or platform
.
To list a nested attribute:
knife node show NODE_NAME -a ATTRIBUTE_NAME.NESTED_ATTRIBUTE_NAME
where ATTRIBUTE_NAME
is something like kernel
and NESTED_ATTRIBUTE_NAME
is something like machine
.
Show the FQDN
To view the FQDN for a node named i-12345678
, enter:
$ knife node show i-12345678 -a fqdn
to return:
fqdn: ip-10-251-75-20.ec2.internal
Show a run-list
To view the run-list for a node named dev
, enter:
$ knife node show dev -r
Show as JSON data
To view information in JSON format, use the -F
common option; use a command like this for a node named devops
:
$ knife node show devops -F json
Other formats available include text
, yaml
, and pp
.
Show as raw JSON data
To view node information in raw JSON, use the -l
or --long
option:
knife node show -l -F json NODE_NAME
and/or:
knife node show -l --format=json NODE_NAME
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs-archive.chef.io/release/12-13/knife_node.html