Search code examples
puppet

Puppet exception handling?


I was wondering how one would do try/catch/throw type exception handling in a puppet manifest. Here's how I wish puppet would work ...

class simple {
    unless ( package { 'simple': ensure => present } ) {
        file { '/tmp/simple.txt':
            content => template( 'simple/simple.erb' ), 
        }
    }
} 

Thanks


Solution

  • I don't think there is an exception handling in a programmatic way you would like in Puppet. If you declare a resource, it is expected that puppet brings your machine to that state (installed package) and if not, it will fail automatically. One thing that you can do (and I don't recommend) and that is not "puppet way" is following:

    1. Create custom facter (not custom function since it is executed on puppet master and you want this ruby code to be executed on puppet agent)
    2. Since it is plain ruby code in facter, you can have exception handling and all programmatic things. You can install package as unix command from puppet code and have some logic which will, if not installed retrieve some value as fact
    3. You would use this fact value and based on it you would determine if you want to create file or not

    Also, if easier, you can write bash script which will do this logic and execute it from puppet using exec resource

    Hope it helps.