Managing Apache VHost with Foreman

Hi All,

I’m kinda figuring out how to manage Apache virtual hosts with Foreman (Puppet 5.5, Foreman 1.16.2).

I’am using the apache module from puppetlabs.

How can I create the specified Vhost under Foreman

apache::vhost {
vhost.example.com’:
port => ‘80’,
docroot => ‘/var/www/vhost’,
}

a) Using JSON ?
b) Using YAML ?

Thanks in advance.

Best regards, Claus

Foreman (or any ENC for that matter) cannot create define resources directly, only class resources. The usual way to handle this is a “wrapper class” which takes the take you wish to define as class parameters, and calls the appropriate defines.

Some patterns for handling this are in wiki - see http://projects.theforeman.org/projects/foreman/wiki/Instantiate_Puppet_resources

Hi,

I’am not shure what to do ?

The class structure of the vhosts.pp files is:

file: vhosts.pp

class apache::vhosts (
 $vhosts = {},
) {
 include ::apache
 create_resources('apache::vhost', $vhosts)
}

file: vhost.pp

define apache::vhost(
  Variant[Boolean,String] $docroot,
  $manage_docroot                                                                   = true,
  $virtual_docroot                                                                  = false,
  $port                                                                             = undef,
  $ip                                                                               = undef,
....

Ah I see, then passing in $vhosts seems to be all you need. Edit the class via Classes > Apache::Vhosts > $vhosts and you’ll be able to set the data type there - you need to use a hash, but you can use Ruby, YAML, or JSON style, as you prefer.

My problem is that I can not create an appropriate input.

I have already edited the corresponding class (apache :: vhosts) under Foreman, but unfortunately no input is accepted.

How should an input look like?

I tried:
Type: yaml
Value:
{
“classes”: [
“apache”,
],
“apache::vhost::vhost_name”: “vm.alpha.lab”,
“apache::vhost::port”: “80”,
“apache::vhost::docroot”: “/var/www”
}

Your example isn’t valid YAML, so I’m unsurprised it’s rejected. What you have there looks like JSON to me… however, it’s still not valid. You need a structure of “key” => “hash of values”, same as you would declare in Puppet itself. Given the code in the first post, something like:

myvhost1.example.com:
  port: 80
  ip: 127.0.0.1
  docroot: /var/www/vhosts/vhost1
myvhost2.example.com:
  port: 80
  ip: 127.0.0.1
  docroot: /var/www/vhosts/vhost2

I.E exactly what you’d put in the define:

apache::vhost{ "myvhost1.example.com":
  ip => 127.0.0.1,
  port => 80,
  docroot => '/var/www/vhosts/vhost2'
}

Again, the link I posted above shows similar examples.