Defined resource type, lambda block

58 views
Skip to first unread message

Manuela Mueller

unread,
Mar 30, 2016, 9:57:11 AM3/30/16
to Puppet Users
Dear All,

I'm trying to set up a small module in order to test defined resource types. Module should create 2 users, data are in host specific yaml file pieps.yaml.
When I try to apply the module I receive the following error message:

[root@pieps modules]# puppet apply --noop --modulepath=./ deftype_users_test/tests/init.pp
Error: Evaluation Error: The value 'name' cannot be converted to Numeric. at /root/Desktop/testing/modules/deftype_users_test/manifests/init.pp:3:40 on node pieps.localdomain

As I'm just beginning to learn Puppet, I have no idea where the error is.
Any help would be greatly appreciated.

Thank you very much and have a nice day,

Manula Atoui

Hiera configuration:
======================
puppet master --configprint all
...
hiera_config = /etc/puppetlabs/code/hiera.yaml

cat /etc/puppetlabs/code/hiera.yaml

hiera.yaml:
---

:backends:
  - yaml
:hierarchy:
# orig  - "nodes/%{::trusted.certname}"
  - "hostname/%{facts.hostname}"
  - "os/%{facts.osfamily}"
  - common

:yaml:
:datadir: /etc/puppetlabs/code/hieradata


I set up a yaml file for host pieps:
/etc/puppetlabs/code/hieradata/hostname/pieps.yaml

--- # start yaml file
message: "sample message from hiera in pieps.yaml"
# deftype_users_test, defined resource type 'create'
user:
  jill:
    uid: 10000
    comment: 'user jill, test defined resource type'
  jack:
    uid: 10001
    comment: 'user jack, test defined resource type'

Structure module deftype_users_test
=====================================
[root@pieps deftype_users_test]# tree
.
├── manifests
│   ├── create.pp
│   ├── create.pp~
│   ├── init.pp
│   └── init.pp~
└── tests
    └── init.pp

2 directories, 5 files

[root@pieps deftype_users_test]# cat manifests/init.pp
class deftype_users_test( Array[Hash] $userlist = [] ) {
  userlist.each |$user| {
    deftype_users_test::create { $user['name']:
      uid => $user['uid'],
      comment => $user['comment'],
    } # end call defined resource type 'create'
  } # end lambda block
} # end class deftype_users_test


[root@pieps deftype_users_test]# cat manifests/create.pp
define deftype_users_test::create(
  Integer $uid,
  String $comment,
) {
  user { $title:
    uid => $uid,
    comment => $comment,
  }
} # end defined resource type 'create'

[root@pieps deftype_users_test]# cat tests/init.pp
include deftype_users_test

Henrik Lindberg

unread,
Mar 30, 2016, 10:58:11 AM3/30/16
to puppet...@googlegroups.com
On 30/03/16 12:51, 'Manuela Mueller' via Puppet Users wrote:
> deftype_users_test::create { $user['name']:

$user here is probably an array and it expects the "key" to be numeric

Regards
- henrik

--

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

Martin Alfke

unread,
Mar 30, 2016, 11:14:23 AM3/30/16
to puppet...@googlegroups.com
Hi Manuela,

On 30 Mar 2016, at 12:51, 'Manuela Mueller' via Puppet Users <puppet...@googlegroups.com> wrote:

> Dear All,
>
> I'm trying to set up a small module in order to test defined resource types. Module should create 2 users, data are in host specific yaml file pieps.yaml.
> When I try to apply the module I receive the following error message:
>
> [root@pieps modules]# puppet apply --noop --modulepath=./ deftype_users_test/tests/init.pp
> Error: Evaluation Error: The value 'name' cannot be converted to Numeric. at /root/Desktop/testing/modules/deftype_users_test/manifests/init.pp:3:40 on node pieps.localdomain
>
> As I'm just beginning to learn Puppet, I have no idea where the error is.
> Any help would be greatly appreciated.
>
> Thank you very much and have a nice day,
>
> Manula Atoui


You want Puppet to do automatic data lookup for the class ‘deftype_users_test’ parameter.
In this case the hiera key has to follow naming convention:

<classname>::<parametername>

in your case:

deftyp_users_test::user list:
‘jill’:
uid: 1000
‘jack’:
uid: 1001

Within your puppet class you iterate over the hash:

class deftype_users_test (
Hash $userlist
){
$userlist.each | String $key, Hash $value| {
notify { “User with name: ${key} and hid ${value[uid]}”: }
}
}

hth,
Martin
> --
> 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/cb8fcf42-80a7-499e-841d-5bda1b3a2369%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Manuela Atoui

unread,
Mar 31, 2016, 6:24:14 AM3/31/16
to Puppet Users
Hello Henrik, hello Martin,

thank you both for your input, it really helped!
Was a bit stuck on this...

Best regards and have a nice day,

Manuela Atoui

Reply all
Reply to author
Forward
0 new messages