Ask Your Question
1

Refer to a define's "variables" outside the define itself?

asked 2013-01-07 11:12:51 -0500

zacharyalexstern gravatar image

updated 2013-02-27 09:11:26 -0500

llowder gravatar image

E.g. I have a define that looks like this:

define apache::createvhost (
    $projectname = $title
    $priority = $20
    $ssl = 'no',
    $registrar = 'gandi'
) {
some puppet code
}

Is it possible to refer to the registrar = 'gandi' inside a template or similar. It's unclear to me if the parameters of a define are actually variables or not, since they're more akin to things like "ensure => present".

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-01-07 15:36:42 -0500

llowder gravatar image

If the template is called from within the define, then you can use the variables in the template.

For example:

<% if @registrar == 'gandi' -%>
--do something --
<% else -%>
-- do something else --
<% end -%>

From within the template, you can access any variables that are in scope (see the documentation for additional details) - this includes variables defined in the define, items passed in as arguments, top scope variables and facts. Even variables from other scopes if you use scope.lookup() to look it up.

edit flag offensive delete link more

Comments

Why the @ symbol? I don't remember having used that before with variables inside templates.

zacharyalexstern gravatar imagezacharyalexstern ( 2013-01-07 15:50:01 -0500 )edit

the @ symbol is used to make the variable into an instance variable. It's the recommended practice currently. My understanding it is that it prevents conflicts between ruby variables and ...(more)

llowder gravatar imagellowder ( 2013-01-07 16:46:11 -0500 )edit
0

answered 2013-01-07 13:10:13 -0500

In this case, you are passing the variables as arguments.

Any code inside "some puppet code", whether it is a template or a resource will be able to access those variables.

But you will not be able to access those variables outside of the 'define'.
http://docs.puppetlabs.com/puppet/2.7/reference/langdefinedtypes.html#defining-a-type

edit flag offensive delete link more

Comments

So the question is, can I have a template used inside the define do something different depending on those variables?

zacharyalexstern gravatar imagezacharyalexstern ( 2013-01-07 13:18:06 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2013-01-07 11:12:51 -0500

Seen: 171 times

Last updated: Jan 07 '13