puppet trying to define global class variables

I'm trying to use puppet as a unix users management.

Here is the class I created to add new users:

class unix_accounts( $username, $forename, $name, $email, $uid) {

user { $username:
    comment => '$forename $name',
    home    => '/home/$username',
    shell   => '/bin/bash',
    uid     => $uid,
    groups  => [$username, 'sudo'],
}

I would like, as basically email and username ca be deduced from $name and $forename, to define these variables in the unix_accounts class. I have tried this:

$username = $forename[0].chr.downcase+name.downcase
$email    = $forename.downcase+'.'+$name.downcase+'@localnet.lan'

But puppetd test on client side fails out at the first dot (.) with the following message:

Error 400 on server: Syntax error at '.'; expected '}' at ....

This has nothing to do with ruby, because I have tried before with irb, it seems to be related to puppet syntax only. Is it possible anyway to created these variables inside a class? How to do that?

Many thanks


Solution 1:

$username = $forename[0].chr.downcase+name.downcase $email
$forename.downcase+'.'+$name.downcase+'@localnet.lan'

What makes you think this will work? None of these are functions provided by the Puppet language.

Have a look at the function documentation to see what you can do with strings directly in Puppet (not much). Then have a look at the template documentation to see how to use ERB templates, which will solve your problem.

Also, and this is not your problem, but it is a problem: if you want your strings to interpolate variables then you have to use double quotes, like so:

user { $username:
    comment => "$forename $name",
    ...