网站首页 文章专栏 python 脚本查看ansible-runner-service任务运行结果
脚本一是测试,不完善(比如:如果在运行时可以持续打印,等)
大家可以优化后,发我。投搞
提前 把 证书拷贝到 脚本同一个目录下
# 此脚本有一个弊端,如果 playbook 正在运行不会持续打印。如果想看 playbook 完整的打印信息。可以多次执行。在 job 运行结束后,打印的输出结果才为完整的.
import requests requests.packages.urllib3.disable_warnings() # 执行playbook 返回的 playbook 执行ID, 只修改这个变量,就可以运行了。 play_uuid = '49c13fae-954b-11ed-8a58-00155d3d0101' req = requests.get('https://192.168.10.155:5001/api/v1/playbooks/' + play_uuid, cert=('client.crt', 'client.key'), verify=False) print('playbook运行状态: {0}'.format(req.json()['msg'])) # print(req.json()) req = requests.get('https://192.168.10.155:5001/api/v1/jobs/{0}/events'.format(play_uuid), cert=('client.crt', 'client.key'), verify=False) for event_key in req.json()['data']['events']: req = requests.get('https://192.168.10.155:5001/api/v1/jobs/{0}/events/{1}'.format(play_uuid, event_key), cert=('client.crt', 'client.key'), verify=False) print(req.json()['data']['stdout']) # print(req.text)
# 此脚本 是执行playbook的脚本
import json import requests
requests.packages.urllib3.disable_warnings()
data = { "time_delay": 30, "variable_host": "192.168.10.12" }
headers = { 'Content-Type': 'application/json' }
req = requests.post('https://192.168.10.155:5001/api/v1/playbooks/runnertest.yml', data=json.dumps(data), headers=headers, cert=('client.crt', 'client.key'), verify=False) print(req.text)