A collection of task oriented solutions in Puppet

 

Nicer exec resource names

Challenge

You're using the command in the exec resource name and it's ugly, unwieldy and awkward to use from other resources.

Solution

class nicer_exec_names {

  # exec with the command as the name
  exec { '/bin/mkdir -p /tmp/needed/directory': }

  package { 'needed':
    ensure  => 'installed',
    # awkward, long require
    require => Exec['/bin/mkdir -p /tmp/needed/directory'],
  }


  # nicer, human friendly naming
  exec { 'create_needed_directory':
    command => '/bin/mkdir -p /tmp/needed/directory',
  }

  package { 'needed':
    ensure  => 'installed',
    # easier to read require
    require => Exec['create_needed_directory'],
  }

}

Explanation

Older manifests, as well as newer ones written by less experienced puppet users, often use the actual command as the name of the exec. While this works, and can be used in other resources relationship parameters such as require and notify, it is unwieldy and leads to a unneeded leaking of implementation details to other resources.

It's much nicer to use a human readable explanation of what the command is for as the execs name and then put the actual command in the command property. This also makes it easier to read related resources, instead of the long and complicated command being used in the package resources require property here:

class messy_exec_relations {

  exec { '/usr/local/bin/cache-tuner -p 3 -d 34 -t name -i btree':

  package { 'needed':
    ensure  => 'installed',
    require => Exec['/usr/local/bin/cache-tuner -p 3 -d 34 -t name -i btree'],
  }

}

You can instead hide the complexity of the command behind an easier to read, more context relevant, exec title and then use that in other related resources.

class cleaner_exec_relations {

  # descriptive name
  exec { 'remove_old_packages':
    command => '/usr/local/bin/cache-tuner -p 3 -d 34 -t name -i btree',
  }

  package { 'needed':
    ensure  => 'installed',
    # the require doesn't know exactly what the exec does
    require => Exec['remove_old_packages'],
  }

}