-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible_index.py
53 lines (50 loc) · 2.01 KB
/
ansible_index.py
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
import json
import datetime
import random, string
from flask import g, current_app
from backend.models import AnsibleTasks
from celery_tasks.tasks import sync_ansible_result, ansible_playbook_exec, ansible_exec, error_handler
from backend.extensions import db
class AnsibleOpt:
@staticmethod
def ansible_playbook(hosts, playbook, option, user=None, extra_vars={}, **kw):
# 调用 celery 的 ansiblePlayBook 任务,将任务推送给 celery 并写入 db
tid = "AnsibleApiPlaybook-%s-%s" % (''.join(random.sample(string.ascii_letters + string.digits, 8)),
datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
# if not extra_vars.get('groupName'):
# extra_vars['groupName'] = groupName
celery_task = ansible_playbook_exec.apply_async(
(tid, hosts, playbook, extra_vars), link=sync_ansible_result.s(tid=tid),
link_error=error_handler.s()
) # 保存 ansible 结果
# print(tid, celery_task.task_id, extra_vars, playbook, "===========")
try:
at = AnsibleTasks(
ansible_id=tid,
celery_id=celery_task.task_id,
user=g.user,
extra_vars=json.dumps(extra_vars),
playbook=playbook,
option_id=option,
)
at.state = 1
db.session.add(at)
db.session.commit()
return {
"playbook": playbook,
"extra_vars": extra_vars,
"ansible_id": tid,
"celery_task": celery_task.task_id,
"pk": at.id
}
except Exception as e:
print(e)
db.session.rollback()
@staticmethod
def ansible_opt():
tid = "AnsibleApiOpt-%s" % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tasks = []
celery_task = ansible_exec.delay(tid, tasks)
return {'tid': tid,
'celery_task': celery_task.task_id,
}