Duplicate declaration

79 views
Skip to first unread message

Ugo Bellavance

unread,
Sep 7, 2018, 9:05:44 PM9/7/18
to Puppet Users
Hi,

I have made a module, a long time ago, that allows me to create directories and httpd config files.

My pattern is /var/www/dev/$devuser/$clientname/$appname/

It worked perfectly until I ended up having more than one $appname for the same $clientname. Here's the error message I get:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: File[/var/www/dev/user1/client1] is already declared in file /etc/puppet/modules/atqapache/manifests/vhost.pp:146; cannot redeclare at /etc/puppet/modules/myapache/manifests/vhost.pp:146 on node server1.example.com

The directive responsible for the creation of the folder is this one:

    file { [ "$client_base", ]:
      ensure  => 'directory',
      owner   => "$owner",
      group   => "$group",
      mode    => 0744,
    }

This line defines the variable: 

    $vhost_base = "${atqapache::params::home}/$envstage/$client-${application}"

Here's my declaration:

  atqapache::vhost { 'client1-user1app1dev1' :
    client         => 'client1',
    envstage       => 'dev',
    application    => 'app1',
    devuser        => 'user1',
  }

  atqapache::vhost { 'client1-user1app2dev11' :
    client         => 'clien1',
    envstage       => 'dev',
    application    => 'app2',
    devuser        => 'user1',
  }

Does anyone know how I could modify my code so that I can have more than one app per client?

Thanks,


Dan White

unread,
Sep 7, 2018, 9:37:29 PM9/7/18
to puppet...@googlegroups.com
Separate the client base directory resource from the application directory resource. 

When declaring each application directory, add a “require” parameter with a value of the client base directory. 

"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."

Bill Waterson (Calvin & Hobbes)

--
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/6a2b9af3-0fb9-4c38-b0a1-7245b2762d38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ugo Bellavance

unread,
Sep 10, 2018, 1:21:27 AM9/10/18
to Puppet Users


On Friday, September 7, 2018 at 5:37:29 PM UTC-4, LinuxDan wrote:
Separate the client base directory resource from the application directory resource. 

When declaring each application directory, add a “require” parameter with a value of the client base directory. 


Thank you for your quick response.

Do you mean that I should create the clients directory separately, feeding it with the list of clients?

I do that already with my devs directories:

  atqapache::varwwwdevuser { [ 'user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7', 'user8', ] :}

Class defined like this:

define atqapache::varwwwdevuser {

  file {
    "/var/www/dev/${title}":
      ensure => directory,
      owner  => "${title}",
      group  => "${title}",
      mode   => 0744,
      require => User["${title}"],
  }
}

Thanks,

Michael Watters

unread,
Sep 10, 2018, 1:46:18 PM9/10/18
to Puppet Users
It sounds like your atqapache::vhost type is attempting to create the /var/www/dev/user1/client1 directory for every vhost that is defined.  Can you post your definition for this type here?

Ugo Bellavance

unread,
Sep 10, 2018, 2:22:18 PM9/10/18
to Puppet Users


On Monday, September 10, 2018 at 9:46:18 AM UTC-4, Michael Watters wrote:
It sounds like your atqapache::vhost type is attempting to create the /var/www/dev/user1/client1 directory for every vhost that is defined.  Can you post your definition for this type here?



Wasn't it in my first post?

Michael Watters

unread,
Sep 10, 2018, 4:18:34 PM9/10/18
to Puppet Users
I'd prefer to see the entire vhost.pp code but it looks like the issue is the same as I mentioned previously, you have multiple atqapache::vhost resources attempting to manage the client base directory.  I've ran into a similar issue with my own modules and was able to hack around it using the defined() function.  For example:

if !defined(File["$client_base"]) {
   
file { [ "$client_base", ]:
      ensure  => 'directory',
      owner   => "$owner",
      group   => "$group",
      mode    => 0744,
    }
}

There may be a better option but this way ensures that the resource is only created once.

jcbollinger

unread,
Sep 11, 2018, 1:31:43 PM9/11/18
to Puppet Users


On Monday, September 10, 2018 at 11:18:34 AM UTC-5, Michael Watters wrote:
I'd prefer to see the entire vhost.pp code but it looks like the issue is the same as I mentioned previously, you have multiple atqapache::vhost resources attempting to manage the client base directory.  I've ran into a similar issue with my own modules and was able to hack around it using the defined() function.  For example:

if !defined(File["$client_base"]) {
   
file { [ "$client_base", ]:
      ensure  => 'directory',
      owner   => "$owner",
      group   => "$group",
      mode    => 0744,
    }
}

There may be a better option but this way ensures that the resource is only created once.


Just about any viable alternative is better than using defined().  If you are in control of all the code that may declare that resource then among the best options are
  • factor out the declaration into a separate class, and declare the class, where needed, via an include-like class declaration.  Unlike resources, classes can safely be declared multiple times, as long as all the declarations except possibly the first-evaluated one are of the include-like kind.
  • A more old-school way would be to move the resource declaration to another existing class that you can rely upon to be included, and make it virtual.  Then realize it in the places where presently you declare it.
  • Or perhaps there is already a class that provides an appropriate context and scope such that you can move the declaration to that class and leave it concrete.
If you do not have full control of all the declarations and cannot obtain it, then
  • At minimum, using the ensure_resource() function from puppetlabs/stdlib is slightly less evil than using defined() for this purpose.

John

Ugo Bellavance

unread,
Sep 11, 2018, 5:11:23 PM9/11/18
to Puppet Users


On Tuesday, September 11, 2018 at 9:31:43 AM UTC-4, jcbollinger wrote:


On Monday, September 10, 2018 at 11:18:34 AM UTC-5, Michael Watters wrote:
I'd prefer to see the entire vhost.pp code

Ugo Bellavance

unread,
Sep 17, 2018, 7:40:30 PM9/17/18
to Puppet Users
To be honest, I used the !defined because I am not good enough with puppet right now to check the other options.  I have made myself a note, though, to revisit this code eventually.

Thanks,
Reply all
Reply to author
Forward
0 new messages