Puppet Tip: Preinstall Stage

If you have some commands that you want to run before everything else, it can be annoying to add a require statement to every resource (as Puppet doesn’t execute things in order). Luckily you can get around this using stages.

stage { 'preinstall':
  before => Stage['main']
}

class apt_get_update {
  file { '/etc/apt/sources.list':
    ensure => 'file',
    source => '/vagrant/puppet/sysconfig/apt/sources.list',
    notify => [Exec['Import GPG keys'], Exec['Update Aptitude sources']],
  }

  exec { 'Import GPG keys':
    command => 'apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 3B4FE6ACC0B21F32',
    require => File['/etc/apt/sources.list'],
    refreshonly => true,
  }

  exec { 'Update Aptitude sources':
    command => 'apt-get update',
    require => Exec['Import GPG keys'],
    refreshonly => true,
  }
}

class { 'apt_get_update':
  stage => preinstall
}

The above code will execute before the “main” stage (e.g. everything).

See: Puppet Run Stages

One Reply to “Puppet Tip: Preinstall Stage”

  1. Thanks a lot!

    Fixed a provisioning issue that I’ve been trying to fix for a couple of hours!

    Great code snippet.

Leave a Reply