How to have init.pp in a module run some function whenever any class in the module is called.

51 views
Skip to first unread message

Dan Mahoney

unread,
Jun 4, 2018, 9:24:01 PM6/4/18
to Puppet Users
Hey all,

We have a simple adduser module, with an init.pp like so:

class adduser::config {
   case $facts['operatingsystem'] {
     'Debian': {
       $bash = '/bin/bash'
       $tcsh = '/bin/csh'
       $sh = '/bin/sh'
       $staffgid = '50'
       $wheelgroups = ['adm', 'sudo']
       $rootgroup = 'root'
     }
     default: {
       $bash = '/usr/local/bin/bash'
       $csh = '/bin/csh'
       $tcsh = '/bin/csh'
       $sh = '/bin/sh'
       $staffgid = '20'
       $wheelgroups = ['wheel']
       $rootgroup = 'wheel'
     }
   }
}

This code *was* in the main "adduser" class, but we found that when we called a user directly, it didn't run.

All our users have stanzas like this one:

class adduser::user_ray {
require adduser::config
  user { 'bob':
    ensure   => 'present',
    comment  => 'Bob User',
    gid      => $staffgid,
    home     => '/home/bob',
    password => '*',
    shell    => $tcsh,
    uid      => '20049',
    managehome => true
  }
}

Even with the direct require of adduser::config we still see errors like this in the puppetserver log:

2018-06-04 03:27:24,369 WARN  [qtp1220040994-783] [puppetserver] Puppet Unknown variable: 'tcsh'. at /usr/local/etc/puppet/code/environments/production/modules/adduser/manifests/init.pp:249:17
2018-06-04 03:27:24,377 WARN  [qtp1220040994-783] [puppetserver] Puppet Unknown variable: 'staffgid'. at /usr/local/etc/puppet/code/environments/production/modules/adduser/manifests/init.pp:460:17
2018-06-04 03:27:24,380 WARN  [qtp1220040994-783] [puppetserver] Puppet Unknown variable: 'wheelgroups'. at /usr/local/etc/puppet/code/environments/production/modules/adduser/manifests/init.pp:461:17
2018-06-04 03:27:24,382 WARN  [qtp1220040994-783] [puppetserver] Puppet Unknown variable: 'bash'. at /usr/local/etc/puppet/code/environments/production/modules/adduser/manifests/init.pp:464:17

What's the proper way to do this inheritance?  And the proper way to name variables so they scope right?

Thanks in advance,

-Dan 

Martin Alfke

unread,
Jun 5, 2018, 9:33:38 AM6/5/18
to puppet...@googlegroups.com
Hi Dan,

variables in classes are always local variables.
You can reference variables from other classes by adding the namespace (classname) to the variable.
Referencing variables from other classes is only possible when the class containing the variables has been declared:

class adduser::user_ray {
require adduser::config
user { 'bob':
ensure => 'present',
comment => 'Bob User',
gid => $adduser::config::staffgid,
home => '/home/bob',
password => '*',
shell => $adduser::config::tcsh,
> --
> 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/801c1a76-75e0-4cc7-a7ee-8c7262c3ca70%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages