stdlib keys() and values()

21 views
Skip to first unread message

Robert

unread,
Sep 28, 2017, 6:45:06 AM9/28/17
to puppet...@googlegroups.com
Hello List,

I can't think of a more basic question, but...

$hash = {'a'=>1, 'b'=>2, 'c'=>3}

notify { "sdf":

  message => values($hash),

}

Robert

unread,
Sep 28, 2017, 6:49:12 AM9/28/17
to puppet...@googlegroups.com
Eh, sorry.

So why's this with the above code:

Notice: /Stage[main]/Main/Notify[sdf]/message: defined 'message' as '1'

and keys($hash) gives 'a'

Why not arrays of the keys / values? Can't understand.

Thanks
Rp

jcbollinger

unread,
Sep 28, 2017, 9:02:55 AM9/28/17
to Puppet Users


On Thursday, September 28, 2017 at 5:49:12 AM UTC-5, Robert wrote:
Eh, sorry.

So why's this with the above code:

Notice: /Stage[main]/Main/Notify[sdf]/message: defined 'message' as '1'

and keys($hash) gives 'a'

Why not arrays of the keys / values? Can't understand.

Although it is not explicitly spelled out in the docs, it should not be much of a surprise that the Notify type expects its 'message' parameter ("The message to be sent to the log") to be a string.  When you provide an array instead, Notify must either reject it with an error, or convert the array to a string by some unspecified mechanism.  In the version of Puppet you are running, it opts in such cases to convert the array to a string by choosing its first element (not necessarily recursively).  I'm uncertain whether this is intentional behavior or simply what happens to fall out from the implementation in this particular variation on providing a value of the wrong type.

If you want to control how the arrays you're working with are converted to strings, then you ought to perform the conversion explicitly,  Details vary to some extent based on what Puppet version you are using (the link goes to the docs for Puppet 4).


John

Robert

unread,
Sep 28, 2017, 9:39:04 AM9/28/17
to puppet...@googlegroups.com
Hey John,

I've just found out that notice() gives back the array indeed.

Thanks for clarifying that, I didn't know this until now and was really confused :)

Best,
Rp

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/e08aedc0-f6f7-46de-b634-c2b2ceb76fbb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages