Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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',
    else
        source => 'puppet:///modules/1/squid.conf_2',
    fi
    }

Is there a way to do this?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|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. –  m0dlx 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
    
Did the custom fact, works great, thanks again. –  chris Dec 12 '11 at 10:22
    
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. –  m0dlx Dec 12 '11 at 21:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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