Product SiteDocumentation Site

Examples

para

C.1. Example Defined Type

A defined type is a custom type you can define yourself. This can be a shortcut to a collection of resources given a set of parameters, or just a single resource, as shown in the following examples.
Defined type for a single resource
This is an example defined type that creates a single resource, with just one parameter controlling the source of where the file comes from.
define yum::repository ($enable = true) {
    file { "/etc/yum/repos.d/$name.repo":
        mode => 644,
        owner => "root",
        group => "root",
        backup => false,
        links => follow,
        source => $enable ? {
            true => "puppet:///yum/$os/$osver/repos/$name.repo",
            default => "puppet:///yum/$os/$osver/repos/$name.repo.disabled"
        }
    }
}
You can use this defined type to manage the files in /etc/yum/repos.d/, and thus the repositories available to the YUM package manager, by calling (for example):
yum::repository { "custom":
    enable => true
}
This will create a File["/etc/yum/repos.d/custom.repo"] and grab if from a location on the puppetmaster's fileserver where it has enabled=1 in the repository configuration file. Should $enable have been set to false in the Yum::Repository["custom"] resource, then the file would have been grabbed from the location on the puppetmaster's fileserver where the contents would have enabled=0.
Multiple resources in a defined type
This is an example
define yum::plugin($enable = false) {
    file { "/etc/yum/pluginconf.d/$name.conf":
        ensure => $enable ? {
            true => file,
            default => absent
        },
        owner => "root",
        group => "root",
        mode => 644,
        source => "puppet:///yum/plugins/$name.conf"
    }

    package { "yum-$name":
        ensure => $enable ? {
            true => installed,
            default => absent
        }
    }

    case $name {
        "versionlock": {
            file { "/etc/yum/pluginconf.d/versionlock.list":
                source => "puppet:///yum/plugins/versionlock.list"
            }
        }
    }
}