一、安裝依賴
pip install f5-sdk
pip install urllib3
二、相關代碼,參考https://f5-sdk.readthedocs.io/
1、創建、操作pool
from f5.bigip import ManagementRoot # Connect to the BigIP mgmt = ManagementRoot("bigip.example.com", "admin", "somepassword") # Get a list of all pools on the BigIP and print their names and their # members' names pools = mgmt.tm.ltm.pools.get_collection() for pool in pools: print pool.name for member in pool.members_s.get_collection(): print member.name # Create a new pool on the BIG-IP mypool = mgmt.tm.ltm.pools.pool.create(name='mypool', partition='Common') # Load an existing pool and update its description pool_a = mgmt.tm.ltm.pools.pool.load(name='mypool', partition='Common') pool_a.description = "New description" pool_a.update() # Delete a pool if it exists if mgmt.tm.ltm.pools.pool.exists(name='mypool', partition='Common'): pool_b = mgmt.tm.ltm.pools.pool.load(name='mypool', partition='Common') pool_b.delete()
2、創建、操作member
members = pool_a.members_s member = pool_a.members_s.members # 創建 m1 = pool_a.members_s.members.create(partition='Common', name='192.168.101.50:80') m2 = pool_a.members_s.members.create(partition='Common', name='192.168.101.51:80') # load the pool members m1 = pool_a.members_s.members.load(partition='Common', name='192.168.101.50:80') m2 = pool_a.members_s.members.load(partition='Common', name='192.168.101.50:80') # Get all of the pool members for pool_1 and print their names for member in members: print member.name # Delete our pool member m1 m1.delete()
3、創建、操作virtual server(VS)
# 創建 try: mgmt = ManagementRoot("192.168.10.10", "admin", "admin") vs = mgmt.tm.ltm.virtuals.virtual.create( name='hn_vs3', partition='Common', destination='192.168.10.122:67', pool='mypool1', source='0.0.0.0/0' ) print vs.raw except Exception as e: if isinstance(e, iControlUnexpectedHTTPError): text = json.loads(e.response.text) response['meaasge'] = text['message'] else: response['meaasge'] = e # 操作 response = {} try: mgmt = ManagementRoot("192.168.10.10", "admin", "admin") vs = mgmt.tm.ltm.virtuals.virtual.load( name='vs_test1', partition='Common' ) if hasattr(vs, 'enabled'): # 禁用 vs.disabled = True else: # 啟用 vs.enabled = True vs.update() except Exception as e: print e if isinstance(e, iControlUnexpectedHTTPError): text = json.loads(e.response.text) response['meaasge'] = text['message'] else: response['meaasge'] = e
注意事項:1、VS創建之后的狀態與pool的狀態有直接關系。
2、pool創建之后的狀態與member和Health Monitors有關。
3、錯誤處理,如果錯誤異常類型為icontrol.exceptions.iControlUnexpectedHTTPError時,需要特殊處理取得message信息。
text = json.loads(e.response.text) message = text['message']
4、狀態為啟動時,屬性enabled為True,並沒有disabled屬性。為禁用時同理。
5、想要打印出pool、member、vs的詳細信息,可以使用raw屬性。