import yaml

with open("./test.yaml") as f:
x = yaml.load(f) print(x) [{'tasks': [{'yum': {'state': 'latest', 'name': 'httpd'}, 'name': 'ensure apache is at the latest version'}, {'name': 'write the apache config file', 'template': {'dest': '/etc/httpd.conf', 'src': '/srv/httpd.j2'}, 'notify': ['restart apache']}, {'name': 'ensure apache is running', 'service': {'state': 'started', 'name': 'httpd'}}], 'hosts': 'webservers', 'remote_user': 'root', 'vars': {'http_port': 80, 'max_clients': 200}, 'handlers': [{'name': 'restart apache', 'service': {'state': 'restarted', 'name': 'httpd'}}]}] test.yaml
---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: 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 -----------------------------------------------------------------
import yaml

d = [{'tasks': [{'yum': {'state': 'latest', 'name': 'httpd'}, 'name': 'ensure apache is at the latest version'}, {'name': 'write the apache config file', 'template': {'dest': '/etc/httpd.conf', 'src': '/srv/httpd.j2'}, 'notify': ['restart apache']}, {'name': 'ensure apache is running', 'service': {'state': 'started', 'name': 'httpd'}}], 'hosts': 'webservers', 'remote_user': 'root', 'vars': {'http_port': 80, 'max_clients': 200}, 'handlers': [{'name': 'restart apache', 'service': {'state': 'restarted', 'name': 'httpd'}}]}]

with open("./test.yaml","w") as f:
yaml.dump(d,f) test.yaml
- handlers:
- name: restart apache
service: {name: httpd, state: restarted}
hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: {name: httpd, state: latest}
- name: write the apache config file
notify: [restart apache]
template: {dest: /etc/httpd.conf, src: /srv/httpd.j2}
- name: ensure apache is running
service: {name: httpd, state: started}
vars: {http_port: 80, max_clients: 200}
												

python 解析 yaml文件的更多相关文章

  1. python解析yaml文件

    YAML语法规则: http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/ 下载PyYAML: http://www.yaml.org/ 解压 ...

  2. python操作YAML文件之pyyaml库

    1. YAML简介 YAML是一种被认为可以超越XML.JSON的配置文件,最早接触是Spring Boot,木有想到python也是支持的,遂研究一下. python解析YAML库叫做pyyaml, ...

  3. Python读取Yaml文件

    近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...

  4. 使用python读取yaml文件

    在做APP测试时,通常需要把参数存到一个字典变量中,这时可以将参数写入yaml文件中,再读取出来. 新建yaml文件(android_caps.yaml),文件内容为: platformName: A ...

  5. Python解析Wav文件并绘制波形的方法

    资源下载 #本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在 ...

  6. Python解析excel文件并存入sqlite数据库

    最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...

  7. python解析ini文件

    python解析ini文件 使用configparser - Configuration file parser sections() add_section(section) has_section ...

  8. python读取yaml文件,在unittest中使用

    python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(u ...

  9. python基础——python解析yaml类型文件

    一.yaml介绍 yaml全称Yet Another Markup Language(另一种标记语言).采用yaml作为配置文件,文件看起来直观.简洁.方便理解.yaml文件可以解析字典.列表和一些基 ...

随机推荐

  1. CentOS7 安装mysql(YUM方式)

    1.下载mysql源安装包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm   2 ...

  2. War包反编译过程

    War包反编译过程 很多人可以将项目编译为war发布,可是有时候得到war确看不到源码.今天分享下war反编译的过程: 1.首先下载一个小工具,在http://jd.benow.ca/官网下载jd-g ...

  3. 多线程实现ping扫描

    代码: # coding=utf-8 import subprocess from Queue import Queue import threading class Pinger(object): ...

  4. ni_set()函数的使用 以及 post_max_size,upload_max_filesize的修改方法

    Apache服务器处理: ini_set('display_errors', 'Off');ini_set('memory_limit', -1); //-1 / 10240Mini_set(&quo ...

  5. C++多线程同步之事件(Event)

    原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...

  6. Spark SQL 编程

    Spark SQL的依赖 Spark SQL的入口:SQLContext 官方网站参考 https://spark.apache.org/docs/1.6.2/sql-programming-guid ...

  7. 谷歌推出新型强化学习框架Dopamine

    今日,谷歌发布博客介绍其最新推出的强化学习新框架 Dopamine,该框架基于 TensorFlow,可提供灵活性.稳定性.复现性,以及快速的基准测试. GitHub repo:https://git ...

  8. sql server查看表占用索引空间(小技巧)

    选择表右键—属性—存储—索引空间

  9. mac mysql中文乱码问题

    God,今天看了好多资料,除了让我命令更熟练以外浪费了好多时间. 遇到的问题:写入数据库有中文的时候,显示??? 最后解决办法: 1.打开终端,输入: mysql -u root -p,然后输入mys ...

  10. tornado-模板,转义,上传静态文件

    class MainHandler(tornado.web.RequestHandler): def get(self): self.render("ces.html") def ...