我的目录结构如下:
`~/ansible-environments/aws/random_name_1/inventory/group_vars/all`
~/ansible-environments/aws/random_name_2/inventory/group_vars/all`
~/ansible-environments/aws/random_name_3/inventory/group_vars/all`
所有文件看起来像
`
alias: bloodyhell
vendor_id: 42
isv_secret_key: "{{ vendor_id }}-SecretKey"
isv_secret_key_id: "{{ vendor_id }}-SecretKeyId"
products: rms_scl
LMIT: 80
sales_order_no: 'RMS_BSO_8182'
`
我需要编写一个可以遍历此目录结构的python脚本,然后提取别名,LMIT,产品,并将内容写入csv文件中:
``` 别名,LMIT,产品 bloodyhell,80,rms_scl
。 。 等等 。 ``` 我编写了如下代码:
import os
import sys
rootdir='/home/ubuntu/ansible-environments/aws'
#print "aa"
for root, subdirs, files in os.walk(rootdir):
# print "a"
for j in subdirs:
try:
mypath = rootdir + '/' + j + '/' + 'inventory/group_vars/all'
print mypath
fo = open(mypath, 'r')
f = fo.readlines()
# print f
for line in f:
# print line
if ('isv_alias' in line or 'LMID' in line or 'products' in line):
path = rootdir + '/' + 'list.csv'
fi = open(path, 'a')
fi.write(line)
fi.close()
fo.close()
except:
pass
这是我得到的结果:
`isv_alias: "bloodhell"^M
isv_alias: bloodhell
products: rms_scl
LMID: 80
isv_alias: rmstest2
products: rms_scl
LMID: 80
isv_alias: suricatatest
products: rms_ems_hosted
LMID: 26
有人可以帮我解决一下吗?