OverviewThe knife exec subcommand allows you to write Ruby scripts that are executed in the context of a fully configured Chef API Client using the knife configuration file. Knife exec scripts also share the same DSL as Shef's main mode - any commands available in the top-level of shef help output are available.
Note the ContextAs you are writing Ruby scripts that are executed in the context of a fully configured Chef API Client using the knife configuration file, the following are some details of note:
InvocationInvoke knife exec in one of three ways to write scripts.ExamplesSome one-line examplesList the search indexes available. Show all the nodes and their free memory available. |
Opscode team member Bryan McLellan wrote a blog post on Knife Reporting: apt + updates, using a Knife exec script to search for and describe systems needing updates. This blog is helpful for knowing how to issue shell commands in the context of your managed nodes.
Community member James Sulinski has a most excellent blog post on using Knife Exec and Fog and Highline gems to reconcile the EC2 and Chef API’s and output it in a way which provides a beneficial overview. (Code - knife.reconcile)
Need to replicate a node setup elsewhere, and aren't sure of all the cookbook dependencies? Or, just want to validate that you've uploaded all the cookbooks that are stated as being required? Community member Stephen Nelson-Smith has a blog post on Dependency Solving using Knife Exec. |
More detailed examplesA slightly different take on the knife status command.scripts/status.rb Post some status information to a google spreadsheet.Requires that you install the google-spreadsheet-ruby gem and configure GOOGLE_EMAIL and GOOGLE_PASSWORD as environment variables with the appropriate values in your shell. scripts/google_status.rb (Thanks, Darrin Eden, for the Google Spreadsheet code!) Query a node for multiple attributes.This is a ruby script executed in the context of a fully configured Chef client that can talk to the API. scripts/search_attributes.rb Example usage and output: Using Chef Knife to Delete Node AttributesThere are a number of items that must be determined initially, and apply against the script settings
Note: Because of the way node attributes work, you need to use override_attrs.delete instead of override.delete Example of changing a 'normal' attribute using set for all nodes with a certain role via knife exec:Passing Arguments to Knife ScriptsCommand-line arguments can be passed into a Knife script and read in using Ruby's global ARGV array. To ensure command line arguments are not later treated as additional files to be executed by knife, end your script with exit 0 (see CHEF-1973). scripts/stale_nodes.knife Use Case ScenarioIs there a reason why deleting a cloud instance using knife does not delete the instance in the management console? We spin instances up and down constantly, and while we do not have more than 5 instances at any given time there are now 40 instances in the dashboard. Managing them manually using a dashboard seems counterintuitive. The reason the node is left behind is because there are cases in which one would want to preserve the node data beyond the life of the specific cloud instance. There is an active discussion about best practices, and a number of people have requested the behavior you desire (instance and node deleted together) as well. While best practices are in discussion and an automated solution that addresses both needs is considered, in the current scenario, there are various approaches that can be undertaken. Some use a small shell function that runs `knife ec2 server delete`, `knife node delete`, and `knife client delete` in sequence. You could also construct a small `knife exec` script that does all three operations. Here is an example: This `knife-exec` script could be used by saving it as server-delete.rb and running it as follows:
|
|
|

