chef how to overwrite attribtues in json file

855 views Asked by At

I'm trying to run the same recipe twice with different attributes. Is there a way to specify it in the run list? Example:

"myRecipe":{
"run-list": "recipe[test], recipe[test]"
}

and the first one should have flag=false while second one should execute with flag=true.

4

There are 4 answers

6
StephenKing On BEST ANSWER

No, that's not possible. You have to implement such logic in your recipe and e.g iterate over an array.

1
max On

I've also tried using resources:

define :installx, :cmd=>'good', :upgrade=>true do
  Chef::Log.info('cmd = #{params[:cmd]}')
  if params[:upgrade]
    Chef::Log.info('upgrading...')
  else
    Chef::Log.info('installing...')
  end
end

installx resource
installx "name" do
  cmd "install 1"
  upgrade true
end

and it errors out: "ERROR: Cannot find a resource for define" This is pretty much right out of the official documentation. If anyone know what's causing this, please let me know.

1
cixelsyd On

I answered a similar question previously: The short version is, you need to modify your thinking. If you need to install software and configure it, you might do better to think about breaking it apart into two pieces.

Longer answer: have you taken a look at any of the cookbooks on the opscode community site? Many patterns recur and work that others have published could certainly be useful... Especially as you appear to be just starting out with chef.

0
Yeung Y Siu On

Some of the Chef cookbook are written very well in my opinion such as the visualstudio cookbook from https://github.com/daptiv/visualstudio.

I do have a case when I need to run this recipe twice. I have to install Visual Studio 2012 and 2013 on a machine to compile different source code. These versions of Visual Studio have the exact same silent install process by point to an XML file so it was easy to make it work for 2012 and 2013.

I was able to create a Chef role file to override the attributes of the visualstudio cookbook to point my private Visual Studio 2012 ISO. That was easy. Then I created another Chef role file for installing 2013 to point to a separate Visual Studio 2013 ISO. Since Chef doesn't run the recipe twice it ends up only installing Visual Studio 2013. :(

It would suck if I have to make two local copies of the "visualstudio" cookbook.