Duplicate declaration error

314 views
Skip to first unread message

Clemens Bergmann

unread,
Jun 8, 2018, 6:40:30 PM6/8/18
to Puppet Users
Hi There,

I have a "can not redeclare" error that I do not understand.

I try to write my own apache::vhost defined type with sane defaults for my environment.

The general class layout is as follows:

class vhost::vhost1 {
custom::apache::vhost{
   #parameters
 }
}
class vhost::vhost2 {
 custom::apache::vhost{
   #parameters
 }
}
define custom::apache::vhost () {
 include custom::apache
}
class custom::apache {
 class { '::apache':
   #parameters
 }
 class { '::apache::mod::ssl':
   #parameters
 }
 include apache::mod::authnz_ldap
 include apache::mod::headers
 include apache::mod::rewrite
 include apache::mod::fastcgi
 apache::listen { '443': }
}



When I define only one custom::vhost then everything works fine.
But when I define multiple custom::vhost then I get the error "Duplicate declaration: Class[Apache] is already declared;" which refers to the line in class "custom::apache" where the "class { '::apache':" is defined.

Can someone explain to me why I get this error?

Thanks.
Clemens

Priyo Phan

unread,
Jun 8, 2018, 8:36:36 PM6/8/18
to puppet...@googlegroups.com
I think it is due to the Apache class being called inside your defined type, it is being called more than once when you try to create more than 1 vhost.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/045a13d7-7bf0-4523-9067-8bbf73c26c89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clemens Bergmann

unread,
Jun 9, 2018, 2:45:46 AM6/9/18
to Puppet Users
What confuses me is that I before switching to puppetlabs-apache I had a layout with a custom apache module as follows:

class vhost::vhost1 {
  custom_apache::site{
#parameters
  }
}

define custom_apache::site (#parameters) {
  include custom_apache::ssl
}

class custom_apache::ssl (#parameters){
  class { 'custom_apache':
   #parameters
 }
}

class custom_apache (#parameters) {
  include custom_apache::params
  class {'custom_apache::install':
    #parameters
  }
  class {'custom_apache::config':
    #parameters
  }
  class {'custom_apache::service':
    #parameters
  }
}

and that worked.

I also suspect the the line https://github.com/puppetlabs/puppetlabs-apache/blob/master/manifests/mod/ssl.pp#L24 to be the problem. But that would also be a problem with only one vhost and that works fine on another node.

Clemens Bergmann

unread,
Jun 10, 2018, 5:50:31 PM6/10/18
to Puppet Users
Hi,

I found out that it does only break if I add one special vhost.
The problematic vhost is defined as follows:

class vhost::vhost3 {
  custom::apache::vhost{
  #parameter
   }

  include ::apache::mod::proxy
  include ::apache::mod::proxy_http
}

If I remove the Includes everything works (but I need mod_proxy for this vhost).

Arnau

unread,
Jun 11, 2018, 3:43:25 AM6/11/18
to puppet...@googlegroups.com
Hi,

"include" shouldn't be a problem: 



the problematic declariation must be one of the resource like declaration. You can only declare a class in such format once.

define custom::apache::vhost () {
  include custom::apache
}
class custom::apache {
  class { '::apache': <- WRONG
    #parameters
  }
}

Your define calls custom::apache and in custom::apache you declar ::apache in the resource-like format as much times as vhost you create.

Could you please show the full code? and the exact error?


Arnau



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/0665b012-601b-4fac-83ff-fa7acfae9fa7%40googlegroups.com.

jcbollinger

unread,
Jun 11, 2018, 9:56:24 AM6/11/18
to Puppet Users


On Monday, June 11, 2018 at 2:43:25 AM UTC-5, Arnau wrote:
Hi,

"include" shouldn't be a problem: 



the problematic declariation must be one of the resource like declaration. You can only declare a class in such format once.


More precisely, when the catalog builder evaluates a resource-like class declaration, it will throw a duplicate declaration error if the class has already been declared, no matter the syntax of prior declarations.  Thus, multiple resource-like declarations of the same class can coexist happily as long as no more than one of them is actually evaluated for any given node, but even a single one can cause a duplicate declaration error if an include-like declaration of the same class is evaluated earlier.

Because they are sensitive to evaluation order and dupication, resource-like class declarations should be avoided, especially across module boundaries.  Rely on automatic data binding instead for setting class parameters.  You may make an exception for module classes declaring other, private classes of the same module via resource-like declarations, as it is reasonable to suppose that you can ensure that the necessary constraints are satisfied in such cases.


John

Reply all
Reply to author
Forward
0 new messages