1. import yaml
  2.  
  3. with open("./test.yaml") as f:
    x = yaml.load(f)
  4.  
  5. print(x)
  6.  
  7. [{'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'}}]}]
  8.  
  9. test.yaml
  1. ---
    - 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
  2.  
  3. -----------------------------------------------------------------
  1. import yaml
  2.  
  3. 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'}}]}]
  4.  
  5. with open("./test.yaml","w") as f:
    yaml.dump(d,f)
  6.  
  7. test.yaml
  1. - 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}
  1.  

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. ubuntu 安装时分辨率太小 导致无法继续安装

    当分辨率是800 *600时,底部的按钮无法显示,不能继续安装. 可以在右上角,点击电源按钮,在系统设置中调整显示的分辨率后,继续安装.

  2. lwip编译选项

    修改默认选项时,不要修改opt.h文件,通过lwipopts.h修改. 和内存管理以及TCP发送性能相关的选项 // 为1时使用系统库malloc/free进行内存管理 #ifndef MEM_LIB ...

  3. LwIP:处理链路状态改变

    [文/告别年代   Email:byeyear@hotmail.com] 重大修订记录 ----------------------------------------- 2016.11.03 感谢@ ...

  4. LwIP raw api下使用tcp keep alive

    // The following code is implemented after tcp_new() or in tcp_connected call back... xxx_connected( ...

  5. react-native在mac上执行gradlew命令报错 ./gradlew: command not found

    这是因为react-native项目是windows上初始化,通过git clone到mac机器上后gradlew这个文件没有可执行权限,如图: 所以只需要给gradlew这个文件增加可执行权限就可以 ...

  6. C++ STL Set 集合

    前言 set是STL中的一种关联容器.集合具有无序性,互异性等特点.熟练使用STL中的set模板类,可以比较简单的解决一些编程问题. 关联容器:元素按照关键字来保存和访问,STL中的map,set就是 ...

  7. Spring+hibernate+mysql事物不回滚的原因以及处理

    最近项目突然出了点问题,然后发现用Service层下面的一个类的一个方法里的事务居然没有回滚.然后自己写了一个测试方法经过了N次测试都是不回滚.以下是测试方法的一部分: @Transactional( ...

  8. 利用x-requested-with判断请求是否是Ajax请求

    在服务器端判断request来自Ajax请求(异步)还是传统请求(同步):         两种请求在请求的Header不同,Ajax 异步请求比传统的同步请求多了一个头参数 1.传统同步请求参数 a ...

  9. SpringMVC中session的使用

    SpringMVC中仍然可以使用传统方式使用session /** * 使用session - 传统方式 */ @RequestMapping("/hello13.action") ...

  10. [转]CSKIN 作者分享的图片处理类

    本代码来自:http://bbs.cskin.net/forum.php?mod=viewthread&tid=113&fromuid=2446 里面没有我想找的任意角度旋转的方法,代 ...