Is there a better way to limit module actions based on facts?

79 views
Skip to first unread message

leam hall

unread,
Feb 10, 2015, 4:18:05 PM2/10/15
to puppet...@googlegroups.com
We have a mixed environment. I wrote a module that would only do stuff on Red Hat family boxes. Under the class definition I put:

  if $::osfamily == 'RedHat' {

and did all the package and file definitions under that. Is there a better or cleaner way to do this?

Leam

Arpin Dominique (Nter)

unread,
Feb 10, 2015, 5:24:08 PM2/10/15
to <puppet-users@googlegroups.com>
Hi,

You could use hiera with osfamily

Regard,

Dominique Arpin
--
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<mailto:puppet-users...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/d76f28b2-c2cf-4f90-91bf-229da4698df4%40googlegroups.com<https://groups.google.com/d/msgid/puppet-users/d76f28b2-c2cf-4f90-91bf-229da4698df4%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

Mise en garde concernant la confidentialité : Le présent message, comprenant tout fichier qui y est joint, est envoyé à l'intention exclusive de son destinataire; il est de nature confidentielle et peut constituer une information protégée par le secret professionnel. Si vous n'êtes pas le destinataire, nous vous avisons que toute impression, copie, distribution ou autre utilisation de ce message est strictement interdite. Si vous avez reçu ce courriel par erreur, veuillez en aviser immédiatement l'expéditeur par retour de courriel et supprimer le courriel. Merci!

Confidentiality Warning: This message, including any attachment, is sent only for the use of the intended recipient; it is confidential and may constitute privileged information. If you are not the intended recipient, you are hereby notified that any printing, copying, distribution or other use of this message is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email, and delete it. Thank you!

Joseph Karns

unread,
Feb 11, 2015, 8:09:36 AM2/11/15
to puppet...@googlegroups.com
Hello Lean:

The way to handle this the best is exactly what you did. OS level specifications should be made at the module level.

I have seen some try to handle mixed environments with the heira hierarchy, but this makes the modules you build less portable. you would also need to create multiple versions of each module to handle each OS family.

Thanks
Joseph

leam hall

unread,
Feb 11, 2015, 10:55:50 AM2/11/15
to puppet...@googlegroups.com
Trying to use a case statement with a setup.pp file that uses hiera and parameters. We have pulled a module from the forge and are trying to make it fit our needs.

The issue I'm trying to resolve is to have the user's home directories created on /home/$user on Red hat systems and /export/home/$user on Solaris nodes. So far I've tried to use a case statement based on osfamily, but I'm getting an error.

Code:
  define sysusers::setup($hash) {

  if(!defined(User[$name])) {
    user { $name :
      case $osfamily {
        'RedHat':  { home  =>  "/home/$hash[$name]['name']"}
        'Solaris': { home  =>  "/export/home/$hash[$name]['name']"}
        default:   { home  =>  "/home/$hash[$name]['name']"}
        }
      ensure               => $hash[$name]['ensure'],
      comment              => $hash[$name]['comment'],
      expiry               => $hash[$name]['expiry'],
      gid                  => $hash[$name]['gid'],

Error:

    Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at 'osfamily'; expected '}' at /etc/puppetlabs/puppet/environments/theblack/modules/sysusers/manifests/setup.pp:5

Module:
  https://forge.puppetlabs.com/mthibaut/users

Case statement reference:
    https://docs.puppetlabs.com/puppet/latest/reference/lang_conditional.html#case-statements

Joseph Karns

unread,
Feb 11, 2015, 1:33:41 PM2/11/15
to puppet...@googlegroups.com
Leam:

The reason you are getting the 400 Error is because you don't have $osfamily defined.

You can write your case statement as such:

case $::osfamily {
   'redhat': {
     # do something RHEL specific
   }
   'debian': {
     # do something Debian specific
   }
   default: {
     # ...
   }
}

Thanks
Joey

Garrett Honeycutt

unread,
Feb 11, 2015, 2:34:53 PM2/11/15
to puppet...@googlegroups.com
On 2/11/15 7:33 PM, Joseph Karns wrote:
> Leam:
>
> The reason you are getting the 400 Error is because you don't have
> $osfamily defined.
>
> You can write your case statement as such:
>
> case $::osfamily {
> 'redhat': {
> # do something RHEL specific
> }
> 'debian': {
> # do something Debian specific
> }
> default: {
> # ...
> }
> }
>
> Thanks
> Joey
>

Hi Leam,

I agree with Joseph's example here of using the case statement with the
addition that the default case be to fail(). This way the module will
work for explicitly defined platforms and fail on anything else. Better
to fail the compilation of the catalog than to make assumptions about
the agent and do the wrong thing.

Here's an example[1] from my nscd module.

You will generally just have variables set to different values based on
the platform, though you may have actual resources as well. Just be sure
that you are not duplicating resources in each of the cases as this
violates DRY[2].


[1] -
https://github.com/ghoneycutt/puppet-module-nscd/blob/master/manifests/init.pp#L103-172

[2] - http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Best regards,
-g

--
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658

leam hall

unread,
Feb 11, 2015, 3:31:01 PM2/11/15
to puppet...@googlegroups.com
Hey all,

I'm still getting stuck, and I've moved the case statement to several
places. The issue is the module's init.pp calls setup.pp. The user
data is stored in Hiera. So far I've tried:

1. Going between $osfamily, $::osfamily, ${osfamily}, {$osfamily}, and
${::osfamily}. The failures seem to be related to syntax, but I'm not
sure how to modify or place the case statement to meet syntax
requirements.

2. I put the case in the init.pp file and had it set $_homedirbase.
Passed that to the call to create the user. Didn't work.

I'm mostly confused.

Leam
--
Mind on a Mission

Joseph Karns

unread,
Feb 11, 2015, 4:18:36 PM2/11/15
to puppet...@googlegroups.com
Hello Leam:

I think your looking for something like this:

-----------------------------

define sysusers::setup(
    $hash
) {
   

    case $::osfamily {
        'redhat': {
            $user_home = '/home'
        }
        'solaris': {
            $user_home = '/export/home'
        }
        'default': {
            $user_home = '/home'

        }
    }

    if(!defined(User[$name])) {
    user { $name :
        home         => "${user_home}/$hash[$name]['name'],

      ensure    => $hash[$name]['ensure'],
      comment    => $hash[$name]['comment'],
      expiry    => $hash[$name]['expiry'],
      gid            => $hash[$name]['gid'],
    }
  }
}

---------------------------------------

Let me know

Thanks
Joey

leam hall

unread,
Feb 11, 2015, 4:49:38 PM2/11/15
to puppet...@googlegroups.com
Fortunately I'm doing this on one box. It put the entire user hash
into each line in /etc/passwd. Still cleaning up. :)

Joseph Karns

unread,
Feb 12, 2015, 8:30:49 AM2/12/15
to puppet...@googlegroups.com
Leam:

Can you reply with your code up too this point. Please include your heira file, so I can see the hash.

Thanks
Joey
Reply all
Reply to author
Forward
0 new messages