I have a puppet class called dhcp which sets up the daemon and installs a base dhcpd.conf. I have also have subclasses like dhcp::pool1, dhcp::pool2 which install other files with DHCP code snippets to provide DHCP to different subnets with different address pools. The manifest for these looks like this: class dhcp::rnw { include dhcp2::common $includernw = ''1'' file { "dhcpd.rnw": name => "/etc/dhcp/dhcpd.rnw", mode => 644, owner => "root", group => "root", notify => Service[dhcpd], source => "puppet:///modules/dhcp/dhcpd.rnw", } In the ERB template for the base dhcpd.conf, I have lines like this: <% if includernw = 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> For some reason, the include lines always get included, even if the subclass hasn''t been applied and therefore the $includernw hasn''t been set. Am I missing something with the way variables are set and assigned, or read in ERB? Thanks, Jonathan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Try: <% if includernw == 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> Note the ''=='' :-). ken. On Wed, Jul 6, 2011 at 2:45 PM, Jonathan Gazeley <jonathan.gazeley@bristol.ac.uk> wrote:> I have a puppet class called dhcp which sets up the daemon and installs a > base dhcpd.conf. > > I have also have subclasses like dhcp::pool1, dhcp::pool2 which install > other files with DHCP code snippets to provide DHCP to different subnets > with different address pools. The manifest for these looks like this: > > class dhcp::rnw { > include dhcp2::common > $includernw = ''1'' > file { "dhcpd.rnw": > name => "/etc/dhcp/dhcpd.rnw", > mode => 644, > owner => "root", > group => "root", > notify => Service[dhcpd], > source => "puppet:///modules/dhcp/dhcpd.rnw", > } > > > In the ERB template for the base dhcpd.conf, I have lines like this: > > <% if includernw = 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> > > For some reason, the include lines always get included, even if the subclass > hasn''t been applied and therefore the $includernw hasn''t been set. > > Am I missing something with the way variables are set and assigned, or read > in ERB? > > Thanks, > Jonathan > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Thanks Ken. Adding the ''=='' now makes my template syntactically valid, which is always nice. Next problem - as the $includernw variable is defined in a subclass, it is out-of-scope when the main dhcpd.conf template is called from the top class. I see in the docs there''s a function called scope.lookupvar but that seems to need a <%= in the template, rather than a <%. Is there a way to use an out-of-scope variable in a simple conditional in a template? I''m a perl kinda guy, but gradually getting to grips with ruby/puppet :) Many thanks, Jonathan On 06/07/11 14:56, Ken Barber wrote:> Try: > > <% if includernw == 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> > > Note the ''=='' :-). > > ken. > > On Wed, Jul 6, 2011 at 2:45 PM, Jonathan Gazeley > <jonathan.gazeley@bristol.ac.uk> wrote: >> I have a puppet class called dhcp which sets up the daemon and installs a >> base dhcpd.conf. >> >> I have also have subclasses like dhcp::pool1, dhcp::pool2 which install >> other files with DHCP code snippets to provide DHCP to different subnets >> with different address pools. The manifest for these looks like this: >> >> class dhcp::rnw { >> include dhcp2::common >> $includernw = ''1'' >> file { "dhcpd.rnw": >> name => "/etc/dhcp/dhcpd.rnw", >> mode => 644, >> owner => "root", >> group => "root", >> notify => Service[dhcpd], >> source => "puppet:///modules/dhcp/dhcpd.rnw", >> } >> >> >> In the ERB template for the base dhcpd.conf, I have lines like this: >> >> <% if includernw = 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> >> >> For some reason, the include lines always get included, even if the subclass >> hasn''t been applied and therefore the $includernw hasn''t been set. >> >> Am I missing something with the way variables are set and assigned, or read >> in ERB? >> >> Thanks, >> Jonathan >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Puppet Users" group. >> To post to this group, send email to puppet-users@googlegroups.com. >> To unsubscribe from this group, send email to >> puppet-users+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/puppet-users?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hi Jonathan. You should be able to use a similar syntax as illustrated below. For instance… <% if scope.lookupvar(''dhcp::includernw'') == 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> <% is a marker for ruby code, whereas <%= is actually a ruby expression that outputs the result of a value or expression. Hope that helps! -James -- James Fryman ph +1.615.669.2048 | twitter jfryman | gtalk: jamison@puppetlabs.com On Jul 6, 2011, at 10:01 AM, Jonathan Gazeley wrote:> Thanks Ken. Adding the ''=='' now makes my template syntactically valid, which is always nice. > > Next problem - as the $includernw variable is defined in a subclass, it is out-of-scope when the main dhcpd.conf template is called from the top class. > > I see in the docs there''s a function called scope.lookupvar but that seems to need a <%= in the template, rather than a <%. Is there a way to use an out-of-scope variable in a simple conditional in a template? > > I''m a perl kinda guy, but gradually getting to grips with ruby/puppet :) > > Many thanks, > Jonathan > > > On 06/07/11 14:56, Ken Barber wrote: >> Try: >> >> <% if includernw == 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> >> >> Note the ''=='' :-). >> >> ken. >> >> On Wed, Jul 6, 2011 at 2:45 PM, Jonathan Gazeley >> <jonathan.gazeley@bristol.ac.uk> wrote: >>> I have a puppet class called dhcp which sets up the daemon and installs a >>> base dhcpd.conf. >>> >>> I have also have subclasses like dhcp::pool1, dhcp::pool2 which install >>> other files with DHCP code snippets to provide DHCP to different subnets >>> with different address pools. The manifest for these looks like this: >>> >>> class dhcp::rnw { >>> include dhcp2::common >>> $includernw = ''1'' >>> file { "dhcpd.rnw": >>> name => "/etc/dhcp/dhcpd.rnw", >>> mode => 644, >>> owner => "root", >>> group => "root", >>> notify => Service[dhcpd], >>> source => "puppet:///modules/dhcp/dhcpd.rnw", >>> } >>> >>> >>> In the ERB template for the base dhcpd.conf, I have lines like this: >>> >>> <% if includernw = 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> >>> >>> For some reason, the include lines always get included, even if the subclass >>> hasn''t been applied and therefore the $includernw hasn''t been set. >>> >>> Am I missing something with the way variables are set and assigned, or read >>> in ERB? >>> >>> Thanks, >>> Jonathan >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Puppet Users" group. >>> To post to this group, send email to puppet-users@googlegroups.com. >>> To unsubscribe from this group, send email to >>> puppet-users+unsubscribe@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/puppet-users?hl=en. >>> >>> >> > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en. >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hi James, Thanks very much - this worked. As a side effect, I learned a little about how ruby works, too. I''d just like to add that the puppet-users list is one of the more friendly and helpful lists I subscribe to :) Cheers, Jonathan On 07/06/2011 04:47 PM, James Fryman wrote:> Hi Jonathan. > > You should be able to use a similar syntax as illustrated below. For instance… > > <% if scope.lookupvar(''dhcp::includernw'') == 1 %> include "/etc/dhcp/dhcpd.rnw";<% end %> > > <% is a marker for ruby code, whereas<%= is actually a ruby expression that outputs the result of a value or expression. > > Hope that helps! > > -James >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.