Bug #6744

Multidimensional Array passed to template will be flattened

Added by Daniel Grafe about 1 year ago. Updated 2 months ago.

Status:Closed Start date:03/17/2011
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:templates
Target version:2.7.6
Affected Puppet version:2.6.5 Branch:
Keywords:erb, arrays
Votes: 0

Description

We are writing several routes like this:

define route::multiple( $routes) {
    file { "routes":
        name => $operatingsystem ? {
            SLES   => "/etc/sysconfig/network/routes",
            RedHat => "/etc/sysconfig/network-scripts/routes"
        },
        ensure => present,
        content => template("net/ifroute_multiple.erb"),
    }

}

Calling the definition like that:

net::route::multiple    { "Additional routes": 
                routes => [
                        [ '10.30.0.0',    '10.30.33.20', '255.255.255.0', 'bond0' ],
                        [ '10.30.32.0',   '239.30.10.2' ,'255.255.255.240', 'bond0' ],
                        [ '10.30.7.14',   '239.30.110.2','255.255.255.240', 'eth4' ],
                        [ '194.49.199.0', '10.30.33.10', '255.255.255.0', 'bond0' ],
                        [ '194.49.239.0', '10.30.33.10', '255.255.255.0', 'bond0' ]
                    ]
        }

The multidimensional array will be expanded in the template:

#######################################################
### /etc/sysconfig/network/routes (<%= name %>)
#######################################################

# destination   gateway     genmask     device
<% routes.each do |r| -%>
<%= r[0] %> <%= r[1] %> <%= r[2] %> <%= r[3] %>
<% end-%>

With puppet Version 2.6.3 the result was a valid routes file with every line defining a route. We have now moved to puppet 2.6.5 and the result is a file with completely cluttered entries. The enclosing array seems to be “flattened”, it is no longer an array of arrays with 4 components each but it is just one serialized array. Iterating this array as shown above resulst in misinterpreted values:

#######################################################
### /etc/sysconfig/network/routes (Additional routes)
#######################################################

# destination   gateway     genmask     device

49 48 46 51
49 48 46 51
50 53 53 46
98 111 110 100

broken.sh (49 Bytes) Daniel Pittman, 03/18/2011 02:32 am

broken.pp (490 Bytes) Daniel Pittman, 03/18/2011 02:32 am

broken.erb (131 Bytes) Daniel Pittman, 03/18/2011 02:32 am

History

Updated by Daniel Pittman about 1 year ago

I can’t reproduce this. More specifically, I can’t reproduce the success case reported: everything from 2.6.0 through 2.6.6 shows this flattening for me.

On the other hand, next shows the expected behaviour, and I know that I actually had to work around related issues where the array wasn’t flattened before it got to the template. (Typically, when an array was made a member of an array, back before nesting was really supported.)

⚡ puppet --version
2.6.0
daniel@ea ~/puppetlabs/puppet                                                        (ruby-1.8.7-p330@puppet) 
⚡ ./broken.sh
notice: Scope(Test[Additional routes]): # destination   gateway     genmask     device
49 48 46 51
49 48 46 51
50 53 53 46

Updated by Daniel Grafe about 1 year ago

  • Status changed from Unreviewed to Closed

Ok, this is our fault then. We have cloned the git repository because of some modifactions we made(zypper and yum provider). And we probably didn’t merge with the tagged version.

I unterstand that multidimensional arrays will not be supported and we will avoid them.

Updated by Walter Heck 7 months ago

  • Category set to templates
  • Target version set to 2.7.x
  • Keywords set to erb, arrays

I’m running into this problem as well, and found out it works in later versions of puppet. Just updating ehre for others to see.

Puppet 2.6.2:

# puppet apply -e '$c = [[1,2,3],[4,5,6]] notice(inline_template("<% c.each do |z| %><%= z[0] %>, <% end %>"))'
notice: Scope(Class[main]): 49, 50, 51, 52, 53, 54, 

in puppet 2.7.6 (possibly earlier 2.7.x versions as well), this works correctly:

# puppet apply -e '$c = [[1,2,3],[4,5,6]] notice(inline_template("<% c.each do |z| %><%= z[0] %>, <% end %>"))'
notice: Scope(Class[main]): 1, 4, 
notice: Finished catalog run in 0.03 seconds

Updated by Daniel Pittman 2 months ago

  • Target version changed from 2.7.x to 2.7.6

Also available in: Atom PDF