Ask Your Question
1

I'm confused about scope. Shouldn't this work?

asked 2017-06-12 09:19:14 -0500

erichymowitz gravatar image

I have a class

 class test
{
  notify { "The value of \$foo is $foo" : }
}

And I have a node

node agent1
{
  $foo="bar"
  include test
}

And when puppet runs, I get

notice: The value of $foo is bar

Cool. Then I change my node to:

class profiles::myprofile
{
  include test
}
node agent1
{
  $foo="bar"
  class { 'profiles::myprofile' : }
}

And I still get

notice: The value of $foo is bar

So far so good. But when I change my node to this:

class profiles::myprofile
{
  $foo="bar"
  include test
}
node agent1
{
  class { 'profiles::myprofile' : }
}

I end up with

notice: The value of $foo is

So ... what am I not understanding about scope, that a variable can get passed from node to profile to class, but not from the profile to the class?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-06-13 12:21:58 -0500

DevOpsProDude gravatar image

Any variables declared in profiles::myprofile would be in scope profiles::myprofile...so the third test, the variable you are setting is profiles::myprofile::foo

The test manifest is looking at test::foo. If you want to allow it to be overridden by profiles::myprofile::foo, you'd have to do:

class test (
  $foo = $profiles::myprofile::foo,
) {
  notify { "The value of \$foo is $foo": 
  }
}
edit flag offensive delete link more

Comments

1

I would recommend only using "internal variables" (even though in Puppet all variables are global) when you need to modify a param in some way, such as `$downcasefoo = downcase($foo)`

DevOpsProDude gravatar imageDevOpsProDude ( 2017-06-13 12:26:59 -0500 )edit

Thank you.

erichymowitz gravatar imageerichymowitz ( 2017-06-13 13:26:32 -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

1 follower

Stats

Asked: 2017-06-12 09:19:14 -0500

Seen: 40 times

Last updated: Jun 13