Ask Your Question
2

Joining array from Hiera+concat other value in erb?

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

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'
cassandrasvr:
- 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
1

answered 2013-12-12 12:37:09 -0600

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.

cassandrasvr=cass1.abc.com:9160,cass2.abc.com:9160,cass3.abc.com:9160,cass4.abc.com:9160,cass1.xyz.com:9160,cass2.xyz.com:9160

NOTE: my original YAML file contained this:

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

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

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:

cassandrasvr=cass1.abc.com:9160,cass2.abc.com:9160,cass3.abc.com:9160,cass4.abc.com:9160,cass1.xyz.com:9160,cass2.xyz.com

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

Comments

You are right, I forgot the colon ;-)

doc75 gravatar imagedoc75 ( 2013-11-23 03:07:05 -0600 )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

Stats

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

Seen: 8,047 times

Last updated: Dec 12 '13