Ask Your Question
0

How to join the content of an array in an EPP template ?

asked 2016-06-21 02:08:27 -0500

Denis Sacchet gravatar image

Hello,

according to the following documentation page :

https://docs.puppet.com/puppet/latest...templateepp.html

I should be able to use the join function to merge the content of an array. I tried a lot of things without success :

$ cat test3.epp 
<%- | Array $test_array_join
| -%>
<%= join($test_array_join,'---') %>
$ puppet epp render test3.epp --values "{ 'test_array_join' => [ 'a', 'b', 'c' ] }"
Error: Evaluation Error: Unknown function: 'join'. at /home/git/features/dmzin_news_ftp/puppet/modules/proftpd/templates/test3.epp:5:5
Error: error while rendering epp
Error: Try 'puppet help epp render' for usage
$ 

I also try to isolate the code from the documentation example, same results :

$ cat test4.epp 
<%- |
      Array   $keys_trusted
| -%>
<% unless $keys_trusted =~ Array[Data,0,0] { -%>
trustedkey <%= $keys_trusted.join(' ') %>
<% } -%>
$ puppet epp render test4.epp --values "{ 'keys_trusted' => [ 'a', 'b', 'c' ] }"
Error: Evaluation Error: Unknown function: 'join'. at /home/git/features/dmzin_news_ftp/puppet/modules/proftpd/templates/test4.epp:5:34
Error: error while rendering epp
Error: Try 'puppet help epp render' for usage
$ 

From my understanding, I can use all Puppet functions inside EPP, but join() is a function of the puppet/stdlib.

Please help me, I need some features introduced by EPP template, but if I can't join array, it will be complicated :(

$ puppet --version
4.3.1

Thanks in advance

Best regards

Denis Sacchet

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2017-01-18 17:47:15 -0500

jack9 gravatar image

updated 2017-01-18 18:05:41 -0500

puppet --version 4.8.1

as far as i understand your question it is the same i wanted to archieve:

set from hiera or in the pp file:

$ips = ['192.168.0.1', '192.168.0.2', '192.168.0.3']

epp template for myservice.conf:

server_list = <%= $ips.join(', ') %>

results in myservice.conf

server_list = 192.168.0.1, 192.168.0.2, 192.168.0.3

edit: if you need it quoted use the following epp syntax (both work):

server_list = <%= $ips.map |$x|{ "'${x}'"}.join(', ') %>
server_list = <%= $ips.map |$x|{ "'$x'"}.join(', ') %>

the orginal ruby syntax does not work:

server_list = <%= $ips.map { |$x| "'#{x}'"}.join(', ') %>

sources:

edit flag offensive delete link more
1

answered 2016-06-23 03:56:39 -0500

Denis Sacchet gravatar image

Hello,

thanks for your answer but it is not related to my problem. Your syntax is for ERB template, and my problem is with EPP template.

Whatever, I resolved my problem, and when I run the "puppet epp render" command, as my user, it doesn't use the /etc/puppetlabs configuration files, so two solutions :

  • add correct configuration in ~/.puppetlabs in order to point the correct lib path to find the stdlib package necessary to use join function in an EPP template
  • or simple call "puppet module install puppetlabs-stdlib" to install the module in the right path for your user environment.

Best regards

Denis Sacchet

edit flag offensive delete link more

Comments

2

you should add your reply as edit of your question or use the comment function on the answer you refering to. using a new answer as a comment is suboptimal

jack9 gravatar imagejack9 ( 2017-01-18 17:34:46 -0500 )edit

If you want puppet to use the values in the 'root' configuration, you have to run it as root... compare `sudo puppet config print confdir` to `puppet config print confdir`. You may also need an --environment flag as well

DarylW gravatar imageDarylW ( 2017-01-19 07:13:45 -0500 )edit
-2

answered 2016-06-23 03:22:32 -0500

lupin gravatar image

updated 2016-06-23 03:25:15 -0500

You wanted to merge the contents of two Arrays into single Array?

E.g If you have Arrays like these;

 $server = [ 'a', 'b', 'c' ]
 $ip     = [1, 2, 3]

And wanted a result like this;

trustedkey = ["a", "b", "c", "1", "2", "3"]

Your template would look like this

<% @server = @server.concat(@ip) %>
trustedkey = <%= @server %>
edit flag offensive delete link more

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: 2016-06-21 02:08:27 -0500

Seen: 358 times

Last updated: Jan 18