miitak
miitak

Reputation: 19

Puppet: if statment inside a custom resource

I am trying to write a manifest that iterates through an array to create and maintain application users. The list of users is passed to the manifest from init.pp in the following fashion:

$app_users = [ at1,et1,at2,et2 ]

The users.pp manifest then reads the array to create the users:

 define appuser {
    user { $name:  
    ensure           => 'present',
    comment          => "Application User - $name",
    gid              => 'app',
    home             => "/apps/$app/$name",
    shell            => '/usr/bin/bash',
  }
 }

 appuser  { $app_users: }

This solution worked very well with another module I wrote, but in this case the home directory path includes a variable, that depends on the user name.

Adding the following if statement inside the resource issues an error:

if $name =~ /^(et|ep)/ {
 $app = "echos"
 notice('app is $app')
 }

Syntax error at 'if'; expected '}' at .../users.pp:9

I read somewhere that you cannot place an if statement inside a resource...

In that case, what are my options? I'm experiencing a coding block...

We're using puppet master version 2.7.19 (Puppet Enterprise 2.7.0).

Upvotes: 1

Views: 6933

Answers (2)

iamauser
iamauser

Reputation: 11469

I would prefer case than if statement in this case, e.g.,

   define appuser {
        user { $name:  
        ensure           => 'present',
        comment          => "Application User - $name",
        gid              => 'app',
        home => $name ? { 
        '/^(et|ep)/' => "/apps/echos/$name",
         default => "/apps/$app/$name",},
        shell            => '/usr/bin/bash',
      }
   }

Upvotes: 2

Kit Ho
Kit Ho

Reputation: 26968

it sounds like it is only a syntax error, you should add your if statement within the definitions, sth like

define appuser {


    if $name =~ /^(et|ep)/ {
      $app = "echos"
      notice('app is $app')
    }

    user { $name:  
    ensure           => 'present',
    comment          => "Application User - $name",
    gid              => 'app',
    home             => "/apps/$app/$name",
    shell            => '/usr/bin/bash',
  }
 }

Upvotes: 2

Related Questions