Ask Your Question
2

Supply array to file type's path attribute

asked 2014-05-12 12:23:05 -0500

When dealing with websites, I generally list my directory requirements as follows:

file {[
    "/var/www/domains/domain.com",
    "/var/www/domains/domain.com/www",
    "/var/www/domains/domain.com/www/htdocs",
]:
    ensure => directory,
}

This time, however, I have a second project on the same domain:

file {[
    "/var/www/domains/domain.com",
    "/var/www/domains/domain.com/sub",
    "/var/www/domains/domain.com/sub/htdocs",
]:
    ensure => directory,
}

When I tried the above, Puppet threw an error:

Error: Duplicate declaration: File[/var/www/domains/domain.com] is already declared in file /tmp/vagrant-puppet-1/modules-0/ryseapache/manifests/init.pp:52; cannot redeclare at /tmp/vagrant-puppet-1/modules-0/ryseapache/manifests/init.pp:97 on node server01.local.vps

Which kind of makes sense, because titles ought to be unique. Granted, considering all three array elements, the title is unique, but because it is an array, and there is one repeating item, it isn't treated as such.

My next step was to attempt and move the array to the path attribute so that the title could be unique, but that also doesn't seem to work:

file { "domain.com/www":
    path => [
        "/var/www/domains/domain.com",
        "/var/www/domains/domain.com/www",
        "/var/www/domains/domain.com/www/htdocs",
    ],
    ensure => directory,
}

file { "domain.com/sub":
    path => [
        "/var/www/domains/domain.com",
        "/var/www/domains/domain.com/sub",
        "/var/www/domains/domain.com/sub/htdocs",
    ],
    ensure => directory,
}

Is there a way to manage this requirement so that:

  • Each project can ensure that the root domain exists
  • I don't have to create three different file resources per project and customizing the title for the root domain

All suggestions to improve this are welcome.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-05-12 13:08:20 -0500

Stefan gravatar image

specifying

file {[
    "/var/www/domains/domain.com",
    "/var/www/domains/domain.com/www",
    "/var/www/domains/domain.com/www/htdocs",
]: }

is the same creating three individual resources:

file { "/var/www/domains/domain.com": }
file { "/var/www/domains/domain.com/www": }
file { "/var/www/domains/domain.com/www/htdocs": }

and each of the invididual resource has to be unique. The path parameter of a file resource does not handle arrays.

The actual implementation depends on the actual usecase: Is the domain domain.com an item of a limited set or can it be completly random? The same goes with the different projects.

But the basic sketch would be to use classes to make sure the base directories exist and use classes (or defines) that act on top of these base directories.

Example:

class webapp::base {
  file { '/var/www/domains': ensure => directory }
}

class webapp::domain {
  file { '/var/www/domains/domain.com': ensure => directory }
}

define webapp::project($domain) {
  # make sure the base directories exist
  include webapp::base
  case $domain {
    "domain.com": {
      include webapp::domain
    }
    default: {
      fail("Invalid ${domain}. Must be domain.com")
    }
  }

  # just handle project specific paths here
  file { "/var/www/domains/${domain}/${name}": ensure => directory }
  file { "/var/www/domains/${domain}/${name}/htdocs": ensure => directory }
}
edit flag offensive delete link more

Comments

Thanks for the explanation and the useful example on how to manage a dependency for two projects.

Ryan Sechrest gravatar imageRyan Sechrest ( 2014-05-12 13:17:28 -0500 )edit
0

answered 2014-05-12 12:54:14 -0500

you can create a third file resource just to make sure that the directory exists.

like:

$directory = ['/var','/var/www','/var/www/domains/domain.com','/var/www/domains/domain.com/www','/var/www/domains/domain.com/sub','/var/www/domains/domain.com/www/htdocs']

file {$directory:
  ensure => directory,
}

this way you just have one file resource to create directory.

edit flag offensive delete link more

Comments

Thanks for that, but I was trying to keep both project's dependencies separate for clarity.

Ryan Sechrest gravatar imageRyan Sechrest ( 2014-05-12 13:17:26 -0500 )edit

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-05-12 12:23:05 -0500

Seen: 4,818 times

Last updated: May 12 '14