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?
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 passensure => present
, of course).To check if a resource is already defined in the catalog or not:
Note:
defined()
is dependent on parse order.The better way of doing this is by making use of ensure_resource function from puppetlabs stdlib
It takes a resource type, title, and a list of attributes that describe a resource as parameters.
say you have test case to only create the resource if it does not already exist-
Or....
And do keep an eye on those quotes and curly braces....
simply,