More than one array passed to a definition

32 views
Skip to first unread message

Fran Rodríguez

unread,
Aug 29, 2014, 7:40:12 AM8/29/14
to puppet...@googlegroups.com
Hi group,

Im trying to do something with to arrays:

array1 = ['name1', 'name2']
array2 = ['port1', 'port2']

The arrays has the same number of elements and what i want to do it is passing to a define function:

instances { $array1:; $array2:; }

The instances define does something simple:

define instances ( $name, $port ) {
   file { "${name}":
       ensure => present,
       path => "/path/${port}"
   }
}

I thing im missing something about iteration with define function because puppet say:

Error 400 on SERVER: Must pass name to Instances[name1]

Could somebody tell me what im doing wrong or point to me in the correct direction?¿ 

Thanks and cheers

Nan Liu

unread,
Aug 29, 2014, 1:35:38 PM8/29/14
to puppet...@googlegroups.com
Puppet array expansion of resources titles doesn't perform any expansion for resource properties. There might be another way in experimental parser, but here's two options.

1. structure the data so you can pass it to create_resource:
$hash_of_resources = { 'name1' => { 'port' => 'port1'}, 'name2' => { 'port' => 'port2' } }
create_resource('instance', $hash_of_resources)

2. Modify the define type to lookup the value within a hash with the resource title as the key:
# change define type to look up port value via resource name as hash key
# do not specify $name as a variable, it's implicit
define instances ( $port ) {
   $port_file = $port[$name]
   file { "${name}":
       ensure => present,
       path => "/path/${port_file}"
   }
}

$array1 = ['name1', 'name2']
$array2 = ['port1', 'port2']

# this makes it [ ['name1', 'port1'], ['name2', 'port2'] ]
$zip = zip($array1, $array2)

# I can't find a to_hash function in stdlib, but essentially a hideous way to generate: { 'name1' => 'port1', 'name2' => 'port2' }
$port = parseyaml(inline_template("<%= Hash[*@zip.flatten(1)].to_yaml %>"))

instances { $array1:
  port => $port,
}

HTH,

Nan
August 29, 2014 at 4:40 AM
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/1d39746a-dc24-4584-b640-35a96fe1e8af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fran Rodríguez

unread,
Sep 1, 2014, 11:21:51 AM9/1/14
to puppet...@googlegroups.com
Thank you very much for the answer Nan, ive used your first option... ;), and works.

Cheers.
Reply all
Reply to author
Forward
0 new messages