大家好,我是Linux运维工程师 Linke,技术过硬,从不挖坑。
先安装 yaml 模块
pip3 install pyyaml
编辑一个yaml文件
windows:
-
"192.168.50.162": "i-2ze4ztxyxi58e631qi2k"
"192.168.50.50": "i-2zed05vp346gt5bk6wjz"
"192.168.50.45": "i-2zed05vp346gt5bk6wjv"
linux:
-
"192.168.50.163": "i-2ze5lrkjx05tzaoobrj9"
"192.168.50.130": "i-2zed7z6p817o712i7sfd"
"192.168.50.148": "i-2zed7z6p817o712i7sfb"
加载yaml文件成为字典
import os
import yaml
def Getconfigdic():
proPath = os.path.dirname(os.path.realpath(__file__))
yamlPath = os.path.join(proPath, "test.yaml")
f = open(yamlPath, "r", encoding="utf-8")
sdata = yaml.full_load(f)
f.close()
return sdata
print(Getconfigdic())
输出内容为一个大字典:
{'windows': [{'192.168.50.162': 'i-2ze4ztxyxi58e631qi2k', '192.168.50.50': 'i-2zed05vp346gt5bk6wjz', '192.168.50.45': 'i-2zed05vp346gt5bk6wjv'}],'linux':[{'192.168.50.163': 'i-2ze5lrkjx05tzaoobrj9', '192.168.50.130': 'i-2zed7z6p817o712i7sfd', '192.168.50.148': 'i-2zed7z6p817o712i7sfb'}]}