saltstack实现批量路由增加
一、需求
现网之前有两个机房,每台主机上有两个网段,三条路由 ——– 业务网段为192.168.X.X,管理网段是10.X.X.X,默认路由走的是业务网段,10.211.6网段是走互联网F5映射用的。后来又新增了一个机房,并根据总部要求分配了两个网段,10.125.X.X 网段是新机房的业务网段,200.200.X.X是新机房的管理网段 。同时三个机房需要互相能够通信。所以最终机器上需要有5条静态路由 。
110.X.X.X            255.0.0.0            DCN gateway
2200.200.X.X         255.255.0.0          DCN gateway
310.211.6.0          255.255.255.0        DCN gateway
410.125.0.0          255.255.0.0          业务 gateway
50.0.0.0             0.0.0.0              业务 gateway
由于默认之前的两个机房上已经有了10.X.X.X 、0.0.0.0 、10.211.6.0三个网段的路由。所以这里就结合下salt 完成所有主机的路由配置。
二、saltstack配置
在master端两个配置文件内容如下:
1[root@itohost ~]# tree /srv/salt
2/srv/salt
3├── _grains
4│   ├── dcnyw.py
5├── route2.sls
dcnyw.py内容如下:
 1# cat /srv/salt/_grains/dcnyw.py
 2import commands
 3def dcn_yw():
 4    '''
 5    Return dcn and yw eth informations
 6    '''
 7    grains = {}
 8    ret = {}
 9    cmd1 = 'netstat -A inet -rn | grep "^0.0.0.0"|tail -1'
10    cmd2 = 'netstat -A inet -rn | grep "^10.0.0.0"|tail -1'
11    ywout = commands.getoutput(cmd1)
12    dcnout = commands.getoutput(cmd2)
13    if ywout:
14       comps1 = ywout.split()
15       ret['ywgw'] = comps1[1]
16       ret['yweth'] = comps1[7]
17    if dcnout:
18       comps2 = dcnout.split()
19       ret['dcngw'] = comps2[1]
20       ret['dcneth'] = comps2[7]
21    grains['dcnyw'] = ret
22    return grains
通过salt ‘*’ saltutil.sync_grains同步到minion端后,可以通过该自定义的grains获取到业务和管理网段的网关信息和对应的网口信息(由于主机有二三千台之多,对应的接口有eth0、eth1、也有bond0、bond1、还有ib0、ib1,所以需要通过路由信息获取接口名称)。
salt 下对应的有network这个state模块,所认可以很方便的利用该模块完成路由配置。对应的sls文件内容如下:
 1# cat /srv/salt/route2.sls
 2routes:
 3  network.routes:
 4    - name: {{ grains['dcnyw']['yweth']}}
 5    - routes:
 6      - name: sq
 7        ipaddr: 10.125.0.0
 8        netmask: 255.255.0.0
 9        gateway: {{ grains['dcnyw']['ywgw']}}
10      - name: internet
11        ipaddr: 10.211.6.0
12        netmask: 255.255.255.0
13        gateway: {{ grains['dcnyw']['ywgw']}}
14      - name: default
15        ipaddr: 0.0.0.0
16        netmask: 0.0.0.0
17        gateway: {{ grains['dcnyw']['ywgw']}}
18    - name: {{ grains['dcnyw']['dcneth']}}
19    - routes:
20      - name: route to 10.0.0.0
21        ipaddr: 10.0.0.0
22        netmask: 255.0.0.0
23        gateway: {{ grains['dcnyw']['dcngw']}}
24      - name: route to 200.200.0.0
25        ipaddr: 200.200.0.0
26        netmask: 255.255.0.0
27        gateway: {{ grains['dcnyw']['dcngw']}}
剩下的就比较简单了,可以通过salt ‘匹配的主机’ state.sls route2 执行,在相应的主机上增加对应的路由信息,并在路由配置文件中增加配置。不过在执行的时候,发现一个问题。有些主机上在执行的时候会有报错,如下:
1ID: routes
2    Function: network.routes
3        Name: eth1
4      Result: False
5     Comment: State 'network.routes' was not found in SLS 'route2'
6              Reason: 'network' __virtual__ returned False
经确认发现执行出错的都是suse系统的主机,通过查看state network帮助信息,发现没有返回:
通过查看官方源代码发现只支持redhat相关系统,如下:
针对suse的问题也并非无解,可以通过自已写一个模块解决,也可以写一个普通脚本,通过cmd.script 执行,更可以通过修改network.py这个states文件,增加对suse的支持。
本篇先到这里吧,后面针对suse的问题,个人偏向写一个模块解决 。后面有时间再搞篇。
捐赠本站(Donate)
 如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/saltstack-route/5585.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.
 
    