Extract cookbook name and version during chef-client run
Asked Answered
R

1

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.

Resupinate answered 3/4, 2014 at 9:44 Comment(2)
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.Neill
@Neill 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 .Resupinate
N
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
Neill answered 3/4, 2014 at 14:55 Comment(10)
I'll just add - if you think you need to do this, you are probably doing something wrong.Morten
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)).Neill
@Morten : 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 ?Resupinate
@user3493086 why? What are you trying to accomplish in doing so?Morten
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 servername/$cookbookname$versionResupinate
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.Neill
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).Dulcle
I wish I could downvote all those "why do you need it" and "you're doing it wrong" guys.Cordon
Sure, why should one ever stop somebody abusing tools and building crap.. Good luckNeill
btw. a classical example where the most important answer is to destroy the solution path the the original poster has in mind is this: #47893788Neill

© 2022 - 2024 — McMap. All rights reserved.