About | FAQ | Help
Ask Your Question

Joining array from Hiera+concat other value in erb?

asked 2013-11-21 14:10:14 -0500

KDub gravatar image

Hi, I'm just starting to covert from using CSV files and extlookup to Hiera and ran into a small snag. In my manifest I have two lookups for a regular string and an array.

$cassandrasvr   = hiera("cassandrasvr"),
$cassandraport  = hiera("cassandraport"),

And my YAML file contains:

cassandraport: '9160'
- cass1.abc.com
- cass2.abc.com
- cass3.abc.com
- cass4.abc.com
- cass1.xyz.com
- cass2.xyz.com

In my ERB template, I have the following which will print out the contents of the array, separated by a comma (except the last value).

cassandrasvr=<%= cassandrasvr.join(',') %>

And the output is this ... (more)

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-12-12 12:37:09 -0500

KDub gravatar image

Ok, we had Tom from Puppetlabs in for an on-site training class and he gave me the correct Ruby syntax for this using the map function.

cassandrasvr=<%= cassandrasvr.map{ |srv| "#{srv}:#{@cassandraport}" }.join(',') %>

Which now results in the correct syntax with the port on the last entry and no trailing comma.


NOTE: my original YAML file contained this:

cassandraport: '9160'
- cass1.abc.com
- cass2.abc.com
- cass3.abc.com
- cass4.abc.com
- cass1.xyz ...
edit flag offensive delete link more

answered 2013-11-22 10:51:15 -0500

KDub gravatar image

Thanks for that - almost there! I changed my ERB to use this:

cassandrasvr=<%= cassandrasvr.join(":#{@cassandraport},") %>

and now get an output of this:


Notice the last entry doesn't have the port. I guess I have sort of two conflicting things here - I want the port on the last entry, but not the comma. How would I accomplish this?

edit flag offensive delete link more


You are right, I forgot the colon ;-)

doc75 gravatar imagedoc75 ( 2013-11-23 03:07:05 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-11-21 14:10:14 -0500

Seen: 8,903 times

Last updated: Dec 12 '13