Ask Your Question
0

How to handle yaml array in erb template?

asked 2016-06-14 17:08:17 -0500

Red Cricket gravatar image

updated 2016-06-15 11:55:17 -0500

Hi,

I have some hiera data that looks like this:

netapp_nfs_shares:
  - 10.199.1.34:/os_cloud
  - 127.0.0.2:/example

This gets passed to my class as parameter and gets used in a erb template file that looks like this:

# This file was generated from nfs-shares.conf.erb by puppet
<% Array(@netapp_nfs_shares).each do |line| -%>
<%= line %>
<% end -%>

But when I puppet agent runs I get a file that looks like this:

# This file was generated from nfs-shares.conf.erb by puppet
["10.199.1.34:/os_cloud", "127.0.0.2:/example"]

I was hoping that I would get a file that looks like this:

# This file was generated from nfs-shares.conf.erb by puppet
10.199.1.34:/os_cloud
127.0.0.2:/example

I suspect that the template I wrote has something wrong with it. What am I doing wrong?

Update:

Thanks your suggestion lavaman but I am having this problem now:

Here's the template:

# This file was generated from modules/cinder/templates/nfs-shares.conf.erb by puppet
<% @netapp_nfs_shares.each do |line| -%>
<%= line %>
<% end -%>

but when I run puppet on my agent I get this error:

# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: NetworkManager is not running.
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template cinder/nfs-shares.conf.erb:
  Filepath: /etc/puppetlabs/puppet/modules/core/cinder/templates/nfs-shares.conf.erb
  Line: 3
  Detail: undefined method `each' for "[\"10.199.1.34:/svl_cos_cloud\", \"127.0.0.2:/example\"]":String
 at /etc/puppetlabs/puppet/modules/core/cinder/manifests/init.pp:90 on node osc1-mgmt-001.example.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

Update II:

I lied a bit about the hiera data. I was trying to keep my question concise. My hiera data looks like this:

---
... lots of other stuff ...
netapp_nfs_shares:
  - 10.199.1.34:/os_cloud
  - 127.0.0.2:/example
... even more stuff ...
wrapper::netapp_nfs_shares: "%{hiera('netapp_nfs_shares')}"

Here is the puppet code that using the template. I have this class ...

class wrapper (
... lots of parameters ...
    $netapp_nfs_shares                    = '',
... a lot more parameters ...
) {
       class {'::cinder':
       ... lots parameters passed to cinder class ...
        netapp_nfs_shares       => $netapp_nfs_shares,
       } ->
... more puppet code for wrapper class

... the code for the cinder class looks like this ...

class cinder (
 ... long list of other parameters ...
    $netapp_nfs_shares,
) {
   ... a lot of unrelated code ...
    file { '/etc/cinder/nfs-shares.conf':
        ensure  => file,
        path    => '/etc/cinder/nfs-shares.conf',
        owner   => cinder,
        group   => cinder,
        mode    => '0640',
        content => template('cinder/nfs-shares.conf.erb'),
    } ->
... more puppet code ...

Update III

I tried using hiera_array as suggested by lavaman like so ...

---
... lots of other stuff ...
netapp_nfs_shares:
  - 10.199.1.34:/os_cloud
  - 127.0.0.2:/example
... even more stuff ...
wrapper::netapp_nfs_shares: "%{hiera_array('netapp_nfs_shares')}"

... but this resulted in this error ....

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template cinder/nfs-shares.conf.erb:
  Filepath: /etc/puppetlabs/puppet/modules/core/cinder/templates/nfs-shares.conf.erb
  Line: 3
  Detail: undefined method `each' for "":String

... what I ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-14 19:54:45 -0500

lavaman gravatar image

The array function is creating an array with one element that is an array itself with two elements. Remove the array function from your template and it should do what you want.

edit flag offensive delete link more

Comments

Thanks lavaman. I am getting an error. I have updated my question with the details. Can you take a look please?

Red Cricket gravatar imageRed Cricket ( 2016-06-14 20:43:16 -0500 )edit

The erb template is interpreting your variable as a String. This is probably due to something you're doing in your actual puppet code, but you haven't posted any.

lavaman gravatar imagelavaman ( 2016-06-14 21:01:41 -0500 )edit

Thanks lavaman. I have posted some snippets of the actual code. I can't image what it is I am mucking up.

Red Cricket gravatar imageRed Cricket ( 2016-06-14 21:29:39 -0500 )edit

Ah. Therein lies the problem. The hiera function returns only string values. Change hiera to hiera_array in your hiera data and it should work.

lavaman gravatar imagelavaman ( 2016-06-14 23:03:13 -0500 )edit

Also, if all of that data is in the same hiera file, a hiera function call is inefficient. There are ways to alias hiera data that are part of the yaml spec. See this link for more info https://github.com/cyklo/Bukkit-OtherBlocks/wiki/Aliases-(advanced-YAML-usage)

lavaman gravatar imagelavaman ( 2016-06-14 23:05:22 -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

Stats

Asked: 2016-06-14 17:08:17 -0500

Seen: 725 times

Last updated: Jun 15 '16