Ruby [SOLVED]: Chef recipes – use node name in attributes

Ruby [SOLVED]: Chef recipes – use node name in attributes

Home Forums Scripting Ruby Tutorials Ruby [SOLVED]: Chef recipes – use node name in attributes

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #193938

    Cloudy Point
    Keymaster

    QuestionQuestion

    In a chef recipe I have the following code:

    if (node['server1']['PT1'] == true)
       setup('PT1')
    elsif (node['server1']['PT2'] == true)
       setup('PT2')
    end
    

    I am checking my attributes to see if the value equals true for either PT1 or PT2. This works correctly if i hardcode server1 into the code but I want to know do it dynamically depending on the server running this. How would I replace node['server1'] with something like node.name to find different servers in the attribute file. An example of my attributes is:

    default['server1'][...]...
    default['server2'][...]...
    default['server3'][...]...
    default['server4'][...]...
    

    If I can dynamically look at the different servers, that’d be the ideal result.

    #193939

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You can make this totally dynamic even:

    node['whatever'][node.name].each do |key, value|
      setup(key) if value == true
    end
    

    Source: https://stackoverflow.com/questions/46473058/chef-recipes-use-node-name-in-attributes
    Author: coderanger
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.