0

I have a list of veriables in node file in cookbook as

"normal": {
        "Data_list": 'one, two, three, four',
    "tags": [

    ]
}

based on this list i want to add values to template , below is the source fro same , but it seems instead of running case and if statement ,it is adding all the logic as it is in template as simple text.

<%= [node['data_list']].each do |data|
     case data
     when 'one'
          "this is one and this will be added in template"
     when 'two'
          "this is two and this will be added in template"
     when 'three'
          "this is three and this will be added in template"
     when 'four'
          "this is four and this will be added in template"
     default
          "this is default and this will be added in template"
     end
end %>

any help to identify where i am doing wrong will be much helpful

nitinmaru
  • 11
  • 3

2 Answers2

0

The JSON data for data_list is set as a single string and not an array. If I understand your question correctly, I think you need to use this as your JSON data:

"normal": {
  "data_list": ["one", "two", "three", "four"],
  "tags": []
}
Jeff Coe
  • 482
  • 3
  • 18
  • problem i am facing is to run a iterator and switch case as it is coming as a plain text in template , however fetch data to template is proper. – nitinmaru Jul 29 '20 at 04:10
  • can you put some highlight on case statement where i am doing mistake? – nitinmaru Jul 29 '20 at 04:10
  • I'm not quite sure what your goal is and what you're trying to return. The case statement is written in a way to only return those strings which will add the plain text string to the file. Also, in your ERB template you're using the "<%=" tag with the equal sign which will return whatever the final result is from what enclosed code and inject it into the file. – Jeff Coe Jul 29 '20 at 18:11
0

When we use a case statement, we would typically choose from 1 of the options. By iterating over an Array in node['data_list'] we will match all or more than one condition.

However, the correct way to render the template with a case statement would be:

<% node['data_list'].each do |data| %>
  <% case data %>
  <% when 'one' %>
    'this is one and this will be added in template'
  <% when 'two' %>
    'this is two and this will be added in template'
  <% when 'three' %>
    'this is three and this will be added in template'
  <% when 'four' %>
    'this is four and this will be added in template'
  <% else %>
    'this is default and this will be added in template'
  <% end %>
<% end %>

Note: Due to iteration, any non matching array items will print the else part many times.

Of course for this to work, you would have to define data_list as an Array like Jeff suggested.

Update:

In a Chef template:

  • Bare text is rendered as it is. Example: "this is one.." (quotes included)
  • String/variable interpolation. Example: <%= node['hostname'] %> will render the hostname
  • Evaluate Ruby code. Example: <% if true %> evaluate if statement
seshadri_c
  • 6,906
  • 2
  • 10
  • 24