On Monday, March 25, 2013 9:53:50 AM UTC-5, Andrew E. wrote:
>
> Hi all.
> I'm newbie.
> Help me, please.
> I write Puppet function
> Piece of code :
>
>
>         n_if={}
>          over_if = arguments[1]
>         
>      over_if.each do |kk,vv|
>           weth={}
>             puts kk,vv,weth
>             weth = arguments[0]
>             weth['in_vlan'] = vv['in_vlan']
>             weth['options']['MTU'] = vv['mtu']
>         n_if['eth'+ kk.to_s]=weth
>     end
>
> Data gotten from 2 files with Hiera, and passed into arguments[0] and 
> arguments[1] respectively:
>
> # template of ethernet interfaces
> eth_: 
>   method: "static"
>   family: "inet"
>   ip: ""
>   netmask: "255.255.0.0"
>   onboot:  true
>   options: 
>     MTU: ""
>   in_vlan: ""
>
> # values for include into ethernet interfaces
> eth_values:
>  0:
>   mtu: 1500
>   in_vlan: 15
>  1:
>   mtu: 9000
>   in_vlan: 125
>
>
> I expect get hash with keys 'eth0' and 'eth1' as follow:
>
> eth1methodstaticfamilyinetin_vlan125ipnetmask255.255.0.0onboottrueoptionsMTU9000
>
> eth0methodstaticfamilyinetin_vlan15ipnetmask255.255.0.0onboottrueoptionsMTU1500
> But I get :
>
> eth1methodstaticfamilyinetin_vlan125ipnetmask255.255.0.0onboottrueoptionsMTU9000
>
> eth0methodstaticfamilyinetin_vlan125ipnetmask255.255.0.0onboottrueoptionsMTU9000
>
>  I use:
>  Puppet 3.1.1 
>  Hiera 1.6.17
>  Ruby 1.8.7
>
> What is my mistake?
>
>
I had to rename your variables to follow your code, but it looks like your 
mistake is at this line (translating back):
    weth = arguments[0]

That sets variable 'weth' to be a reference to the *same object* that that 
is referenced as arguments[0].  You then proceed to modify that object in 
each iteration of the loop, and assign other references to it to your 
result variable, but in the end it's still only one object.

Here's a version that I think will do what you want:

    template     = arguments[0]
    param_groups = arguments[1]       
    results      = {}
       
    param_groups.each_pair do |ifc_num, params|
        # key difference:
        result = template.clone

        # no need to clone other objects:
        result['in_vlan'] = params['in_vlan']
        result['options']['MTU'] = params['mtu']

        # especially here:
        results['eth' + ifc_num.to_s] = result
    end



John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to