7

Please help me with the recipe to pull the cookbook name and version during the chef-client run. I want to store the values to variables and use as part of my recipe.

I am finding difficulty on how to pull the cookbook version and name from the recipe during chef-client run on the node.

Thanks in advance.

thecontent
  • 81
  • 1
  • 3
  • 1
    I find it unfair to vote down, without giving the author a hint, how to improve the question. My answer is a pretty wild guess.. I hope this is what you want. Try to be more explicit about what you try to achieve, please. Welcome to stackoverflow, btw. – StephenKing Apr 03 '14 at 14:56
  • @StephenKing Thank you for giving me a chance to improve the question.I am new to chef, so not sure if the question made sense or not. Let me explain you my problem : In the recipe I want to put a variable and store the cookbook name and another variable version, so that in the later part of the code I am using the cookbook name and version. I would appreciate if you share an example or guide me to the link or blog where I can refer and write the recipe. Thanks again . – thecontent Apr 03 '14 at 17:24

1 Answers1

9

You can access the current cookbook's name through the cookbook_name variable.

The version of this cookbook (and other cookbooks) can be retrieved through the run_context:

run_context.cookbook_collection[cookbook_name].version
StephenKing
  • 30,871
  • 9
  • 73
  • 105
  • 1
    I'll just add - if you think you need to do this, you are probably doing something wrong. – sethvargo Apr 03 '14 at 16:03
  • 1
    I agree, I've also never had a use case about this or could imagine one (well.. maybe reporting in a handler or so.. but this is about running inside recipes (I think)). – StephenKing Apr 03 '14 at 16:09
  • @sethvargo : Please guide me if I am doing wrong. Is it incorrect to get the cookbookname and version and store in a variable as part of recipe. Is there any other way to achieve the same ? – thecontent Apr 03 '14 at 17:25
  • @user3493086 why? What are you trying to accomplish in doing so? – sethvargo Apr 03 '14 at 18:22
  • 1
    I am creating a folder on machine with the cookbook name+version on central server and will publish it. During the chef-client run , I want below command to run : wget http://servername/$cookbookname$version/ – thecontent Apr 03 '14 at 20:20
  • Why do want do manually download cookbooks? Are you running chef-solo? Even then there are other ways. And you can't easily just execute the cookbooks you've just downloaded. I suggest that you edit your question and add a detailed scenario description. – StephenKing Apr 04 '14 at 05:34
  • If you're trying to do something like have a tomcat cookbook with a version 7.0.0 and version 8.0.0 and pulling down the tomcat version based on the cookbook version you are doing it extremely wrong and need to stop. Those version numbers need to be attributes. If you need to have different behavior for v7 vs. v8 you need to break those out into different recipes in the cookbooks (and you can drive including the different version recipes based on the attribute). – lamont Sep 03 '14 at 19:55
  • 1
    I wish I could downvote all those "why do you need it" and "you're doing it wrong" guys. – Nowaker Dec 15 '17 at 16:18
  • 1
    Sure, why should one ever stop somebody abusing tools and building crap.. Good luck – StephenKing Dec 15 '17 at 17:21
  • btw. a classical example where the most important answer is to destroy the solution path the the original poster has in mind is this: https://stackoverflow.com/questions/47893288/add-a-new-role-to-a-chef-json-file-with-sed – StephenKing Dec 20 '17 at 05:28