Ask Your Question
0

Passing array between class

asked 2018-01-24 04:19:42 -0600

Florian gravatar image

Hello,

I'm very new to puppet development module, and I'm trying to pass an array between class and class parameter, then each it. Could you help me please ?

Here's the code :
init.pp

class test (
    $my_array = ['fqdn.foo.bar', 'fqdn.bar.foo'],
) {
    notice( "${my_array}" )
    test::loopit { $my_array: }
}

and loopit.pp

define test::loopit (
    $my_array = $title,
) {
    $my_array.each |$index, $value| {
         notice( "Key is ${key} and value ${value}" )
    }
}

the output looks like :

<truncted> [puppetserver] Scope(Class[Test]) [fqdn.foo.bar, fqdn.bar.foo]
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 0 and value f
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 1 and value q
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 2 and value d
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 3 and value n
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 4 and value .
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 5 and value f
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 6 and value o
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 7 and value o
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 8 and value .
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 9 and value b
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 10 and value a
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.foo.bar]) Key is 11 and value r
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 0 and value f
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 1 and value q
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 2 and value d
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 3 and value n
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 4 and value .
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 5 and value b
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 6 and value a
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 7 and value r
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 8 and value .
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 9 and value f
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 10 and value o
<truncted> [puppetserver] Scope(Test::Loopit[fqdn.bar.foo]) Key is 11 and value o

Instead iterating over index, the code is iterating over each character. Do you have any suggestion ? Thanks.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2018-01-24 13:56:00 -0600

The output is plausible.

If you specify an array of strings in front of the colon (as the resource title), for each array's element a resource is created, see documentation: array of titles.

So, as you can see, there are the resources Test::Loopit[fqdn.foo.bar] and Test::Loopit[fqdn.bar.foo]. Since test::loopit defaults to $title as $my_array, $my_array is a string. Resource titles _have_ to be strings (Type[$title] = String‼).

Ergo, your lambda iterates over each character.

You'd have to write something like:

class test (
  $my_array = ['fqdn.foo.bar', 'fqdn.bar.foo'],
) {
  notice("${my_array}")
  test::loopit { 'foobar':
    my_array => $my_array,
  }
}
edit flag offensive delete link more
0

answered 2018-01-25 02:48:29 -0600

Florian gravatar image

Hello Kai Burghardt;

Thanks for your answer, I didnt understand how "$title" works.

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

Stats

Asked: 2018-01-24 04:19:42 -0600

Seen: 168 times

Last updated: Jan 25