I would like to use a different squid.conf file depending on a condition. Something like:

    file { '/etc/squid/squid.conf':
    ensure => file,
    if condition = true then
        source => 'puppet:///modules/1/squid.conf_1',
        source => 'puppet:///modules/1/squid.conf_2',

Is there a way to do this?


You need a selector, which will let you have a conditional inside the resource - or set a variable up outside of it.

Here's the selector:

file { '/etc/squid/squid.conf':
  ensure => file,
  source => $fact ? {
    "foo"   => 'puppet:///modules/1/squid.conf_1',
    "bar"   => 'puppet:///modules/1/squid.conf_2',
    default => 'puppet:///modules/1/squid.conf_default',

Or set a variable and use that instead:

if $fact == "foo" {
  $squidconf = 'puppet:///modules/1/squid.conf_1'
} else {
  $squidconf = 'puppet:///modules/1/squid.conf_2'

file { '/etc/squid/squid.conf':
  ensure => file,
  source => $squidconf,

There are other variations of course, have a look at the language guide linked above and the Learning Puppet tutorial section on variables.

| improve this answer | |
  • What if $fact = $(ps aux | grep "something")? – chris Dec 11 '11 at 1:49
  • You would need to implement a fact in Ruby that provides equivalent functionality, see these docs. Manifests are parsed on the master, so it isn't as simple as $(..), but adding a fact will let it run on the client before the catalog is built. – Dominic Cleal Dec 11 '11 at 11:54
  • Isn't there any trick to achieve the same using onlyif? I mean, somehow declare twice the same file with just a different onlyif? Sorry if I push it... I think it could be quite useful... – chris Dec 11 '11 at 14:55
  • Glad it worked. Just to answer the onlyif question, that's only a valid parameter on exec and one or two others. Even then, it's fundamentally flawed as for this, you could only test that the file existed, rather than a) had the right content, b) had the right mode, owner, group etc. Puppet's much richer than what onlyif can provide. – Dominic Cleal Dec 12 '11 at 21:00

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.