Virtual resources ?

70 views
Skip to first unread message

Jakov Sosic

unread,
Mar 31, 2016, 10:39:11 AM3/31/16
to Puppet Users
Hi guys,

I have a `test.pp` manifest that looks like:


```
define basket($arg) {
file { "/tmp/$name":
ensure => present,
content => $arg,
}
}

foobar = {
'fruit' => { arg => 'apple' },
'berry' => { arg => 'watermelon' }
}

@basket[$foobar]

realize( Basket[fruit] )
realize( Basket[berry] )
```


When I run `puppet apply test.pp`, this is what I get:

Error: Could not parse for environment production: Virtual (@) can only
be applied to a Resource Expression at /tmp/pero.pp:16:1 on node workstation


Any ideas?

Martin Alfke

unread,
Mar 31, 2016, 11:10:36 AM3/31/16
to puppet...@googlegroups.com
Hi,
You are passing a Hash as an parameter to a define.
Inside the define you expect the parameter to be of type String - according to what you do in the define.

You need to take care of splitting data accordingly in your define:
With Puppet 4 this is very easy to achieve:

define basket (
Hash $arg,
){
$arg.each |String $key, Hash $value| {
file { “/tmp/${key}”:
ensure => file,
content => $value[‘arg’],
}
}
}

hth,
Martin

>
>
> Any ideas?
>
> --
> 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/56FD367F.90000%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Jakov Sosic

unread,
Mar 31, 2016, 11:29:12 AM3/31/16
to puppet...@googlegroups.com
On 03/31/2016 05:10 PM, Martin Alfke wrote:

> You are passing a Hash as an parameter to a define.
> Inside the define you expect the parameter to be of type String - according to what you do in the define.
>
> You need to take care of splitting data accordingly in your define:
> With Puppet 4 this is very easy to achieve:
>
> define basket (
> Hash $arg,
> ){
> $arg.each |String $key, Hash $value| {
> file { “/tmp/${key}”:
> ensure => file,
> content => $value[‘arg’],
> }
> }
> }


Thanks for an idea. I didn't know something like that is possible...



Why couldn't I just use create_resources? If I try with create_resources:

create_resources(@basket, $foobar)


I get this:

Martin Alfke

unread,
Mar 31, 2016, 11:40:49 AM3/31/16
to puppet...@googlegroups.com
You need to fix the content of $foobar

Generally it works:
https://github.com/puppetlabs/puppet/blob/master/lib/puppet/parser/functions/create_resources.rb

But no longer required since Puppet 4:
https://docs.puppetlabs.com/puppet/latest/reference/lang_resources_advanced.html#implementing-the-createresources-function


>
> --
> 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/56FD423A.2080203%40gmail.com.

Jakov Sosic

unread,
Mar 31, 2016, 8:40:18 PM3/31/16
to puppet...@googlegroups.com
Thanks!

I've decided to stick with create_resources :)

Reply all
Reply to author
Forward
0 new messages