13

I know the basics of ordering in puppet to run apt-get update before a specific package but would like to specify to just run apt-get update only once and then execute the rest of the puppet file. Is that possible?

All of the ways listed Here need to either run apt-get before every package or use arrows or requires to specify each package.

Community
  • 1
  • 1
isimmons
  • 2,016
  • 2
  • 20
  • 32

2 Answers2

28

This would be my recommendation from that list:

exec { "apt-update":
    command => "/usr/bin/apt-get update"
}

Exec["apt-update"] -> Package <| |>

This will ensure that the exec is run before any package, not that the exec is run before each package. In fact, any resource in puppet will only ever be executed at most once per puppet run.

But if you're wanting the exec to occur before ANY type of resource I guess you could do something like:

exec { "apt-update":
    command => "/usr/bin/apt-get update",
    before  => Stage["main"],
}

The "main" Stage is the default stage for each resource, so this would make the exec occur before anything else.

I hope that this helps.

Sekm
  • 1,658
  • 13
  • 22
  • Thank you Sekm. I was misunderstanding how it worked and thinking if there are 5 packages for example then it would run apt-get update 5 times. Thank you for explaining it and also for the second option as it could come in handy for other things. – isimmons Jul 17 '13 at 02:09
3

With puppetlabs-apt module, it should be enough to define dependency on the module for any package that will be installed:

Class['apt::update'] -> Package <| provider == 'apt' |>

This assumes basic configuration of apt, e.g.:

class { 'apt':
    update => {
      frequency => 'daily',
    },
    purge => {
      'sources.list' => false,
      'sources.list.d' => true,
    },
  }
Tombart
  • 30,520
  • 16
  • 123
  • 136
  • This is the better answer; it's the one recommended in the Puppet docs, and it avoids having to manually specify a command to exec. – David Ehrmann Dec 28 '16 at 20:54
  • After doing that, when running `pdk test unit` I get `dependency cycles found: (Package[gnupg] => Class[Apt] => Package[gnupg])` and tests fail – KamilCuk Jan 17 '22 at 13:58