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}


  • 相关阅读:
    关于ARM CM3的启动文件分析
    curl 基本使用简介
    在windows下获取硬盘序列号(win7 32位,Windows Server 64位测试,希望在其他平台测试,遇到问题的网友留言分享)
    oracle 数据库用户登录相关
    ubuntu 下搭建一个python3的虚拟环境(用于django配合postgresql数据库开发)
    ubuntu下安装postgres
    python 中变量的命名规范
    python 各模块
    python中的model模板中的数据类型
    python编程中在ubuntu中安装虚拟环境及环境配置
  • 原文地址:https://www.cnblogs.com/mhc-fly/p/7083574.html
Copyright © 2011-2022 走看看