Ask Your Question

Declare a resource only if not already declared?

asked 2015-11-01 09:56:01 -0600

JohnsonEarls gravatar image

I'm writing a module for which there are a number of resources that I want to declare, but only if those resources do not get declared elsewhere - in other words, I want a set of "default" resources that can be overridden.

I finally came up with this solution, but I think it won't work due to parsing order dependencies:

class foo::defaults {
    # we want a 'baz' and 'quux' resource, but only if the they haven't
    # already been defined somewhere else
    if not defined(foo::bar['baz']) {
        foo::bar { 'baz': ensure => present }
    if not defined(foo::bar['quux']) {
        foo::bar { 'quux': ensure => present }
    # etc...

... somewhere else ...

    foo::bar { 'quux': ensure => absent }

The issue (I think) is that I need to have class foo::defaults evaluated after all possible declarations of foo::bar are complete. I'm pretty sure that resource ordering won't help, because that doesn't change evaluation order; similarly with trying to use stages.

Does anyone have an alternate solution, or a way to make this solution work? Or can tell me that my evaluation of the ordering issue is wrong?

Thanks in advance, - Johnson

edit retag flag offensive close merge delete


I've never used it myself but you might want to check the stdlib `defined_with_params`.

lupin gravatar imagelupin ( 2015-11-01 15:28:55 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-11-13 09:33:06 -0600

scoffland gravatar image

Try using a collector to modify the resource instead of ensuring only when not already defined.

class foo:defaults {
  file { 'baz':
    ensure => present,
    content => do_nothing,

class foo:bar {
  File <| title == 'baz' |> {
     content => do_something,
edit flag offensive delete link more


My problem is that `ensure` is one of the parameters that I want to default - I only want to set `ensure => present` if the calling module has not set `ensure => absent` already. Since `ensure` is a required parameter, I can't create the resource without it and define it via collector later.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-12-03 07:07:37 -0600 )edit

PS - upvoting answer as a good trick to think about, but I don't think it works for my particular situation. Thanks!

JohnsonEarls gravatar imageJohnsonEarls ( 2015-12-03 07:08:27 -0600 )edit

answered 2015-11-12 01:26:27 -0600

Cristian Falcas gravatar image

The execution order is randomish, bu the manifests are read in the same order each time and also the checks are performed in the same order each time. So, you only need to declare foo::bar in your code before foo::defaults.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-11-01 09:56:01 -0600

Seen: 2,468 times

Last updated: Nov 13 '15