有没有大佬会把这个脚本改成 python3.9 + ansible2.9.27 的,求 i 求了

2023-11-22 21:12:07 +08:00
 miaosl
#!/usr/bin/env python
# coding=utf-8
import os
import sys
libpath = os.path.join(os.path.dirname(__file__),'lib')
sys.path.insert(0,libpath)
from ansible import playbook, callbacks
import logging
import pprint
import time
DT=time.strftime('%Y%m%d')
TM=time.strftime('%H')
#import xlsxwriter
#workbook = xlsxwriter.Workbook('array_formula.xlsx')
def myprint(color,mes):
"""
用于打印颜色
:param color: 颜色
:param mes: 打印内容
:return: 没有返回
"""
"""
30: 黑色
31: 红色
32: 绿色
33: 黄色
34: 蓝色
35: 紫色
36: 深绿色
37: 白色

"""

info = {'red':31, 'green': 32, 'yellow':33, 'blue':34,'dark_green':36,'default':37}
if color in info:
fore = info[color]
else:
fore = 37
color = "\x1B[%d;%dm" % (1,fore)
print("%s%s\x1B[0m" % (color,mes))

class LoggingCallbacks(callbacks.PlaybookCallbacks):
def log(self, level, msg, *args, **kwargs):
logging.log(level, msg, *args, **kwargs)
print msg
def on_task_start(self, name, is_conditional):
#self.log(logging.INFO, 'task: {0}'.format(name))
#super(LoggingCallbacks, self).on_task_start(name, is_conditional)
#la.append('name')
resall=u'TASK:'+name+'*'*40+'\n'
myprint("default",resall)
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-ok.txt', 'a') as f:
f.write(resall)
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-error.txt', 'a') as f:
f.write(resall)
class LoggingRunnerCallbacks(callbacks.PlaybookRunnerCallbacks):
def log(self, level, msg, *args, **kwargs):
logging.log(level, msg, *args, **kwargs)

def _on_any(self, level, label, host, orig_result):
result = orig_result.copy()
result.pop('invocation', None)
result.pop('verbose_always', True)
item = result.pop('item', None)
if not result:
msg = ''
elif len(result) == 1:
msg = ' | {0}'.format(result.values().pop())
else:
msg = '\n' + pprint.pformat(result)
if item:
self.log(level, '{0} (item={1}): {2}{3}'.format(host, item, label, msg))
else:
self.log(level, '{0}: {1}{2}'.format(host, label, msg))

def on_failed(self, host, res, ignore_errors=False):
if ignore_errors:
level = logging.INFO
label = 'FAILED (ignored)'
else:
level = logging.ERROR
label = 'FAILED'
self._on_any(level, label, host, res)
super(LoggingRunnerCallbacks, self).on_failed(host, res, ignore_errors)
resall=host+ u'\nFAILED:\n{0}'+format(res['stderr'])
myprint("red",resall)
myprint("red",res['stdout']+'\n'+'\n')
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-error.txt', 'a') as f:
f.write(resall)
f.write(res['stdout']+'\n'+'\n')
def on_ok(self, host, res):
#self._on_any(logging.INFO, 'SUCCESS', host, res)
super(LoggingRunnerCallbacks, self).on_ok(host, res)
#print host,res
resall=host+ u'\n'+format(res['stdout'])
myprint("green",resall)
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-ok.txt', 'a') as f:
f.write(resall+'\n')
def on_error(self, host, msg):
self.log(logging.ERROR, '{0}: ERROR | {1}'.format(host, msg))
super(LoggingRunnerCallbacks, self).on_error(host, msg)
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-error.txt', 'a') as f:
f.write(str(host)+'\n')
f.write(str(msg)+'\n')
def on_skipped(self, host, item=None):
if item:
self.log(logging.INFO, '{0} (item={1}): SKIPPED'.format(host, item))
else:
self.log(logging.INFO, '{0}: SKIPPED'.format(host))
super(LoggingRunnerCallbacks, self).on_skipped(host, item)
def on_unreachable(self, host, res):
#self._on_any(logging.ERROR, 'UNREACHABLE', host, dict(unreachable=res))
super(LoggingRunnerCallbacks, self).on_unreachable(host, res)
resall=host+ u'\nERROR:CONNECT FAILD:\n{0}\n'+format(res)
myprint("red",resall+'\n')
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-error.txt', 'a') as f:
f.write(resall+'\n')
def on_no_hosts(self):
self.log(logging.ERROR, 'No hosts matched')
super(LoggingRunnerCallbacks, self).on_no_hosts()
def run_playbook(name):
if not os.path.exists('/root/ans/log/'+DT):
os.mkdir('/root/ans/log/'+DT)
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-ok.txt', 'w') as f:
f.write('Today is '+DT+' Let the check begin'+'\n\n')
with open('/root/ans/log/'+DT+'/'+TM+'-'+sys.argv[1]+'-error.txt', 'w') as f:
f.write('Today is '+DT+' Let the check begin'+'\n\n')
stats = callbacks.AggregateStats()
playbook_cb = LoggingCallbacks(verbose=3)
runner_cb = LoggingRunnerCallbacks(stats, verbose=3)

pb = playbook.PlayBook(
playbook=name,
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
)
pb.run()

if __name__ == "__main__":
run_playbook(sys.argv[1])
1855 次点击
所在节点    云计算
12 条回复
Jat001
2023-11-22 22:38:37 +08:00
6 ,python 没了缩进还看啥
ysicing
2023-11-22 22:59:59 +08:00
为啥不自己试试 gpt 呢,然后再来问呢。
tomczhen
2023-11-22 23:36:33 +08:00
学生吗?要不再放个收款码?改完代码额外 v 你 50 。
Enzoliu
2023-11-23 02:51:47 +08:00
目前的行业现状是:人手一个 GPT ,最次也得是 3.5
直接扔给 GPT 让它给改就是了
dayeye2006199
2023-11-23 07:58:18 +08:00
你先贴个 gist 吧
Goooooos
2023-11-23 08:41:08 +08:00
这就是我烦 python 的点,缩进一乱了,要恢复得靠猜
kingmo888
2023-11-23 08:49:28 +08:00
就你这么缩进的样,别求求了,没诚意
miaosl
2023-11-23 08:56:37 +08:00
@Enzoliu 是这样想的但是 4 也上了,改不了一点
miaosl
2023-11-23 08:57:06 +08:00
@Jat001 呀没注意当时复制下来就直接粘贴了
miaosl
2023-11-23 08:57:42 +08:00
@kingmo888 抱歉,没注意哎
knightdf
2023-11-23 09:33:06 +08:00
你加钱会有人帮你做的
hupeng
2023-11-23 09:39:56 +08:00
那我给你报个价吧 2w ,先付 50%定金

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/994314

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX