Specifying which cookbooks to run with Chef Solo

后端 未结 5 675
孤城傲影
孤城傲影 2021-01-31 11:05

I\'m using chef-solo to test my cookbooks locally, but I want to be able to only run the cookbook(s) that I\'m testing. Currently, it seems like chef-solo will run all cookbooks

相关标签:
5条回答
  • 2021-01-31 11:34

    chef-client --local-mode hello.rb
    check
    https://learn.chef.io/learn-the-basics/ubuntu/configure-a-resource/
    https://docs.chef.io/ctl_chef_client.html

    0 讨论(0)
  • 2021-01-31 11:39

    Also worth noting that when testing you can easily override the runlist

    chef-solo --override-runlist "role["somerole"],recipe[mycookbook::recipe]"
    
    0 讨论(0)
  • 2021-01-31 11:39

    Sadly, I don't think there is. Chef runs in a two-pass mode: first, it parses and builds a model of your cookbooks (the "compile" phase), and then it runs only those cookbooks that you specify in the run list. I believe it does this to make sure it catches the dependencies between cookbooks in the compile phase. The compile phase has to be consistent or else it errors out.

    To really understand this, look at the ruby block resource. Any ruby that's floating around in your recipes will be evaluated at compile time; any ruby you want to run at evaluation time has to be placed in a ruby block resource. Once you grok that, the nature of the two-pass evaluation becomes apparent.

    I think your only options are to bring in your cookbooks one at a time, like you are doing, or to fix your attributes.

    0 讨论(0)
  • 2021-01-31 11:52

    You need to create a JSON file. There you can specify what exactly cookbook should run:

    {
      "run_list": [ "recipe[hello-chef]" ]
    }
    

    Very well explained in this step-by-step tutorial: http://codeflex.co/creating-and-running-your-first-chef-cookbook/

    0 讨论(0)
  • 2021-01-31 11:56

    Update: chef-solo is no longer a "separate" tool from chef-client. From the documentation:

    chef-solo is a command that executes chef-client in a way that does not require the Chef server in order to converge cookbooks. chef-solo uses chef-client’s Chef local mode, and does not support the following functionality present in chef-client / server configurations

    This change was implemented in Chef version 12.11, which fulfills the community RFC. This was released on June 8, 2016. The old behavior, which is described below (albeit from 4 years ago at this time), is available with the --legacy-mode argument to chef-solo.

    For the most current and up to date information about Chef Solo, please read the official documentation

    My original answer is below:

    Chef (solo OR client) does not "run" all the cookbooks.

    It loads all the cookbook's Ruby files in the following directories, in this order:

    • libraries/*.rb
    • providers/*.rb
    • resources/*.rb
    • attributes/*.rb
    • definitions/*.rb

    Then, it loads all the recipes that are in the node's expanded run list. With chef-solo, this comes from a JSON file supplied with -j, or can be done in attributes files - however the latter is deprecated and not recommended.

    Any recipes that are included by those in the expanded run list via include_recipe are also loaded. Chef loads recipes by evaluating them as Ruby code. When it encounters ruby code that it recognizes to be a resource, or a definition, it adds the resource to the Resource Collection, which is an numerically ordered indexed hash of all the resources. Definitions are special, in that Chef adds the resources they contain, not the definition itself, to the Resource Collection. Included recipes' resources via include_recipe are inserted in place, then Chef continues in the including recipe.

    Once Chef has processed all the recipes for all their resources, it walks the resource collection taking the specified action on each in the order it was added to the collection.

    I highly recommend reading the documentation on this process. It applies to Chef Solo; only the part where the cookbooks are downloaded from the server is skipped.

    • https://docs.chef.io/chef_client.html#the-chef-client-run

    To ensure that only the recipes that you want to test are tested, include them in the node's run list via a JSON file. It looks like this:

    { "run_list": ["recipe[mything]", "recipe[anotherthing]"] }
    

    The run list is just an array, and items can be recipe[cookbookname] or role[somerole]. You can read more about how to use Roles with Chef Solo on the Chef Solo documentation.

    • https://docs.chef.io/chef_solo.html

    If you have system changes being made when Chef is loading the cookbook components (the ruby files in each cookbook), then "You're Doing It Wrong"(tm) and should refactor those things to be done in a resource called from a recipe.

    0 讨论(0)
提交回复
热议问题