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've been trying to figure out a way to test if a resource is already defined in another file, and if not create it? A quick example:

  if File[$local_container] {
    alert("Testing - It existed $local_container")
  } else {
    file{ "$local_container":
      ensure => directory,
    }
  }

However - File[$local_container] always seems to evaluate to true. Is there a way to do this?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

Do you mean "test if a resource is already defined"? If you define a resource (ie, file {}, etc) Puppet will create what you're describing if doesn't already exist (assuming you pass ensure => present, of course).

To check if a resource is already defined in the catalog or not:

mark-draytons-macbook:~ mark$ cat test.pp 
file { "/tmp/foo": ensure => present }

if defined(File["/tmp/foo"]) {
  alert("/tmp/foo is defined")
} else {
  alert("/tmp/foo is not defined")
}

if defined(File["/tmp/bar"]) {
  alert("/tmp/bar is defined")
} else {
  alert("/tmp/bar is not defined")
}

mark-draytons-macbook:~ mark$ puppet test.pp 
alert: Scope(Class[main]): /tmp/foo is defined
alert: Scope(Class[main]): /tmp/bar is not defined
notice: //File[/tmp/foo]/ensure: created

Note: defined() is dependent on parse order.

share|improve this answer
    
Wow - That simple, thanks! –  gnarf Sep 9 '09 at 21:53
    
That "depends on parse order" part makes it almost useless. –  joerx Apr 1 at 2:47

simply,

file{ "$local_container":
  ensure => directory,
  replace => false,
}
share|improve this answer
    
Nope, if the "$local_container" file was already defined somewhere else (like say by something that wanted to control the permissions/owner) you can't define the same resource twice. –  gnarf Jun 3 at 18:37

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.