Ask Your Question

exception handling

asked 2016-01-29 12:43:39 -0500

Red Cricket gravatar image

updated 2016-01-30 20:28:14 -0500


Is there way to do exception handling in Puppet (PE 3.7.*)? I wish I could write my puppet manifest like this.

class foo {
    unless ( package {'foo': ensure => installed} ) {
        # Do this stuff if there is a problem installing the package
        file { '/tmp/foo.txt':
            content => template( 'foo/foo.erb' ), 
        ... do more stuff here that RPM would have done ...

Update: As per Alex's suggestion I will explain what I am trying to accomplish. Often in my environment yum repos break. Whoever is in charge of keeping them up to date does a poor job of it. Also often I just want to install simple RPM's that have no dependencies and have a payload of a few files and occasionally execute some simple commands from their %post section like ldconfig. So I was hoping there was away to "catch" failed package resources and execute a set of file and exec resources.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-01-30 04:01:32 -0500

updated 2016-01-31 06:29:13 -0500

Yeah, you're trying to do procedural programming in Puppet. Puppet is a declarative programming language. Let it go, and embrace declarative programming. You haven't explained exactly what you're trying to do, but here's a pattern I use all the time:

class nginx {
  $nginx_uid = '...'
  $nginx_gid = '...'
  group { 'nginx':
    ensure => present,
    gid    => $nginx_gid,
  user { 'nginx':
    ensure     => present,
    uid        => $nginx_uid,
    gid        => $nginx_gid,
    comment    => 'nginx user',
    home       => '/var/cache/nginx',
    shell      => '/sbin/nologin',
    managehome => false,
  package { 'nginx':
    ensure => installed,
  User['nginx'] -> Package['nginx']

Let me know exactly what you're trying to do.


Thanks for the update, yep it sounds very messy.

I would stubbornly resist creating a complicated, messy solution on your side in order to accommodate someone else's inability to build RPMs. Rather, help them fix their issues. I would look into continuous integration in the RPM build process. Have a look at this.

But suppose this is unachievable. What you can still do from your side is to set up continuous integration of your Puppet code that catches the broken RPMs before your Puppet code ever runs. So set up Beaker (Serverspec) so that your code is run automatically in a virtual test environment, and any broken RPMs will be detected before your Puppet code runs in production. At that point, tell the project that you can't proceed until the other team fixes the RPM. Sometimes you need to be a little difficult before the business can start to learn from its mistakes.

OK, and suppose this is also unachievable. You might need to write a custom provider for a yum install with error handling. Then in your manifest you might simply say:

package { 'foo':
  ensure   => installed,
  provider => yum_with_error_handling,

Since providers are written in Ruby, you can handle the RPM installation issues there. Make sure you document your reasons for writing the custom provider.

Good luck!

edit flag offensive delete link more


So there is no exception handling in declarative programming languages?

Red Cricket gravatar imageRed Cricket ( 2016-01-30 13:40:15 -0500 )edit

I can't tell you about all declarative languages, but there are no exceptions in Puppet, so nothing to handle. Why not let us know what you're trying to do.

Alex Harvey gravatar imageAlex Harvey ( 2016-01-30 19:52:38 -0500 )edit

Thanks for comment Alex. I have update my OP with what I am trying to do.

Red Cricket gravatar imageRed Cricket ( 2016-01-30 23:39:06 -0500 )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


Asked: 2016-01-29 12:43:39 -0500

Seen: 642 times

Last updated: Jan 31 '16