Templates

Thomas Neumann blacky+fai at fluffbunny.de
Tue Aug 24 19:39:21 CEST 2010


hiya

Just an update on the template engine modification. So far I'm very happy
with my modification to Thomas' and Kai's original work. I like to give an
appetizer what is possible by now. (Does anybody care? The feedback from
my previous posts was kinda non existent.)

This is a template configuring a SLES10 or SLES11 network interface (eth1)
[I know it looks quite intimidating.]

The input:
(snippet of a yaml file)

------------------------------------------------
[...]
interfaces:
[...]
  1:
    iface:
      mac: 00:50:56:3F:85:82
      name: eth1
    ip4:
      -
        ip: 10.255.133.130
        is_auto: true
        netmask: 255.255.255.0
        gateway: 10.255.133.254
      -
        ip: 10.255.133.131
        is_auto: false
        netmask: 255.255.255.0
        gateway: 10.255.133.254
      -
        ip: 10.255.133.132
        is_auto: false
        netmask: 255.255.255.0
        gateway: 10.255.133.254
[...]
------------------------------------------------

The template:

1)  decides what the filename is going to be
SLES10: /etc/sysconfig/network/ifcfg-eth-id-00:50:56:3F:85:82
SLES11: /etc/sysconfig/network/ifcfg-eth1

2) figures out if the config file is needed
(maybe there is no interface to configure?)

3) creates a suitable configuration for the primary IP

4) and optionally creates config stanzas for virtual IPs

hints:

--- meta --- contains control information for the template engine
--- body --- contains the actual template
[%#  %] are comments
[%  -%] tells the template engine to skip printing "newline"

For easier understanding I strongly suggest to copy the template to a file
and properly indent it. Sadly this can't be done in the template, else the
tabs and spaces would be included in the output file.

the actual template:

------------------------------------------------
[% DEFAULT id = 1 %]
[% ip_counter = 0 %]
--- meta ---
description: network config file for SuSE installations
[% IF files.netconfig.idstring == "mac" %]
	filename: /etc/sysconfig/network/ifcfg-eth-id-[% interfaces.$id.iface.mac %]
[% ELSE %]
	filename: /etc/sysconfig/network/ifcfg-[% interfaces.$id.iface.alias %]
[% END %]
[% IF interfaces.$id %]
	present: true
[% ELSE %]
	present: false
[% END %]
--- body ---
[% FOREACH ip4item IN interfaces.$id.ip4 -%]
[% IF ip_counter == 0 -%]
BOOTPROTO='static'
BROADCAST='[% interfaces.$id.ip4.$ip_counter.broadcast %]'
ETHTOOL_OPTIONS=''
IPADDR='[% interfaces.$id.ip4.$ip_counter.ip %]'
MTU=''
NAME='[% interfaces.$id.ip4.$ip_counter.hwname %]'
NETMASK='[% interfaces.$id.ip4.$ip_counter.netmask %]'
NETWORK='[% interfaces.$id.ip4.$ip_counter.network %]'
REMOTE_IPADDR=''
[%# default the startmode to auto if no value is given -%]
[% SWITCH interfaces.$id.ip4.$ip_counter.is_auto -%]
[% CASE false -%]
STARTMODE='manual'
[% CASE DEFAULT -%]
STARTMODE='auto'
[% END -%]
[% ELSE -%]
LABEL_[% ip_counter %]='[% ip_counter %]'
IPADDR_[% ip_counter %]='[% interfaces.$id.ip4.$ip_counter.ip %]'
NETMASK_[% ip_counter %]='[% interfaces.$id.ip4.$ip_counter.netmask %]'
[% END -%]
[% ip_counter = ip_counter + 1 -%]
[% END -%]
------------------------------------------------

output:

BOOTPROTO='static'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR='10.255.133.130'
MTU=''
NAME=''
NETMASK='255.255.255.0'
NETWORK=''
REMOTE_IPADDR=''
STARTMODE='auto'
LABEL_1='1'
IPADDR_1='10.255.133.131'
NETMASK_1='10.255.133.131'
LABEL_2='2'
IPADDR_2='10.255.133.132'
NETMASK_2='10.255.133.132'


tschüß
thomas



More information about the linux-fai mailing list