I''ve got a conditional statement that isn''t working the way I expect. I''ve created a define() for building network interfaces, but two of the entries (master & slave) are only appropriate for bonded interfaces. I would like them to be completely excluded from regular interfaces. This seems easy enough according to the docs, set master & slave to ''undef'' and use the ''if has_variable'' syntax in the template. It doesn''t work though. I also tried using ''if variable != "undef"'' but this also does not work. Interestingly the results of the two are exactly the same. I''m clearly missing something. How do I get the template to skip these lines? The results end up looking like this: ### This file is being managed by Puppet ### DO NOT EDIT DEVICE=eth0 BOOTPROTO=none ONBOOT=yes IPADDR=192.168.1.1 NETMASK=255.255.255.0 HWADDR=eth0 MASTER=undef SLAVE=undef The define is built like so: define network_interface ( $bootproto, $onboot, $ipaddr = undef, $netmask = undef, $hwaddr = undef, $master = undef, $slave = undef ){ <SNIP> } And the template looks like: ### This file is being managed by Puppet ### DO NOT EDIT DEVICE=<%= name %> BOOTPROTO=<%= bootproto %> ONBOOT=<%= onboot %> <% if ipaddr != "undef" -%> IPADDR=<%= ipaddr %> <% end -%> <% if netmask != "undef" -%> NETMASK=<%= netmask %> <% end -%> <% if final_hwaddr != "undef" -%> HWADDR=<%= final_hwaddr %> <% end -%> <% if master != "undef" -%> MASTER=<%= master %> <% end -%> <% if slave != "undef" -%> SLAVE=<%= slave %> <% end -%> Or I also tried: ### This file is being managed by Puppet ### DO NOT EDIT DEVICE=<%= name %> BOOTPROTO=<%= bootproto %> ONBOOT=<%= onboot %> <% if has_variable?("ipaddr") then -%> IPADDR=<%= ipaddr %> <% end -%> <% if has_variable?("netmask") then -%> NETMASK=<%= netmask %> <% end -%> <% if has_variable?("final_hwaddr") then -%> HWADDR=<%= final_hwaddr %> <% end -%> <% if has_variable?("master") then -%> MASTER=<%= master %> <% end -%> <% if has_variable?("slave") then -%> SLAVE=<%= slave %> <% end -%> -- 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.
On Mon, May 23, 2011 at 4:09 PM, Aaron Grewell <aaron.grewell@gmail.com> wrote:> I''ve got a conditional statement that isn''t working the way I expect. I''ve > created a define() for building network interfaces, but two of the entries > (master & slave) are only appropriate for bonded interfaces. I would like > them to be completely excluded from regular interfaces. This seems easy > enough according to the docs, set master & slave to ''undef'' and use the ''if > has_variable'' syntax in the template. It doesn''t work though. I also tried > using ''if variable != "undef"'' but this also does not work. Interestingly > the results of the two are exactly the same. I''m clearly missing > something. How do I get the template to skip these lines? > > The results end up looking like this: > ### This file is being managed by Puppet > ### DO NOT EDIT > DEVICE=eth0 > BOOTPROTO=none > ONBOOT=yes > IPADDR=192.168.1.1 > NETMASK=255.255.255.0 > HWADDR=eth0 > MASTER=undef > SLAVE=undef > > The define is built like so: > define network_interface ( > $bootproto, > $onboot, > $ipaddr = undef, > $netmask = undef, > $hwaddr = undef, > $master = undef, > $slave = undef > ){ <SNIP> } > > And the template looks like: > ### This file is being managed by Puppet > ### DO NOT EDIT > DEVICE=<%= name %> > BOOTPROTO=<%= bootproto %> > ONBOOT=<%= onboot %> > <% if ipaddr != "undef" -%> > IPADDR=<%= ipaddr %> > <% end -%> > <% if netmask != "undef" -%> > NETMASK=<%= netmask %> > <% end -%> > <% if final_hwaddr != "undef" -%> > HWADDR=<%= final_hwaddr %> > <% end -%> > <% if master != "undef" -%> > MASTER=<%= master %> > <% end -%> > <% if slave != "undef" -%> > SLAVE=<%= slave %> > <% end -%> > > Or I also tried: > ### This file is being managed by Puppet > ### DO NOT EDIT > DEVICE=<%= name %> > BOOTPROTO=<%= bootproto %> > ONBOOT=<%= onboot %> > <% if has_variable?("ipaddr") then -%> > IPADDR=<%= ipaddr %> > <% end -%> > <% if has_variable?("netmask") then -%> > NETMASK=<%= netmask %> > <% end -%> > <% if has_variable?("final_hwaddr") then -%> > HWADDR=<%= final_hwaddr %> > <% end -%> > <% if has_variable?("master") then -%> > MASTER=<%= master %> > <% end -%> > <% if has_variable?("slave") then -%> > SLAVE=<%= slave %> > <% end -%>Don''t quote it, has_variable?(slave). Nan -- 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.
Yup, that works. FYI there''s a bug in the docs. The example shown here: http://docs.puppetlabs.com/guides/templating.html#undefined-variables Looks like so: <% if has_variable?("myvar") then %> myvar has <%= myvar %> value <% end %> On Mon, May 23, 2011 at 4:59 PM, Nan Liu <nan@puppetlabs.com> wrote:> On Mon, May 23, 2011 at 4:09 PM, Aaron Grewell <aaron.grewell@gmail.com> > wrote: > > I''ve got a conditional statement that isn''t working the way I expect. > I''ve > > created a define() for building network interfaces, but two of the > entries > > (master & slave) are only appropriate for bonded interfaces. I would > like > > them to be completely excluded from regular interfaces. This seems easy > > enough according to the docs, set master & slave to ''undef'' and use the > ''if > > has_variable'' syntax in the template. It doesn''t work though. I also > tried > > using ''if variable != "undef"'' but this also does not work. > Interestingly > > the results of the two are exactly the same. I''m clearly missing > > something. How do I get the template to skip these lines? > > > > The results end up looking like this: > > ### This file is being managed by Puppet > > ### DO NOT EDIT > > DEVICE=eth0 > > BOOTPROTO=none > > ONBOOT=yes > > IPADDR=192.168.1.1 > > NETMASK=255.255.255.0 > > HWADDR=eth0 > > MASTER=undef > > SLAVE=undef > > > > The define is built like so: > > define network_interface ( > > $bootproto, > > $onboot, > > $ipaddr = undef, > > $netmask = undef, > > $hwaddr = undef, > > $master = undef, > > $slave = undef > > ){ <SNIP> } > > > > And the template looks like: > > ### This file is being managed by Puppet > > ### DO NOT EDIT > > DEVICE=<%= name %> > > BOOTPROTO=<%= bootproto %> > > ONBOOT=<%= onboot %> > > <% if ipaddr != "undef" -%> > > IPADDR=<%= ipaddr %> > > <% end -%> > > <% if netmask != "undef" -%> > > NETMASK=<%= netmask %> > > <% end -%> > > <% if final_hwaddr != "undef" -%> > > HWADDR=<%= final_hwaddr %> > > <% end -%> > > <% if master != "undef" -%> > > MASTER=<%= master %> > > <% end -%> > > <% if slave != "undef" -%> > > SLAVE=<%= slave %> > > <% end -%> > > > > Or I also tried: > > ### This file is being managed by Puppet > > ### DO NOT EDIT > > DEVICE=<%= name %> > > BOOTPROTO=<%= bootproto %> > > ONBOOT=<%= onboot %> > > <% if has_variable?("ipaddr") then -%> > > IPADDR=<%= ipaddr %> > > <% end -%> > > <% if has_variable?("netmask") then -%> > > NETMASK=<%= netmask %> > > <% end -%> > > <% if has_variable?("final_hwaddr") then -%> > > HWADDR=<%= final_hwaddr %> > > <% end -%> > > <% if has_variable?("master") then -%> > > MASTER=<%= master %> > > <% end -%> > > <% if has_variable?("slave") then -%> > > SLAVE=<%= slave %> > > <% end -%> > > Don''t quote it, has_variable?(slave). > > Nan > > -- > 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.
Argh, I''ll have to take that back. The docs are right, the quotes are in fact necessary. Without them has_variable will always return false. Here''s the rub: If a variable is never declared then has_variable returns false. If it''s set to ''undef'' then it returns true. That wouldn''t be so bad if you could use the comparison operators to check for ''undef'' but you can''t. I worked around it by using the string value "none" as my class default value so that the != operator would work in the template. The problem with that is that it breaks the ''if $variable { do stuff }'' construct which is so widely used. IMHO the current functionality should either be changed or specifically called out in the templating docs so users aren''t caught by surprise. On Tue, May 24, 2011 at 8:30 AM, Aaron Grewell <aaron.grewell@gmail.com>wrote:> Yup, that works. FYI there''s a bug in the docs. The example shown here: > http://docs.puppetlabs.com/guides/templating.html#undefined-variables > > Looks like so: > <% if has_variable?("myvar") then %> > myvar has <%= myvar %> value > <% end %> > > > On Mon, May 23, 2011 at 4:59 PM, Nan Liu <nan@puppetlabs.com> wrote: > >> On Mon, May 23, 2011 at 4:09 PM, Aaron Grewell <aaron.grewell@gmail.com> >> wrote: >> > I''ve got a conditional statement that isn''t working the way I expect. >> I''ve >> > created a define() for building network interfaces, but two of the >> entries >> > (master & slave) are only appropriate for bonded interfaces. I would >> like >> > them to be completely excluded from regular interfaces. This seems easy >> > enough according to the docs, set master & slave to ''undef'' and use the >> ''if >> > has_variable'' syntax in the template. It doesn''t work though. I also >> tried >> > using ''if variable != "undef"'' but this also does not work. >> Interestingly >> > the results of the two are exactly the same. I''m clearly missing >> > something. How do I get the template to skip these lines? >> > >> > The results end up looking like this: >> > ### This file is being managed by Puppet >> > ### DO NOT EDIT >> > DEVICE=eth0 >> > BOOTPROTO=none >> > ONBOOT=yes >> > IPADDR=192.168.1.1 >> > NETMASK=255.255.255.0 >> > HWADDR=eth0 >> > MASTER=undef >> > SLAVE=undef >> > >> > The define is built like so: >> > define network_interface ( >> > $bootproto, >> > $onboot, >> > $ipaddr = undef, >> > $netmask = undef, >> > $hwaddr = undef, >> > $master = undef, >> > $slave = undef >> > ){ <SNIP> } >> > >> > And the template looks like: >> > ### This file is being managed by Puppet >> > ### DO NOT EDIT >> > DEVICE=<%= name %> >> > BOOTPROTO=<%= bootproto %> >> > ONBOOT=<%= onboot %> >> > <% if ipaddr != "undef" -%> >> > IPADDR=<%= ipaddr %> >> > <% end -%> >> > <% if netmask != "undef" -%> >> > NETMASK=<%= netmask %> >> > <% end -%> >> > <% if final_hwaddr != "undef" -%> >> > HWADDR=<%= final_hwaddr %> >> > <% end -%> >> > <% if master != "undef" -%> >> > MASTER=<%= master %> >> > <% end -%> >> > <% if slave != "undef" -%> >> > SLAVE=<%= slave %> >> > <% end -%> >> > >> > Or I also tried: >> > ### This file is being managed by Puppet >> > ### DO NOT EDIT >> > DEVICE=<%= name %> >> > BOOTPROTO=<%= bootproto %> >> > ONBOOT=<%= onboot %> >> > <% if has_variable?("ipaddr") then -%> >> > IPADDR=<%= ipaddr %> >> > <% end -%> >> > <% if has_variable?("netmask") then -%> >> > NETMASK=<%= netmask %> >> > <% end -%> >> > <% if has_variable?("final_hwaddr") then -%> >> > HWADDR=<%= final_hwaddr %> >> > <% end -%> >> > <% if has_variable?("master") then -%> >> > MASTER=<%= master %> >> > <% end -%> >> > <% if has_variable?("slave") then -%> >> > SLAVE=<%= slave %> >> > <% end -%> >> >> Don''t quote it, has_variable?(slave). >> >> Nan >> >> -- >> 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.
On Tue, May 24, 2011 at 9:06 AM, Aaron Grewell <aaron.grewell@gmail.com> wrote:> Argh, I''ll have to take that back. The docs are right, the quotes are in > fact necessary. Without them has_variable will always return false.My bad, responded in haste. Unquoted in ruby is actually a variable, so no that wouldn''t work correctly.> Here''s the rub: If a variable is never declared then has_variable returns > false. If it''s set to ''undef'' then it returns true. That wouldn''t be so bad > if you could use the comparison operators to check for ''undef'' but you > can''t. I worked around it by using the string value "none" as my class > default value so that the != operator would work in the template. The > problem with that is that it breaks the ''if $variable { do stuff }'' > construct which is so widely used. IMHO the current functionality should > either be changed or specifically called out in the templating docs so users > aren''t caught by surprise.You can test if has_variable?("slave") && slave != :undef. Looking at the code doesn''t seem complicated to change so undef also returns false, but I''m not sure we want the same behavior from has_variable, perhaps under a different function name? Thanks, Nan -- 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.