0

I have two attributes like these:

default['cookbook']['array1'] = [ "a", "b", "c", "d" ]
default['cookbook']['array2'] = [ "x", "y", "z", "w" ]

I need to pass these attributes as variables to a template like this:

template "/tmp/some.sh" do
  source "some.sh.erb"
  owner 'root'
  group 'root'
  mode "0755"
  variables(
      :bash_array1 => node['cookbook']['array1'],
      :bash_array2 => node['cookbook']['array2']
  )
end

In my bash script i need to have two arrays which will have each, the value of the two array above, like this

#!/bin/bash
inputs1=( "a" "b" "c" "d" )
inputs2=( "x" "y" "z" "w" )

What's the simpliest way to do it?

Thank you,

Gabriel

EDIT: The sh.erb file with what I've tried until now looks like this:

####### the original sh file #########
#inputs1=( "a" "b" "c" "d" ) #this is the original sh file
#inputs2=( ""x" "y" "z" "w" )
####### end of the original sh file #########

What I've tried:

inputs1=<%= @bash_array1 %>
inputs2=<%= @bash_array2 %>

and the result:

inputs1=[ "a", "b", "c", "d" ] #which cannot be used
inputs2=[ "x", "y", "z", "w" ] #which cannot be used

The end result in my sh should be

inputs1=( "a" "b" "c" "d" )
inputs2=( "x" "y" "z" "w" )

1 Answer 1

1

Not sure what you are asking beyond just how to use Erb template syntax within a Chef template. Erb has two main directives, <% %> for non-printing control code and <%= %> for printing expression values to the output. You can access the variables you pass in via @name (so for example @bash_array1). Write some Ruby code which generates the output you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for taking the time to read my problem. Indeed, my problem is that I have little to no knowledge about ruby and if I wasn't clear about that, I apologies. What I need is to know how do I get from array attribute to bash array. Thanks again.
There isn't a specific way you do it, you need to write some code. Use the basic building blocks I mentioned, with that you can write out any format you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.