Answers: <p>You may use <a href="https://docs.chef.io/attributes.html" rel="nofollow noreferrer">attributes precedence</a> for that or a wrapper cookbook, I highly encourage you to take some time to check out <a href="https://learn.chef.io" rel="nofollow noreferrer">https://learn.chef.io</a> which cover those uses cases. </p>
  
  <p>This is a kind of antipattern, one shot configuration are not what Chef is for, but for this use case I'd go with a common cookbook doing what is needed and a wrapper cookbook defining the attributes and including the common recipe.</p>
  
  <p>A typical wrapper cookbook would need a few files:</p>
  
  <ul>
  <li><code>metadata.rb</code> as another cookbook with a line <code>depends 'common_cookbook'</code></li>
  <li><code>attributes\default.rb</code> with the values for this type of server, same syntax as in common_cookbook</li>
  <li><code>recipes\default.rb</code> with the line <code>include_recipe 'common_cookbook'</code></li>
  </ul>
  
  <p>This pattern is also known as role cookbooks, more details in <a href="https://blog.chef.io/2017/02/14/writing-wrapper-cookbooks/" rel="nofollow noreferrer">this blog post</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-22T06:28:43.093Z
Answers: Thanks! Really glad to learn about the wrapper cookbooks, since I want to do things according to "legal" patterns et cetera. Although I suspected that using recipe for doing things only once (deploying some app for example) is actually an antipattern.
Answers: Learner
Answers: CC BY-SA 4.0
Answers: 2019-05-21T09:54:51.350Z
Answers: 2019-05-21T09:54:51.350Z
Answers: Tensibai
Body: <p>I have a cookbook that is being used to deploy some software on a chef nodes. I use it when bootstraping a node and then remove it from the run_list since I need it only to run once. I don't know if it's proper to do thing like that in Chef or if it's an antipattern. But this cookbook needs to have some variables that change on every node. Let's say that it needs to know the name of the main network interface of that node and I need to provide to it some kind of license number. I do that by using attributes, I specify in attributes/default.rb:</p>
  
  <pre><code>default['cookbook_name']['interface'] = 'eth0'
  default['cookbook_name']['license'] = 'abcxyz'
  </code></pre>
  
  <p>And then I upload this to chef-server and run recipe on a node that is using this attributes. But if the other person wants to deploy the same software using this cookbook on some  other node, then he needs to wait until I finish because if he will replace those values in attributes.rb with values of second node and upload it to chef-server then it will overwrite values that I need right now.</p>
  
  <p>What is the proper way to do that? I feel like I am missing something about the work flow of attributes. As far as I see, using data bags will not solve the problem. Is there some way to assign "attribute" only to a specific node? Can I assign an attribute only to a given node?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-05-21T21:44:32.577Z
Comments: Just do knife node edit and set the attributes on the node. It’s really easy once you’ve done it once. Chef is idempotent (more or less) so running the same recipe again should be fine too.
Comments: Gaius
Comments: CC BY-SA 4.0
Comments: 2019-05-22T06:27:30.020Z
Comments: Thanks Gaius, it seems like what I was looking for :)
Comments: Learner
Comments: CC BY-SA 4.0
Comments: 2019-05-22T10:34:28.897Z
Comments: Note also that you can knife node show -F json, load the JSON into e.g. Python, manipulate it, then write it back out and update the node that way...
Comments: Gaius
Tags: <chef>
Title: How to use in Chef different attributes values for different nodes
UserDisplayName: Learner
ThreadText: # How to use in Chef different attributes values for different nodes
  
  (Asked by: username_0 on 2019-05-21)
  
  I have a cookbook that is being used to deploy some software on a chef nodes. I use it when bootstraping a node and then remove it from the run\_list since I need it only to run once. I don't know if it's proper to do thing like that in Chef or if it's an antipattern. But this cookbook needs to have some variables that change on every node. Let's say that it needs to know the name of the main network interface of that node and I need to provide to it some kind of license number. I do that by using attributes, I specify in attributes/default.rb:
  
  ```
  default['cookbook_name']['interface'] = 'eth0'
  default['cookbook_name']['license'] = 'abcxyz'
  
  ```
  
  And then I upload this to chef-server and run recipe on a node that is using this attributes. But if the other person wants to deploy the same software using this cookbook on some other node, then he needs to wait until I finish because if he will replace those values in attributes.rb with values of second node and upload it to chef-server then it will overwrite values that I need right now.
  
  What is the proper way to do that? I feel like I am missing something about the work flow of attributes. As far as I see, using data bags will not solve the problem. Is there some way to assign "attribute" only to a specific node? Can I assign an attribute only to a given node?
  
  **Tags:** chef
  
  > **Comments:**
  > **username_1**: Just do knife node edit and set the attributes on the node. It’s really easy once you’ve done it once. Chef is idempotent (more or less) so running the same recipe again should be fine too.  
  > **username_0**: Thanks username_1, it seems like what I was looking for :)  
  > **username_1**: Note also that you can knife node show -F json, load the JSON into e.g. Python, manipulate it, then write it back out and update the node that way...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-05-21. Score: 0 | Accepted answer
  
  You may use [attributes precedence](https://docs.chef.io/attributes.html) for that or a wrapper cookbook, I highly encourage you to take some time to check out <https://learn.chef.io> which cover those uses cases.
  
  This is a kind of antipattern, one shot configuration are not what Chef is for, but for this use case I'd go with a common cookbook doing what is needed and a wrapper cookbook defining the attributes and including the common recipe.
  
  A typical wrapper cookbook would need a few files:
  
  * `metadata.rb` as another cookbook with a line `depends 'common_cookbook'`
  * `attributes\default.rb` with the values for this type of server, same syntax as in common\_cookbook
  * `recipes\default.rb` with the line `include_recipe 'common_cookbook'`
  
  This pattern is also known as role cookbooks, more details in [this blog post](https://blog.chef.io/2017/02/14/writing-wrapper-cookbooks/)
  
  > **Comments:**
  > **username_0**: Thanks! Really glad to learn about the wrapper cookbooks, since I want to do things according to "legal" patterns et cetera. Although I suspected that using recipe for doing things only once (deploying some app for example) is actually an antipattern.  
  
  ---
  
  