Ask Your Question
2

Writing modules: best strategies to avoid "Duplicate declaration" for files, packages, etc.

asked 2014-06-24 14:25:14 -0500

petkraft gravatar image

New to Puppet. Writing my own modules. Here's a simple case where I've encountered the "Duplicate declaration" error.

For the NTP infrastructure, I felt the requirements for NTP clients are different enough from the requirements for NTP servers to warrant writing separate modules (that may or may not be correct, but it's irrelevant to the issue discussed here). So I've created the ntp-servers and ntp-clients modules. Here are the init.pp files for each. First ntp-servers:

class ntp-servers {
        package { 'ntp':
                ensure          => present,
        }

        file { 'ntp.conf':
                path            => '/etc/ntp.conf',
                ensure          => file,
                require         => Package['ntp'],
                source          => 'puppet:///modules/ntp-servers/ntp.conf'
        }

        file { 'step-tickers':
                path            => '/etc/ntp/step-tickers',
                ensure          => file,
                require         => Package['ntp'],
                source          => 'puppet:///modules/ntp-servers/step-tickers'
        }

        service { 'ntpd':
                name            => $service_name,
                ensure          => running,
                enable          => true,
                subscribe       => File['ntp.conf'],
        }
}

include ntp-servers

And now ntp-clients:

class ntp-clients {
        $packagename = $osfamily ? {
                'RedHat'        => 'ntp',
                'Solaris'       => 'SMCntp',
        }

        package { $packagename:
                ensure          => present,
        }

        file { 'ntp.conf':
                path            => '/etc/ntp.conf',
                ensure          => file,
                require         => Package['ntp'],
                content         => template('ntp-clients/ntp.conf.erb'),
        }

        if $osfamily == 'RedHat' {
                file { 'step-tickers':
                        path            => '/etc/ntp/step-tickers',
                        ensure          => file,
                        require         => Package['ntp'],
                        content         => template('ntp-clients/step-tickers.erb'),
                }
        }

        service { 'ntpd':
                name            => $service_name,
                ensure          => running,
                enable          => true,
                subscribe       => File['ntp.conf'],
        }
}

include ntp-clients

Obviously, that generated a lot of "duplicate declaration" errors.

As I'm writing my own modules, what are some well-known strategies to avoid this happening in the future? What are the general guidelines to make sure duplicates don't happen again?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-06-24 17:26:32 -0500

lavaman gravatar image

updated 2014-06-24 17:29:02 -0500

Actually, having separate modules is not at all irrelevant to your situation. You need to combine the modules into a single ntp module that has a server and client class and have either the base class or an ntp::install that installs the common resources.

In general, when you run into duplicate declaration errors, you've organized your modules incorrectly. You either need to combine modules, or create a common module both of them can include.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-06-24 14:25:14 -0500

Seen: 4,622 times

Last updated: Jun 24 '14