将ansible执⾏结果进⾏处理,变成excel
ansible输出结果,统计起来很⿇烦,将结果变为excel,并且按照结果统计汇总,可读性会强很多
ansible输出结果:
ansible输出结果
192.168.250. 250| SUCCESS | rc=0 >>
Selinux check success!This gbase01's selinux is Disabled and selinux config is SELINUX=disabled.
Swappiness check failed! This gbase01's swappiness is 60.
Ulimit check success! This gbase01's ulimit is 655360.
Hugepage check failed! This gbase01's hugepage status is never.
Timezone check success! This gbase01's timezone is +0800.
This gbase01's kernel is 2.6.32-431.el6.x86_64.
This gbase01's openssl is openssl-1.0.1e-30.el6.x86_64.
CPU check success! This gbase01's CPU number is 4.
Mem check success! This gbase01's Mem size is 16GB.
Hostname check success! This gbase01 is right.
192.168.152.222 | UNREACHABLE! => {
"changed": false,
"msg": "mkdir: cannot create directory `/root/.ansible/tmp/ansible-tmp-1536203934.77-45972825051570': No space left on device\n",    "unreachable": true
}
使⽤代码将ansible输出结果进⾏处理
import pandas as pd
import re
import numpy as np
#打开⽂件
with open(r'F:\python教程\练⼿程序\a.txt','r') as f:
ad()
data = text.strip()
place("|","\n").replace(">>","\n").replace("{","\n").replace("}","\n").replace(r"\r\n","\n")#处理⽂本中的特殊字符
data1 =data.split("\n")
data2 = [i for i in data1 if i != '']
rs = []
#将ip匹配出来,放在列表中
ip = ""
state=['SUCCESS','FAILED','UNREACHABLE']
status=''
result=''
info=''
for d in data2:
if re.findall('\d+\.\d+\.\d+\.\d', d):#如果在⾏中到ip地址
ip = d
#print(d)
elif d.strip() in state:#输出状态值
execl函数 linuxstatus=d
else:
if len(d.split('!'))==2:
result=d.split('!')[0]
info=d.split('!')[1]
rs.append((ip,status, result,info))#以ip,每⾏内容,作为元素加到列表中
else:
result=d
info=''
rs.append((ip,status, result,info))
df = pd.DataFrame(rs, columns=["ip","status","result", "info"])#从列表创建DataFrame,指定data和columns
df = df.groupby(["ip","status"]).apply(lambda x: x["info"])#apply函数会⾃动遍历每⼀⾏DataFrame的数据,最后将所有结果组合成⼀个Series数据结构并返回。df.to_excel("test2.xlsx")#将结果输出变成exc
el
输出结果⼿动调整⼀下,如上,over

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。