I am trying to configure Apache Virtual Hosts with Puppet and have been trying different things with little success.

I have defined a node as the following :

node 'test1.cob' inherits serveurClient {
        $smcvhost = 'all'

The serveurClient class includes the apache class. This works fine as Apache gets installed and all the configuration gets applied correctly, except the virtual hosts.

The configuration relating to the virtual hosts is the following :

class apache::config {
        require => Class["apache::install"],
        notify  => Class["apache::service"],
        ensure  => present,
        owner   => "www-data",
        group   => "www-data",
        mode    => 755
    if ( $smcvhost == 'belleville' ) or ( $smcvhost == 'all' ) {
            client => 'belleville',

The apache::smcvhost definition works correctly because if I specify it directly in the node without the condition, the virtual host gets created correctly with no errors. If I remove the if statement, it will also get created correctly. I have tried only specifying the second condition but that did not make it work.

When this fails to be executed, I do not get any error. The puppet report just ignores this configuration part.

I am thinking that this is some sort of variable scoping problem but from what I have read, this practice seems correct and I imagine puppet would give me some error if I tried to evaluate a non-existing variable.


Puppet doesn't warn you on unset variables (except on templates, curiously enough), and, when you set a variable, only stuff specified after it is affected. So:

node 'test1.cob' inherits serveurClient {
        # Nothing above this line knows $smcvhost
        $smcvhost = 'all'
        # Stuff from this line until "}" knows $smcvhost
        # Stuff from this point on don't know $smcvhost

If you look at the release notes for recent versions of Puppet, you'll see that dynamic scoping is on its way to retirement. My advice is to avoid using variables to pass information as much as possible -- use parameters, such as in definitions or classes with parameters.

  • Thank you ! Too bad Puppet doesn't warn you... I'll find another way to do this then ! Aug 12 '11 at 14:53

Instead of:

node 'test1.cob' inherits serveurClient { $smcvhost = 'all' }

if you do:

node 'test1.cob' { $smcvhost = 'all' include serveurClient }

then your variable will exist in the serveurClient scope, which is where I assume your apache class is referenced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.