Andrew Glen-Young
2007-Aug-24 15:16 UTC
Variable scope: Class inheritance vs Include statement.
Hello. I''m a long time cfengine user, some might say sufferer, and I have finally managed to get around to experimenting with Puppet. So far I''m very impressed. I am trying to get puppet to configure the openntpd config file, from an ERB template, based on the node definition in the site config. What I''m trying to understand is why when I define a variable in a child class, the variable does not seem to be evaluated correctly in a ERB template that is defined in the parent class. If instead of inheriting the parent class, I use an "include" statement, then the variable is evaluated correctly. My puppet configs and templates are listed below. As I understand it, Puppet variables are evaluated where the code is executed. I assume that this is a lack of understanding on my part and any clarification would help greatly. Possibly there is a even better way of doing this and my cfengine knowledge is coming back to bite me? My puppet config: Puppet version: 0.23.2 /etc/puppet/manifests/classes/ntp.pp: # My ''parent'' class. class ntp { file { "/etc/openntpd/ntpd.conf": owner => "root", group => "root", mode => 644, content => template("/etc/puppet/templates/apps/openntpd/ntpd.conf.erb") } } # This works. class ntp_server { $ntp_class = "ntp_server" include ntp } # Why does this NOT work? # class ntp_server inherits ntp { # $ntp_class = "ntp_server" # } class ntp_client { # Could also possibly read: $ntp_class = $name $ntp_class = "ntp_client" include ntp } /etc/puppet/templates/apps/openntpd/ntpd.conf.erb: <% if ntp_class == "ntp_server" -%> listen on * servers pool.ntp.org <% elsif ntp_class == "ntp_client" -%> server ntp.mydom.ain <% end -%> /etc/puppet/manifests/site.pp: import "classes/*.pp" node default { include ntp_server } -- Regards. Andrew Glen-Young
Luke Kanies
2007-Aug-24 16:10 UTC
Re: Variable scope: Class inheritance vs Include statement.
On Aug 24, 2007, at 10:16 AM, Andrew Glen-Young wrote:> I am trying to get puppet to configure the openntpd config file, from > an ERB template, based on the node definition in the site config.Variables in subclasses cannot override variables in parent classes, at least partially because I haven''t been able to find a way to make that work. The answer is to model the thing using the variable as a resource, and then you can override parameters in that resource in the subclass. -- You don''t learn anything the second time you''re kicked by a mule. -- Anonymous Texan --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com
Ian Burrell
2007-Aug-24 17:26 UTC
Re: Variable scope: Class inheritance vs Include statement.
On 8/24/07, Andrew Glen-Young <aglenyoung@gmail.com> wrote:> > /etc/puppet/templates/apps/openntpd/ntpd.conf.erb: > > <% if ntp_class == "ntp_server" -%> > listen on * > servers pool.ntp.org > <% elsif ntp_class == "ntp_client" -%> > server ntp.mydom.ain > <% end -%> >If that is all there is to your template, why don''t you just have two files. ntpd.conf.server or ntpd.conf.client. You wouldn''t even need a template. I would normally put them in separate directories: ntp_server/ntpd.conf and ntp_client/ntpd.conf. - Ian
Luke Kanies
2007-Aug-24 17:41 UTC
Re: Variable scope: Class inheritance vs Include statement.
On Aug 24, 2007, at 12:26 PM, Ian Burrell wrote:> If that is all there is to your template, why don''t you just have two > files. ntpd.conf.server or ntpd.conf.client. You wouldn''t even need > a template. I would normally put them in separate directories: > ntp_server/ntpd.conf and ntp_client/ntpd.conf.Frankly, if that''s your whole template, just use an inline string in your manifest. -- The Microsoft Exchange Information Store service depends on the Microsoft Exchange Directory service which failed to start because of the following error: The operation completed successfully. --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com
Andrew Glen-Young
2007-Aug-24 18:01 UTC
Re: Variable scope: Class inheritance vs Include statement.
On 24/08/07, Ian Burrell <ianburrell@gmail.com> wrote:> On 8/24/07, Andrew Glen-Young <aglenyoung@gmail.com> wrote: > > > > /etc/puppet/templates/apps/openntpd/ntpd.conf.erb: > > > > <% if ntp_class == "ntp_server" -%> > > listen on * > > servers pool.ntp.org > > <% elsif ntp_class == "ntp_client" -%> > > server ntp.mydom.ain > > <% end -%> > > > > If that is all there is to your template, why don''t you just have two > files. ntpd.conf.server or ntpd.conf.client. You wouldn''t even need > a template. I would normally put them in separate directories: > ntp_server/ntpd.conf and ntp_client/ntpd.conf. >Indeed, I had thought of this, but since I have just started playing with puppet and I was starting small. -- Andrew Glen-Young