# Resource defaults not working for Apache

I am using the PuppetLabs apache module. I would like to set SetEnvIf values for ever single vhost.

I set the value in my web_server profile:

class profile::web_server {
class { 'apache':
default_vhost => false,
}

Apache::Vhost {
setenvif      => [
'User-Agent "KEMP 1.0" dontlogkemp dontlog',
'User-Agent "SearchApp 2.1" dontlogsearchapp dontlog',
],
}
}


My vhosts are defined in other classes, but I want them to inherit this default:

class profile::web_server::cluster {
apache::vhost { 'clustered site 1':
...
}
apache::vhost { 'clustered site 2':
...
}
}
class profile::web_server::server1 {
apache::vhost { 'site 1 on server 1 only':
...
}
apache::vhost { 'site 2 on server 1 only':
...
}
}
class profile::web_server::server2 {
apache::vhost { 'site 3 on server 2 only':
...
}
apache::vhost { 'site 4 on server 2 only':
...
}
apache::vhost { 'site 5 on server 2 only':
...
}
apache::vhost { 'site 6 on server 2 only':
...
}
}
...


No matter what I do, it is not added to the config. I tried several other values for testing (port and priority) and none of them seem to work either.

All the sites are behind a load balancer and need this SetEnvIfso that their access logs don't fill up with uptime checks by our load balancer and search engine indexer. The useragent may change or we may add additional ones, so I only want to have it in one place for 100+ vhosts.

Puppet version: 3.8.1

I do have a sort of ugly workaround (which puts the config for the whole apache server instead of for every vhost):

define apache_setenv($setenvif) { apache::custom_config { 'setenvif': priority => '0', content => template('apache/vhost/_setenv.erb'), } } apache_setenv( setenvif => [ 'User-Agent "KEMP 1.0" dontlogkemp dontlog', 'User-Agent "SearchApp 2.1" dontlogsearchapp dontlog', ] );  edit retag close merge delete ## Comments The code doesn't show any use of apache::vhost. Note that resource defaults have an area of affect based on dynamic scoping, so setting it in one class and expecting to apply to another won't work: https://docs.puppetlabs.com/puppet/4.2/reference/lang_defaults.html#area-of-effect ( 2015-10-09 05:09:42 -0600 )edit Updated with location of vhost definitions ( 2015-10-09 08:31:35 -0600 )edit Added requirement that all 100+ vhosts in multiple classes need same default SetEnvIf and the reason for it. ( 2015-10-11 02:11:15 -0600 )edit ## 2 Answers Sort by » oldest newest most voted Maybe not the most efficient way because you have to add a line for all your vhost but what about: class profile::web_server {$my_setenvif = [
'User-Agent "KEMP 1.0" dontlogkemp dontlog',
'User-Agent "SearchApp 2.1" dontlogsearchapp dontlog',
]
}


class profile::web_server::server1 {
apache::vhost { 'site 1 on server 1 only':
...
setenvif => $profile::web_server::my_setenvif } apache::vhost { 'site 2 on server 1 only': ... setenvif =>$profile::web_server::my_setenvif
}
}


But at least, you are sure to use the same value for all your vhost and if you need to modify it, you just have to modify it at one place.

more

It should be like.

 class { '::apache':
default_vhost => false,
}

apache::vhost { 'clusternode1':
servername => '127.0.0.1',
setenvif => [ 'User-Agent "KEMP 1.0" dont logkemp dontlog', ],
docroot  => '/var/www/html',
}

more

That only applies to one vhost, but I have 100 spread through 4 classes that all need the same setenvif directives. (We run a load balancer and don't log the checks that see if the server is up - the useragent can change or there may be additional ones, I want to only set it in one place.)

( 2015-10-11 02:05:16 -0600 )edit

I missed your "4 classes" keyword. If you're creating your vhost using using create_resources you can pass a default parameters where you can put the value of setenvif.

( 2015-10-11 03:26:46 -0600 )edit