saltstack自定义路由增加模块

本篇承接上一篇 <a href="https://www.361way.com/saltstack-route/5585.html" target="_blank" rel="noopener">saltstack实现批量路由增加</a> ,在上一篇中提到了使用saltstack自带的network state实现了redhat路由的自动配置,不过在suse系统下却无解,晚上没事写了个模块,实现了针对suse系统实现路由增加。代码如下:



<br />
#coding: utf-8
# code from www.361way.com
import commands
__outputter__ = {
  'suse': 'txt',
}
_SUSE_ROUTE_FILE = '/etc/sysconfig/network/routes'
def init():
    ywgw = __grains__['dcnyw']['ywgw']
    yweth = __grains__['dcnyw']['yweth']
    dcngw = __grains__['dcnyw']['dcngw']
    dcneth = __grains__['dcnyw']['dcneth']
    return ywgw,yweth,dcngw,dcneth
def routing():
    out = ""
    ywgw,yweth,dcngw,dcneth = init()
    if ywgw and yweth and dcngw and dcneth:
        r1 = "route add default " + ywgw
        r2 = "route add -net 10.0.0.0 netmask 255.0.0.0 gw " + dcngw
        r3 = "route add -net 200.200.0.0 netmask 255.255.0.0 gw " + dcngw
        r4 = "route add -net 10.211.6.0 netmask 255.255.255.0 gw " + ywgw
        r5 = "route add -net 10.125.0.0 netmask 255.255.0.0 gw " + ywgw
        out = commands.getoutput(r1 + '\n' + r2 +'\n' + r3 +'\n' + r4 +'\n' + r5)
    return out
def suse():
    if __grains__['os'] == 'SUSE':
        routing()
        ywgw,yweth,dcngw,dcneth = init()
        routestr = """default %s --
10.0.0.0 %s 255.0.0.0 %s
10.125.0.0 %s 255.255.0.0 %s
10.211.6.0 %s 255.255.255.0 %s
200.200.0.0 %s 255.255.0.0 %s
""" %(ywgw,dcngw,dcneth,ywgw,yweth,ywgw,yweth,dcngw,dcneth)
        f = open(_SUSE_ROUTE_FILE, 'w')
        f.writelines(routestr)
        f.close()
        cmd = 'netstat -A inet -rn |tail -n +3'
        return __salt__['cmd.run'](cmd)
    else:
                return 'OS not SuSE or can not get eth and gateway info!'
上面的代码需要注意,调用系统grains时,不能写在def函数体外,最开始我想将该定义写在函数体外,发现调用不成功,在minion端的/var/log/salt/minion日志中提示grains定义不存在。



具体执行效果见下图:



<img src="https://www.361way.com/wp-content/uploads/2017/02/sqroute.png" title="sqroute" alt="sqroute" width="667" height="198" />



上面示例中只实了suse 路由这一个功能,如果不想使用系统自带的network.route state功能,也可以把redhat下的路由增加功能在该模块中写下。



<br />

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注