27

Is there a way to force puppet to do certain things first? For instance, I need it to install an RPM on all servers to add a yum repository (IUS Community) before I install any of the packages.

Jon Haddad
  • 1,372
  • 3
  • 13
  • 20
  • For a more complex use case, see also my [related question](http://serverfault.com/questions/430123/how-can-i-tell-puppet-if-i-declare-class-x-apply-its-resources-before-class-y). – Matt McClure Sep 20 '12 at 17:06

7 Answers7

37

If you want to make sure a repository is installed on all your server then I would suggest something like this

node default {
   include base
}

class base {
   yumrepo { "IUS":
      baseurl => "http://dl.iuscommunity.org/pub/ius/stable/$operatingsystem/$operatingsystemrelease/$architecture",
      descr => "IUS Community repository",
      enabled => 1,
      gpgcheck => 0
   }
}

Then, for any node that extends base you can say

class foo {
   package { "bar": ensure => installed, require => Yumrepo["IUS"] }
}

This will ensure that

  • The package bar will not be installed unless the IUS repository is defined
  • The package will not attempt to install before the IUS repository is defined
Dave Cheney
  • 18,567
  • 8
  • 49
  • 56
  • Ah, ok. This is pretty awesome. I didn't see the yumrepo in there - thanks! – Jon Haddad Feb 25 '10 at 20:11
  • It only works with distros that use Yum, so Rhel5 only, not Rhel4 – Dave Cheney Feb 25 '10 at 20:12
  • 2
    What happens if I don't know what repository has a package? For example a package might be available in the *fedora* repository on Fedora and in the *epel* repository on RHEL, CentOS etc. This means that if I use a 3rd party recipe/module I'll have to customize it. – Cristian Ciupitu Jul 06 '11 at 00:36
18

Although stages can handle this and so can specific yum repo dependencies, better is to declare the relationship generically.

Just put Yumrepo <| |> -> Package <| provider != 'rpm' |> in your puppet manifest.

node default {
  Yumrepo <| |> -> Package <| provider != 'rpm' |>
}

This makes it so that all yumrepo types will get processed before any packages that don't have 'rpm' as their provider. That latter exclusion is so that I can use the (for example) epel-release RPM package to help install the yum repo.

Steven Roberts
  • 181
  • 1
  • 2
8

(I found this question after I replied almost the same.. so thought my answer applies here as well and it's worth repeating it (it's safer to have an answer in two places..)

As far as I understand, this is exactly what stages are for -- they let you group and order class executions. I use "stages" to update and configure APT at Debian servers, which should be very similar to what you are going to do with YUM.

First of all, you declare "yum" stage at top level (above "nodes"), so that classes in "yum" stage will be executed before "main" ones:

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

Then, you assign stage to the classes. You can do this right in your node definition:

node default {
  class { 'yumrepos' : stage => yum }

  include packages
}
Alexander Azarov
  • 3,550
  • 21
  • 19
5

You could use Tags. This would allow you to tag the repo installer with firstrun or something,

then run

 puppetd --tags firstrun

and it would only execute the modules/statements matching the tag.

Tom O'Connor
  • 27,480
  • 10
  • 73
  • 148
3

The key thing you need to use is the require keyword - "Evaluate one or more classes, adding the required class as a dependency."

An example using an apt repository could be:

class installcustompackages {
   # make sure we have the repository file and public key
   file { "/etc/apt/sources.list.d/myrepo.list":
      source => "puppet://puppet/files/etc/apt/sources.list.d/myrepo.list",
      ensure => present;
          "/etc/apt/trusted.gpg":
      source => "puppet://puppet/files/etc/apt/trusted.gpg",
   }

   # do an update whenever the list or trusted key file change
   exec { "/usr/bin/apt-get update":
      alias => "aptgetupdate",
      require => [ File["/etc/apt/sources.list.d/myrepo.list"], File["/etc/apt/trusted.gpg"] ],
      subscribe => [ File["/etc/apt/sources.list.d/myrepo.list"], File["/etc/apt/trusted.gpg"] ],
      refreshonly => true;
   }

   package { "mypackage":
      ensure => latest,
      require => Exec["aptgetupdate"];
             "mypackage2":
      ensure => latest,
      require => Exec["aptgetupdate"];
   }

   service { "myservice":
      enable => false,
      require => Package[mypackage];
   }
}

(Adapted from this example of puppet bootstrapping).

So you can see how each stage requires that the previous one be done first. I'll leave you to work out how to apply this to yum as I'm not familiar with where it stores it's files.

Hamish Downer
  • 9,420
  • 6
  • 38
  • 51
0

Puppet reads the config from top to bottom, so if you include a class with the yum repo first in that class, this repo will be added before anything else.

If you use the require settings on a package, you will ensure that the required resource type is present before adding the package, as such:

node 'yournode.domain.com' {
   package { "bar": ensure => installed, require => Yumrepo["IUS"] }
   yumrepo { "IUS":
      baseurl => "http://dl.iuscommunity.org/pub/ius/stable/$operatingsystem/$operatingsystemrelease/$architecture",
      descr => "IUS Community repository",
      enabled => 1,
      gpgcheck => 0
   }
}

This code above will add the repo before adding the package.

tore-
  • 1,396
  • 2
  • 10
  • 18
  • 10
    Puppet uses a declarative language, so it does **not** worry about the order of things in the file. Your first paragraph is wrong I'm afraid. The correct way to do this is to use the `require` keyword, which you have done in the second part. – Hamish Downer Jul 12 '10 at 09:34
0

Something like this worked for me:

yumrepo { A:
  descr    => "A repo",
  baseurl  => '',
  enabled  => 1,
  gpgcheck => 1,
  gpgkey   => "",
  priority => 3
}

yumrepo { B:
  descr    => "B repo",
  baseurl  => '',
  enabled  => 1,
  gpgcheck => 1,
  gpgkey   => "",
  priority => 3
}

yumrepo { C:
  descr    => "C repo",
  baseurl  => '',
  enabled  => 1,
  gpgcheck => 1,
  gpgkey   => "",
  priority => 3;
}

Package {
  require => [Yumrepo[A], Yumrepo[B], Yumrepo[C]]
}

I included something like this on mysite.pp. In this way, your puppet modules, are free of references to yum repos.

Andrew Williams
  • 667
  • 8
  • 20