zoukankan      html  css  js  c++  java
  • python 解析 yaml文件

    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}


  • 相关阅读:
    沙县小吃炖罐做法 114沙县小吃配料网
    党参_百度百科
    EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~续~添加事务机制
    EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离
    知方可补不足~Sqlserver中的几把锁和.net中的事务级别
    面对大数据,我们应该干的事~大话开篇
    EF架构~在T4模版中自定义属性的getter和setter
    SurfaceView的一个小应用:开发示波器
    Jetty入门
    ios ARC
  • 原文地址:https://www.cnblogs.com/mhc-fly/p/7083574.html
Copyright © 2011-2022 走看看