3

I'm trying to define a static media server that uses a common website layout (e.g. with directories etc/, log/, html/, etc.), as well as an nginx config that will get included into a host-wide nginx config. The site's nginx config needs to make use of the doc root that gets defined by the declared layouts::website resource.

So, what I'd like to do is something like this:

class sites::static ($sites_dir) {

    $domain = "static.example.com"

    layouts::website {$domain:
        base_dir => $sites_dir,
        name => $domain,
    }

    nginx::siteconfig {$domain:
        domain => $domain,
        doc_root => $layouts::website[$domain]::doc_root,
    }
}

Which could then be used by declaring:

class {'sites::static': sites_dir => "/opt/sites"}

However, the above fails because the $layouts::website[$domain]::doc_root part is invalid. I've tried various alternatives here, unsuccessfully.

Is this possible? If not, how would you suggest I accomplish the stated goal of using a base layout that then needs to be used by various bits that will populate content within that base layout?

6
  • In essence you're trying to access a value of a parameter passed in to resource, right? Me too.
    – mogsie
    Apr 24, 2012 at 21:08
  • I found this which seems to answer your question. serverfault.com/questions/306446/puppet-parameter-lookup
    – mogsie
    Apr 24, 2012 at 21:15
  • 1
    @mogsie Not necessarily a parameter passed into a resource, but a variable that has been defined by the other resource whether passed to it or defined within it. From what I can tell so far, it is possible to access a variable within a declared class, but not within a declared resource type. It seems that the only way to accomplish this is to declare a variable in the sites::static scope and pass it to the declared resources, however doing that pretty much defeats the purpose of modularizing things into separate components in the first place.
    – Gary
    Apr 24, 2012 at 22:49
  • I've also come to the same conclusion that it isn't possible to access the parameters of a given resource type :/
    – mogsie
    Apr 26, 2012 at 19:57
  • if that's the case, how am I supposed to use templates that get their values from parameters passed to the defined resource? Oct 26, 2012 at 13:23

1 Answer 1

0

As far as I know, you can't get at variables inside defined resources.

I would approach this by reordering the resource type definitions and putting the nginx::siteconfig clause inside the layouts::website definition. (If every use of layouts::website should also have a nginx::siteconfig too, then nesting the one in the other is a good way to DRY.) If you need to have some cases where a use of layouts::website does not also have a nginx definition, you'll have to add a parameter to layouts::website, something like $nginx = true.

Also, the following bits of code:

layouts::website {$domain:
    name => $domain,
}

nginx::siteconfig {$domain:
    domain => $domain,
}

smell like unnecessary repetition to me. The website name and nginx domain seem like perfect candidates to be namevars so you can leave off the parameter in most cases.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .