用Python生成Ansible YAML文件

用Python生成Ansible YAML文件,python,ansible,yaml,Python,Ansible,Yaml,如何使用Python3生成Ansible YAML playbook,它看起来是这样的: email.yml --- - name: Send a success email hosts: localhost tasks: - name: send email mail: host: "smtp.email.com" port: 587 sender: "Notification noti

如何使用Python3生成Ansible YAML playbook,它看起来是这样的:

email.yml

---
  - name: Send a success email
    hosts: localhost
    tasks:
      - name: send email
        mail:
          host: "smtp.email.com"
          port: 587
          sender: "Notification notification@email.com"
          username: "notification@email.com"
          password: "password"
          to: "Some Email <some.email@university.edu>"
          cc: "Some Email <some.email@university.edu>"
          subject: "Backup complete - test"
          subtype: html
          body: "<h1>The backup of</h1><br> .. is complete."
          secure: starttls
---
-姓名:发送成功电子邮件
主机:本地主机
任务:
-姓名:发送电子邮件
邮寄:
主机:“smtp.email.com”
港口:587
发件人:“通知notification@email.com"
用户名:“notification@email.com"
密码:“密码”
致:“一些电子邮件”
抄送:“一些电子邮件”
主题:“备份完成-测试”
子类型:html
正文:“备份
。已完成。” 安全:starttls
我已尝试用Python解析YAML文件,并将其放入使用以下相同格式的脚本中:

#!/usr/bin/python3
import yaml

d=[None, [{'name': 'Send a success email', 
        'hosts': 'localhost', 
        'tasks': [{'name': 'send email', 
        'mail': {'host': 'smtp.email.com', 
        'port': 587, 
        'sender': 'Notification notification@email.com', 
        'username': 'notification@email.com', 
        'password': 'password', 
        'to': 'Some Email <some.email@university.edu>', 
        'cc': 'Some Email <some.email@university.edu>', 
        'subject': 'Backup complete - test', 
        'subtype': 'html', 
        'body': '<h1>The backup of</h1><br> .. is complete.',
        'secure': 'starttls'}}]}]]

f=open('output.yaml','w')
f.write(yaml.dump(d))
f.close
#/usr/bin/python3
进口yaml
d=[None,[{'name':'sendasuccessemail',
“主机”:“本地主机”,
'tasks':[{'name':'send email',
'mail':{'host':'smtp.email.com',
“端口”:587,
“发件人”:“通知”notification@email.com', 
“用户名”:”notification@email.com', 
“密码”:“密码”,
'收件人':'一些电子邮件',
“抄送”:“一些电子邮件”,
“主题”:“备份完成-测试”,
“子类型”:“html”,
“正文”:“备份
。已完成。”, 'secure':'starttls'}}]}] f=打开('output.yaml','w') f、 写入(yaml.dump(d)) f、 接近
output.yaml的结果:

- null
- - hosts: localhost
    name: Send a success email
    tasks:
    - mail:
        body: <h1>The backup of</h1><br> .. is complete.
        cc: Some Email <some.email@university.edu>
        host: smtp.email.com
        password: password
        port: 587
        secure: starttls
        sender: Notification notification@email.com
        subject: Backup complete - test
        subtype: html
        to: Some Email <some.email@university.edu>
        username: notification@email.com
      name: send email
-null
--主机:localhost
姓名:发送成功电子邮件
任务:
-邮寄:
正文:备份
。。完成了。 抄送:一些电子邮件 主机:smtp.email.com 密码:密码 港口:587 安全:starttls 发件人:通知notification@email.com 主题:备份完成-测试 子类型:html 收件人:一些电子邮件 用户名:notification@email.com 姓名:发送电子邮件
这里有几个问题:没有双引号,行不整齐

解决方案: 我能够解决我在ruamel.yaml和往返过程中遇到的问题,以保留我希望生成的yaml文件非常需要的双引号

import sys
import ruamel.yaml
from ruamel.yaml import YAML

inp = """\
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
"""

yaml = YAML()
code = ruamel.yaml.round_trip_load(inp, preserve_quotes=True)

ruamel.yaml.round_trip_dump(code, sys.stdout)
import sys
import ruamel.yaml
from ruamel.yaml import YAML

inp = """\
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
"""

yaml = YAML()
code = ruamel.yaml.round_trip_load(inp, preserve_quotes=True)

ruamel.yaml.round_trip_dump(code, sys.stdout)
导入系统 进口ruamel.yaml 从ruamel.yaml导入yaml inp=”“”\ -名称:“发送成功的电子邮件” 主机:本地主机 任务: -名称:“发送电子邮件” 邮寄: 主机:“smtp.email.com” 港口:587 发件人:“通知notification@email.com" 用户名:“notification@email.com" 密码:“密码” 致:“一些电子邮件” 抄送:“一些电子邮件” 主题:“备份完成-测试” 子类型:html 正文:“备份
。。“完成了” 安全:starttls """ yaml=yaml() 代码=ruamel.yaml.round\u trip\u load(inp,preserve\u quotes=True) ruamel.yaml.往返行程转储(代码,系统标准) 给出:

- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
-name:“发送成功的电子邮件”
主机:本地主机
任务:
-名称:“发送电子邮件”
邮寄:
主机:“smtp.email.com”
港口:587
发件人:“通知notification@email.com"
用户名:“notification@email.com"
密码:“密码”
致:“一些电子邮件”
抄送:“一些电子邮件”
主题:“备份完成-测试”
子类型:html
正文:“备份
。已完成” 安全:starttls
我能够解决我在ruamel.yaml和往返过程中遇到的问题,以保留我希望生成的yaml文件非常需要的双引号

import sys
import ruamel.yaml
from ruamel.yaml import YAML

inp = """\
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
"""

yaml = YAML()
code = ruamel.yaml.round_trip_load(inp, preserve_quotes=True)

ruamel.yaml.round_trip_dump(code, sys.stdout)
import sys
import ruamel.yaml
from ruamel.yaml import YAML

inp = """\
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
"""

yaml = YAML()
code = ruamel.yaml.round_trip_load(inp, preserve_quotes=True)

ruamel.yaml.round_trip_dump(code, sys.stdout)
导入系统 进口ruamel.yaml 从ruamel.yaml导入yaml inp=”“”\ -名称:“发送成功的电子邮件” 主机:本地主机 任务: -名称:“发送电子邮件” 邮寄: 主机:“smtp.email.com” 港口:587 发件人:“通知notification@email.com" 用户名:“notification@email.com" 密码:“密码” 致:“一些电子邮件” 抄送:“一些电子邮件” 主题:“备份完成-测试” 子类型:html 正文:“备份
。。“完成了” 安全:starttls """ yaml=yaml() 代码=ruamel.yaml.round\u trip\u load(inp,preserve\u quotes=True) ruamel.yaml.往返行程转储(代码,系统标准) 给出:

- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
- name: "Send a successful email"
  hosts: localhost
  tasks:
  - name: "send email"
    mail:
      hosts: "smtp.email.com"
      port: 587
      sender: "Notification notification@email.com"
      username: "notification@email.com"
      password: "password"
      to: "Some Email <some.email@university.edu>"
      cc: "Some Email <some.email@university.edu>"
      subject: "Backup complete - test"
      subtype: html
      body: "<h1> The backup of </h1><br> .. is complete"
      secure: starttls
-name:“发送成功的电子邮件”
主机:本地主机
任务:
-名称:“发送电子邮件”
邮寄:
主机:“smtp.email.com”
港口:587
发件人:“通知notification@email.com"
用户名:“notification@email.com"
密码:“密码”
致:“一些电子邮件”
抄送:“一些电子邮件”
主题:“备份完成-测试”
子类型:html
正文:“备份
。已完成” 安全:starttls
请查看此页面。它应该能回答您的问题您到目前为止尝试了什么?更新了原始帖子主要问题主要问题:#1我无法让脚本按照原始yaml文件中的顺序写行#2我无法在yaml文件中添加双引号,方法与原始文件中相同