Spredzy
Spredzy

Reputation: 5164

Is it possible to define a list of Defined Resource in Puppet?

Let's say for example I have 3 servers, those 3 servers has apache installed. For each of them I have to instantiate several apache::vhost defined resource.

Is there any way that instead of doing that

node 'srv1.example.com' inherit webserver {

   apache::vhost { 'toto1' :
           ... => ... ,
   }

   apache::vhost { 'toto2' :
           ... => ... ,
   }

   apache::vhost { 'toto3' :
           ... => ... ,
   }

}

One can do something with the following pattern (admitting that my defined resource just with the name can do what it needs to)

node 'srv1.example.com' inherit webserver {

     $vhost = ['toto1', 'toto2', 'toto3'];

     ??????
}

Upvotes: 1

Views: 1185

Answers (1)

freiheit
freiheit

Reputation: 5054

Yes.

node 'srv1.example.com' inherit webserver {

  $vhosts = ['toto1', 'toto2', 'toto3'];

  apache::vhost { [$vhosts]:
    ... => ... ,
  }
}

This, of course, requires that everything be the same or be based on the name (which is available as $name inside the apache::vhost define).

If you want to keep apache::vhost as a straightforward define and still do some more complicated things to work out parameters from the name, you can do them with an intermediate define:

define blah::vhost {
  $wwwroot = "/var/www/html/blah/${name}"
  $wwwhostname = "${name}.example.com"
  if ! defined(File[$wwwroot]) {
    file { $wwwroot:
      ensure => directory,
      mode   => 0775,
    }
  }
  apache::vhost { $name:
     path     => $wwwroot,
     aliases  => [$name],
     hostname => $wwwhostname,
     require  => File[$wwwroot],
  }
}

blah::vhost { [$vhosts]: }

Upvotes: 4

Related Questions