common template instead of two

9 views
Skip to first unread message

Pearl Raj

unread,
Jun 27, 2016, 11:54:55 AM6/27/16
to Puppet Users

Hi, While setting up persistant load balancer configuration in linux virtul server, I ended up with following two templates. 

On the load balancer: In lvs/manifests/ifcfg-lo_lb.erb
 
DEVICE=eth1:lb
IPADDR=<IP_ADDRESS>
NETMASK=255.255.255.0
ONBOOT=yes
NM_CONTROLLED=no
 
On the app servers: In lvs/manifests/ifcfg-eth1_lb.erb 
 
DEVICE=lo:lb
IPADDR=<IP_ADDRESS>
NETMASK=255.255.255.255
ONBOOT=yes

[these params is to be put in hiera]

Templates are instantiated in the following puppet classes 

1) lvs/manifests/appserver.pp

class lvs::appserver ...{

file { '/etc/sysconfig/network-scripts/ifcfg-lo:lb':
  ensure  => present,
  content => template("${module_name}/ifcfg-lo_lb.erb"),
  owner   => root,    
}
}

2) lvs/manifests/lb.pp

class lvs::lb ... {

file { '/etc/sysconfig/network-scripts/ifcfg-eth1:lb':
  ensure  => present,
  content => template("${module_name}/ifcfg-eth1_lb.erb"),
  owner   => root,    
}  

}

Is it possible to use one common template instead of two with some conditional statements differentiating load balancer and app server? 

Regards,
Pearl

Peter Kristolaitis

unread,
Jun 27, 2016, 12:40:48 PM6/27/16
to puppet...@googlegroups.com
On 2016-06-27 11:54 AM, Pearl Raj wrote:

Hi, While setting up persistant load balancer configuration in linux virtul server, I ended up with following two templates. 

On the load balancer: In lvs/manifests/ifcfg-lo_lb.erb
 
DEVICE=eth1:lb
IPADDR=<IP_ADDRESS>
NETMASK=255.255.255.0
ONBOOT=yes
NM_CONTROLLED=no
 
On the app servers: In lvs/manifests/ifcfg-eth1_lb.erb 
 
DEVICE=lo:lb
IPADDR=<IP_ADDRESS>
NETMASK=255.255.255.255
ONBOOT=yes

...(snip)...


Is it possible to use one common template instead of two with some conditional statements differentiating load balancer and app server? 


Absolutely.  There are a couple ways.

Firstly, there's nothing stopping you from setting NM_CONTROLLED=yes ... so perhaps the obvious solution is to just define a variable that has either the value "yes" or "no" depending on the server's role and you fill the value into the template the same way you do with the IP address.   You will need to use this approach to template the device name.

The other option is to conditionally include the line.  All you need to do is define some variable (doesn't matter if it comes from Hiera or is a local variable inside your class) do something like this in your template:

<% if @is_load_balancer -%>
NM_CONTROLLED=no
<% end -%>

When @is_load_balancer evaluates to true, then the line gets included in the output.  If it evaluates to false, it gets excluded from the output.

Note the use of "-%>" instead of "%>" -- that prevents you from getting extra blank lines in the final output.  Also note the use of "<%" instead of "<%=".... the "<%" syntax allows you to run any arbitrary Ruby code you want, whereas "<%=" outputs the value of a variable.

Because "<%" allows you to execute arbitrary Ruby code, you can not only do conditional logic as shown above, but also iteration and other fun things, for example:

<% @vhosts.each do |vhost| -%>
            <%= vhost %>
<% end -%>

Which would take an array and output each element on its own line.
Reply all
Reply to author
Forward
0 new messages