Ask Your Question
0

Optional Hash in puppet

asked 2017-08-23 15:33:07 -0600

CBR gravatar image

Hi,

I'm trying to create an optional hash variable in puppet but it gives me an error. What am I doing wrong here ?

 define mym::usr_acct (
  String $user = $title,
  String $group,
  Integer $uid,
  Integer $gid,
  String $home,
  Optional[Hash[String, Integer]] $groups,
){

It gives me below error saying it expects an integer. Error: Evaluation Error: Error while evaluating a Resource Statement, Mym::Usr_acct[websvc]: parameter 'groups' entry 'name' expects an Integer value, got String at /tmp/a.pp:1 on node centos.vm

This is what I have in /tmp/a.pp

mym::usr_acct { websvc :   
    group   => 'websvc',   
    uid        =>  4501,   
    gid        =>  507,   
    home       => '/home/websvc',   
    groups     => {
            name => 'allok',
            id =>   9999,   }, }
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-08-23 16:04:58 -0600

Emerson Prado gravatar image

The documentation on hash parameters show that the types are specified for the keys and values. That is: Hash[String, Integer] expects a hash with String keys and Integer values.

For you to validate values of different types, you can use the abstract type Struct:

Optional[Struct[{name => String, id => Integer}]]

edit flag offensive delete link more

Comments

Thank you. This solved my problem.

CBR gravatar imageCBR ( 2017-08-24 06:30:02 -0600 )edit
2

answered 2017-08-24 04:42:02 -0600

Henrik Lindberg gravatar image

You already got the answer for the specific problem (use a Struct allows different types per Hash key). You have however an additional problem - the specification of Optional means that an undef is accepted, but you have no default expression. You should have = undef as a default value expression. Without that default value expression you cannot omit giving a value for groups since a given undef there means "use the default" (and there is none).

edit flag offensive delete link more

Comments

yup. I faced this issue and already using undef.

CBR gravatar imageCBR ( 2017-08-24 06:30:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-08-23 15:33:07 -0600

Seen: 91 times

Last updated: Aug 24