How use puppet data types in templates

104 views
Skip to first unread message

Ilya

unread,
Feb 2, 2018, 11:05:26 AM2/2/18
to Puppet Users

Hi, how use in correct way puppet data types when you use call_function and lookup in templates, for example

puppet server version 4.9

<%= scope.call_function('lookup', ['somekey', 'Array', 'deep']) -%>

In this way there is an error.

Henrik Lindberg

unread,
Feb 2, 2018, 2:49:42 PM2/2/18
to puppet...@googlegroups.com
On 02/02/18 12:16, Ilya wrote:
> Hi, how use in correct way puppet data types when you use call_function
> and lookup in templates, for example
>
> puppet server version 4.9
>
> |<%=scope.call_function('lookup',['somekey','Array','deep'])-%>|
>
> In this way there is an error.
>
Yes, that will not work because 'Array' is a string and not an instance
of a data type. There is a type parser that can be used to turn strings
into data type instances. Strongly recommend to use EPP instead of ERB
though - as that would completely remove the problem and you would
simply write:

<%= lookup('somekey', Array, 'deep') %>

If you really need to stay with ERB you can call the type parser (give
type as string) or the type factory, or work directly with the types.

Using the type parser is recommended and specifying the types as strings
as that is how it is done in the 4.x function API and the code is the
easiest to read.

For example (using type parser):

<%=scope.call_function('lookup',
['somekey',
Puppet::Pops::Types::TypeParser.singleton.parse('Array'),
'deep'])-%>


or example (using type factory):

<%=scope.call_function('lookup',
['somekey',
Puppet::Pops::Types::TypeFactory.array_of(
Puppet::Pops::Types::TypeFactory.any),
'deep'])-%>

In your particular case you could use an available constant for
Array[Any] like this:

<%=scope.call_function('lookup',
['somekey', Puppet::Pops::Types::PArrayType::DEFAULT, 'deep'])-%>

but calling the types directly is only convenient when the DEFAULT is
suitable.

Hope this helps.
Best,
- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Reply all
Reply to author
Forward
0 new messages