这是很早前写的一个样例,基于业务模板,系统自动化创建业务配置
业务输入模板(可以通过任何形式产生,例如一个自服务的portal website):
createvs.txt createpool.txt createmonitor.txt
中间程序F5_Deploy.py, 中间输出 tmsh.txt (作为ssh.py的输入)
(如果系统支持iControlRest,可以直接利用Python执行REST)
自动执行ssh.py
基于业务模板自动化配置F5业务
1 文件 3.91 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
###Tested on v11.6### #only support tcp,http,icmp monitor now ## Created by WWW.MYF5.NET @2015 def createmonitor(): try: f=file('createmonitor.txt') lines=f.readlines() print lines l=[] for line in lines: print line #get value of each line linedata=line.split(",")[1] #remove"\n" i=linedata.split("\n")[0] print i l.append(i) print l if l[6]=="enabled": if l[0]=="icmp" and l[7]!="" and l[7]!="*": #if it is icmp monitor, only need ip and it has an valid ip tr="transparent enabled destination %s"%l[7] else: #for other monitors, ip and port must be there if l[7]=="" or l[7]=="*" or l[8]=="" or l[8]=="*": print "Error: With transparent enabled, must have ip and port" return else: tr="transparent enabled destination %s:%s"%(l[7],l[8]) else: #not transparent, could set alias, f5 only support *:* or *:port or ip:port format if l[0]=="icmp": #icmp monitor,cant have port setting if l[7]=="" or l[7]=="*": #default setting tr="" else: tr="destination %s"%l[7] else: #not icmp,support *:* or *:port or ip:port if (l[7]=="" or l[7]=="*") and (l[8]=="" or l[8]=="*"): #default setting tr="" elif l[7]!="" and l[7]!="*" and (l[8]=="" or l[8]=="*"): #it is ip:*, dont support print "Error: Dont support ip:* alias settting" return else: if l[7]=="": l[7]="*" tr="destination %s:%s"%(l[7],l[8]) if l[9]=="enabled": res="manual-resume enabled" else: res="" if l[4]!="" and l[5]!="" and l[0]!="icmp": sr="send \'%s\' recv \'%s\'"%(l[4],l[5]) else: sr="" tmsh="tmsh create ltm monitor %s %s interval %s timeout %s %s %s %s\n"%(l[0],l[1],l[2],l[3],sr,tr,res) print tmsh f.close() out=file('tmsh.txt','a') out.write(tmsh) out.close except: print "Cant open the file,or something wrong in during creating pool. Please turn on debug for detail." def createpool(): try: tmsh="" monitor="" member="" mr="" noratio=1 sda="" f=file('createpool.txt') lines=f.readlines() print lines l=[] for line in lines: print line #get value of each line linedata=line.split(",")[1] #remove "\n" i=linedata.split("\n")[0] print i #create string list for pool l.append(i) print l if l[1]=="": print "No monitor, will use tcp as default" monitor="monitor tcp" print l else: #handle monitor if it is more than 1 if l[2]=="": #no min of monitor setting monitorstring=l[1].replace("|"," and ") monitor="monitor %s"%monitorstring else: #consider min of monitor monitorstring=l[1].replace("|"," ") monitor="monitor min %s of { %s }"%(l[2],monitorstring) print monitor print l if l[3]=="": sda="" else: sda="service-down-action %s"%l[3] if l[4]=="ratio-member" or l[4]=="ratio-least-connections-member": noratio=0 if l[4]=="": l[4]="round-robin" if l[5]=="": #no priority group setting p="" else: #has priority, need set min-active-members in tmsh p="min-active-members %s"%l[5] if l[6]=="": print "No members!" return else: #handle members if it is more than 1 member=l[6].split("|") for m in member: m1=m.split(":") if m1[2]=="" or noratio==1: m1[2]="1" if m1[3]=="": m1[3]="0" if p: mr=mr + " %s:%s { ratio %s priority-group %s} "%(m1[0],m1[1],m1[2],m1[3]) else: mr=mr + " %s:%s { ratio %s } "%(m1[0],m1[1],m1[2]) print mr tmsh="tmsh create ltm pool %s members add {%s} %s load-balancing-mode %s %s %s\n"%(l[0],mr,monitor,l[4],sda,p) f.close() out=file('tmsh.txt','a') out.write(tmsh) out.close print tmsh except: print "Cant open the file,or something wrong in during creating pool. Please turn on debug for detail." def createvs(): try: f=file('createvs.txt') lines = f.readlines() print lines vlan="" snat="" l=[] tmsh='' for line in lines: print line #get value of each line linedata=line.split(",")[1] #remove "\n" i= linedata.split("\n")[0] print i #create string list for vs l.append(i) print l if l[3]=="": #vs_type is blank, so it will be performance l4 or standard vs if l[13]!="": #vlan enabled_on has setting vlan="vlans add { %s } vlans-enabled"%l[13] if l[14]!="": #vlan disabled_on has setting vlan="vlans add { %s } vlans-disabled"%l[14] if l[15]!="": #it is snat automap snat="source-address-translation { type automap } " if l[16]!="": #it is snat pool snat="source-address-translation { type snat pool %s }"%l[16] tmsh="tmsh create ltm virtual %s destination %s:%s ip-protocol %s profiles add { %s %s %s %s %s %s %s %s} %s %s mirror %s persist replace-all-with { %s %s } pool %s rules { %s %s} %s\n"\ %(l[0],l[1],l[2],l[4],l[5],l[6],l[7],l[8],l[9],l[10],l[11],l[12],vlan,snat,l[17],l[18],l[19],l[20],l[21],l[22],l[23]) elif l[3]=="ipforwarding": print "it is ip forwarding vs" return else: print "Not support vs type" return print tmsh f.close() out=file('tmsh.txt','a') out.write(tmsh) out.close() except: print "Cant open the file,or something wrong in during creating vs. Please turn on debug for detail." createmonitor() createpool() createvs() |
文章评论