Ruby output array via erb pattern

I use a puppet to create a set of constants for a ruby ​​program. I need to provide an array of host names over which my program will iterate.

In the bash script that I used before, I just had as a puppet variable

hosts => "host1,host2"

which I provided a bash script like

HOSTS=<%= hosts %>

obviously this will not work for ruby ​​- I need this in the format

hosts = ["host1","host2"]

as

p hosts

and

puts my_array.inspect

provide a conclusion

["host1","host2"]

I was hoping to use one of them. Unfortunately, I can’t understand in my entire life how to do this. I tried each of the following:

<% p hosts %>
<% puts hosts.inspect %>

I found somewhere where they indicated that I would need to put "function_" before the function calls ... which does not seem to work. I decided to use an iterative model:

[<% hosts.each do |host| -%>"<%=host%>",<% end -%>]

it works by giving me

["host1","host2",]

. . - ? ?

+5
3

(Puppet v3.0.1) :

{
    "your_key1": {
        "your_key2": [<% puppet_var.join('", "').each do |val| %>"<%= val %>"<% end -%>]
    }
}

$puppet_var - , [ "a", "b" ] :

{
    "your_key1": {
        "your_key2": ["a", "b"]
    }
}
+4

Ruby % w shorcut . :

hosts = %w{<%= hosts.join(' ') %>}
+3

Use to_json;

hosts.to_json
=> "[\"host1\",\"host2\"]"
0
source

All Articles