python之模块分类(三)
Python对yaml格式配置文件的处理:
Python也可以很容易的处理ymal文档格式,只不过需要安装一个pyyaml模块。
http://pyyaml.org/wiki/PyYAMLDocumentation
一、加载yaml
警告:调用 yaml.load 处理从不可信任的源接收的数据可能存在风险。yaml.load 与 pickle.load 的功能一样强大,可以调用所有Python函数。
(1)yaml.load 函数的作用是用来将YAML文档转化成Python对象。
#Author:Anliu import yaml doc = """ - host - name - handle """ print(yaml.load(doc))
执行结果:
['host', 'name', 'handle'] K:/pyprogram/modler_test/pyyaml_test/test2.py:8: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. print(yaml.load(doc))
以上告警原因是: yaml 5.1版本后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版本之后就修改了需要指定Loader,通过默认加载器(FullLoader)禁止执行任意函数。
解决方法:
#Author:Anliu import yaml doc = """ - host - name - handle """ print(yaml.load(doc,Loader=yaml.FullLoader))
Loader的几种加载方式
BaseLoader--仅加载最基本的YAML
SafeLoader--安全地加载YAML语言的子集。建议用于加载不受信任的输入。
FullLoader--加载完整的YAML语言。避免任意代码执行。这是当前(PyYAML 5.1)默认加载器调用
yaml.load(input)(发出警告后)。
UnsafeLoader--(也称为Loader向后兼容性)原始的Loader代码,可以通过不受信任的数据输入轻松利用。
结果为:
['host', 'name', 'handle']yaml.load 函数可以接受一个表示YAML文档的字节字符串、Unicode字符串、打开的二进制文件对象或者打开的文本文件对象作为参数。若参数为字节字符串或文件,那么它们必须使用 utf-8 、utf-16 或者 utf-16-le 编码。yaml.load 会检查字节字符串或者文件对象的BOM(byte order mark)并依此来确定它们的编码格式。如果没有发现 BOM ,那么会假定他们使用 utf-8 格式的编码。
将一下ansible的http安装文件命名为test.yaml:
#任何playbook文件(其实就是yaml文件)都要以这个开头
---
#选择的主机组
- hosts: webservers
#这个是变量
vars:
http_port: 80
max_clients: 200
tasks:
#利用yum模块来操作
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
#触发重启服务器(只要上面的httpd.conf文件变化了,就触发handlers中的restart apache)
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
#这里的restart apache 和上面的触发是配对的。这就是handlers的作用。相当于ta
handlers:
- name: restart apache
service: name=httpd state=restarted
#Author:Anliu import yaml with open("test.yaml","r",encoding="utf-8") as f: a = yaml.load(f,Loader=yaml.FullLoader) print(a)
返回结果:yaml.load 方法的返回值为一个Python对象,
[{'hosts': 'webservers', 'vars': {'http_port': 80, 'max_clients': 200}, 'tasks': [{'name': 'ensure apache is at the latest version', 'yum': 'pkg=httpd state=latest'}, {'name': 'write the apache config file', 'template': 'src=/srv/httpd.j2 dest=/etc/httpd.conf', 'notify': ['restart apache']}, {'name': 'ensure apache is running', 'service': 'name=httpd state=started'}], 'handlers': [{'name': 'restart apache', 'service': 'name=httpd state=restarted'}]}]
yaml.load_all 函数将它们全部反序列化,得到的是一个包含所有反序列化后的YAML文档的生成器对象:#Author:Anliu import yaml with open("test.yaml","r",encoding="utf-8") as f: #a = yaml.load(f,Loader=yaml.FullLoader) a = yaml.load_all(f,Loader=yaml.FullLoader) print(a) for i in a: print(i[0].get("hosts"))
二、转存yaml
yaml.dump 函数接受一个Python对象并生成一个YAML文档。
#Author:Anliu import yaml dict = { "name":"anliu", "age":18, "ip":"192.168.42.111", "host":"DNS" } with open("test1.yaml","w",encoding="utf-8") as f: yaml.dump(dict,f)
转存结果:


浙公网安备 33010602011771号