Home > General, Puppet > Conditionally Installing Packages With Puppet

Conditionally Installing Packages With Puppet

March 15th, 2012

If you want to install a package using puppet only if another package is already installed, you can use puppet’s virtual resources to accomplish this. The proper way to do this is to define your two classes and then realize the virtual package in the dependent class. For example, if I wanted to install php5-dev only if gcc was installed, I would make two modules: a gcc module and a php5 module.

In the php5 module:


class php5($type) {
    package { 'php5-common':
        ensure => installed,
    }
    package { 'php5-cli':
        ensure => installed,
        require => Package['php5-common'],
    }
    @package { 'php5-dev':
        ensure => installed,
        tag => 'develpkgs',
    }
}

The ‘@’ symbol defines the php5-dev package as a virtual resource, so it doesn’t actually get realized when the puppet manifest is compiled unless some other module realizes it. To realize it, we go into our gcc module:


class gcc {
    package { 'gcc': ensure => installed, }
    package { 'g++': ensure => installed, }
    package { 'make': ensure => installed, }
    Package <| tag == 'develpkgs' |>
}

This will search through all of your modules and realize any virtual resource that is tagged with ‘develpkgs’. So for example, if you have another module called mysql and you want to install the mysql development package:


class mysql {
    package { 'mysql': ensure => installed, }
    package { 'mysql-server': ensure => installed, }
    @package { 'libmysqlclient-dev':
        ensure => installed,
        tag => 'develpkgs',
    }
}

General, Puppet ,

  1. No comments yet.
  1. No trackbacks yet.