-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-config-from-yaml.py
More file actions
39 lines (34 loc) · 1.38 KB
/
Copy pathget-config-from-yaml.py
File metadata and controls
39 lines (34 loc) · 1.38 KB
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
"""
YAML配置信息批量提取脚本
功能描述:
该脚本用于从指定目录中的多个YAML配置文件中批量提取关键信息,
如服务名称和容器镜像版本等。特别适用于Kubernetes环境中的
配置审计、版本清单生成、资源盘点等场景。
技术实现:
- os.listdir(): 遍历指定目录中的所有文件
- file.endswith('.yaml'): 过滤YAML文件
- yaml.safe_load(): 安全地解析YAML文件内容
- os.path.join(): 构建完整的文件路径
- 字典嵌套访问: 提取深层嵌套的配置字段
"""
# 从两个yaml文件中获取到deployment的name和image
import yaml, os
def get_config(yaml_dir):
report = []
for file in os.listdir(yaml_dir):
if file.endswith('.yaml'):
# 把yaml文件读取成字典
with open(os.path.join(yaml_dir,file), 'r') as f:
config = yaml.safe_load(f)
# 获取字典键值
name = config['metadata']['name']
image = config['spec']['template']['spec']['containers'][0]['image']
# 列表中直接把编写好的字符串放进去
report.append(f"Service name: {name}, image version: {image}")
return report
if __name__ == '__main__':
os.chdir('Python/python-manuscripts')
yaml_dir = 'configs'
report = get_config(yaml_dir)
for line in report:
print(line)