上章Jenkins build 內寫了一個remote,裡面內容是python執行去呼叫F5做上下線動作
下面介紹F5 python Script
https://devcentral.f5.com/questions/add-value-in-data-group-via-rest-api
rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm
yum -y install python-pip
pip
pip install requests
F5上需開啟icrd功能,測試是用版本11.6
https://devcentral.f5.com/questions/failed-to-use-rest-api-404s-53301
bigstart stop icrd
bigstart start icrd
tmsh show /sys service icrd
輸入python 進去demo 一下
首先帶入帳密post到login,取到token代入驗證去執行enabled動作
#python
>>> import requests, json, argparse, getpass, sys, time
>>> requests.packages.urllib3.disable_warnings()
>>> payload = {}
>>> payload['username'] = 'admin'
>>> payload['password'] = 'ABC'
>>> payload['loginProviderName'] = 'tmos'
>>> b = requests.session()
>>> b.headers.update({'Content-Type':'application/json'})
>>> b.auth = ('admin', 'ABC')
>>> b.verify = False
>>> b.post('https://172.16.1.1/mgmt/shared/authn/login', json.dumps(payload)).json()['token']['token']
u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
>>> b.headers.update({'X-F5-Auth-Token':'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'})
>>> payload = {}
>>> payload['state'] = 'user-up'
>>> payload['session'] = 'user-enabled'
>>> b.put('https://172.16.1.1/mgmt/tm/ltm/pool/pool_test/members/~Common~123.12.123.123:80', json.dumps(payload)).json()
{u'state': u'up', u'kind': u'tm:ltm:pool:members:membersstate', u'inheritProfile': u'enabled', u'name': u'123.12.123.123:80', u'priorityGroup': 0, u'generation': 1111, u'partition': u'Common', u'ephemeral': u'false', u'fqdn': {u'autopopulate': u'disabled'}, u'rateLimit': u'disabled', u'session': u'monitor-enabled', u'dynamicRatio': 1, u'connectionLimit': 0, u'address': u'123.12.123.123', u'logging': u'disabled', u'fullPath': u'/Common/123.12.123.123:80', u'ratio': 1, u'selfLink': u'https://localhost/mgmt/tm/ltm/pool/pool_test/members/~Common~123.12.123.123:80?ver=11.6.1', u'monitor': u'default'}
http://docs.python-requests.org/zh_CN/latest/user/advanced.html