Re: Weird variable dependency issue + different behavior between master/masterless

36 views
Skip to first unread message

jcbollinger

unread,
Sep 5, 2012, 10:01:02 AM9/5/12
to puppet...@googlegroups.com


On Wednesday, September 5, 2012 5:09:52 AM UTC-5, renaud wrote:
Hello all,

I know that execution order is not guaranteed within a scope without explicitly declaring dependencies.  
However I've always been able to set variables in classes, and expect them to be used properly in templates that I declare in a File statement in the same class.


Indeed you should be able to rely on that, for the correct definition of "properly".

 
It looks like I came across a problem with this yesterday :

My class contains :

$master_port = 6379
$master_host = $hostname ? {
   /(stage|live)-xx([a-d])-redis1/ => "$1-yy$2-redis1",
   default                          => false,
}

 file { "/etc/redis/redis.conf":
   ensure      => file,
   owner       => "root",
   group       => "root",
   mode        => "0644",
   content     => template("redis/redis.conf.erb"),
 }

And redis.conf.erb contains :

<% if master_host -%>
slaveof <%= master_host %> <%= master_port %>
<% end -%> 

When the hostname matches the regexp, here's what I get in redis.conf depending on how I run puppet : 

- If I run puppet masterless using "puppet apply testing.pp" (with testing.pp being a simple manifest that just include the module that contains the class above) :
"slaveof live-yya-redis1 6379"
This is fine
- But if I run puppet through "puppet agent --test", with the exact same manifest on the puppetmaster as locally, I get this :
"slaveof   6379"

So, it looks like in the second case the master_host variable is definitely set (otherwise the if statement in the .erb would exit), but set to an empty string, while the other master_port variable, which is defined right next to it in the manifest, works fine !

Any idea why that should be ?


The Puppet templating docs recommend referring to DSL variables via Ruby class variables, so as @master_host and @master_port in your case.  Referring to them via local variables (as your template does) will often work, but it can fail in interesting ways if your variables happen to have the same name as in-scope local variables of the Puppet application.  I think that's what has happened to you.

I suspect that the conflicting master_* variables belong to the puppet master code (makes sense), so it is plausible that they are not in scope when you apply your class via "puppet apply", whereas they are in scope when the template is processed by the master in order to service "puppet agent".


John

renaud

unread,
Sep 5, 2012, 10:19:06 AM9/5/12
to puppet...@googlegroups.com
Thanks John,

The Puppet templating docs recommend referring to DSL variables via Ruby class variables, so as @master_host and @master_port in your case.  Referring to them via local variables (as your template does) will often work, but it can fail in interesting ways if your variables happen to have the same name as in-scope local variables of the Puppet application.  I think that's what has happened to you.

Indeed I've seen this since posting and started addressing my variables with @.
This didn't help unfortunately.
 

I suspect that the conflicting master_* variables belong to the puppet master code (makes sense), so it is plausible that they are not in scope when you apply your class via "puppet apply", whereas they are in scope when the template is processed by the master in order to service "puppet agent".

... and I also thought of this, so I prepended "redis_" in front of my variable names. Unfortunately this still didn't help.
To clarify, my template now looks like this :

<% if @redis_master_host -%>
slaveof <%= @redis_master_host %> <%= @redis_master_port %>
<% end -%>


renaud

unread,
Sep 5, 2012, 10:24:10 AM9/5/12
to puppet...@googlegroups.com
I should add that I've added a 'notice' statement for debugging, which again shows the correct things when the manifest is applied manually, but doesn't seem to be executed (at all) when run through the puppetmaster 

renaud

unread,
Sep 5, 2012, 11:20:17 AM9/5/12
to puppet...@googlegroups.com
I've found the problem !
"$1-yy$2-redis1" just didn't work on the puppetmaster, it needs better escaping : ""${1}-yy${2}-redis1"

I'd still be interested to know the reason for that, and also why my 'notice' statement had no output when run from puppetmaster.

R.I.Pienaar

unread,
Sep 5, 2012, 11:38:48 AM9/5/12
to puppet...@googlegroups.com


----- Original Message -----
> From: "renaud" <ren...@renaudguerin.net>
> To: puppet...@googlegroups.com
> Sent: Wednesday, September 5, 2012 4:20:17 PM
> Subject: [Puppet Users] Re: Weird variable dependency issue + different behavior between master/masterless
>
> I've found the problem !
> "$1-yy$2-redis1" just didn't work on the puppetmaster, it needs
> better escaping : " "${1}-yy${2}-redis1"
>
>
> I'd still be interested to know the reason for that, and also why my
> 'notice' statement had no output when run from puppetmaster.
>

are the agents and masters the same version? There was some change in behavior
between some versions wrt to -'s in variable names, you should always fo
"${foo}" inside quotes

renaud

unread,
Sep 5, 2012, 11:42:20 AM9/5/12
to puppet...@googlegroups.com
That was the problem indeed.
The agent was 2.7.19 and the master 2.7.14.

Thanks !
Reply all
Reply to author
Forward
0 new messages