python开发F5相关功能
一、安装依赖
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属性。