【问题标题】:Generating Ansible YAML file with Python使用 Python 生成 Ansible YAML 文件
【发布时间】:2023-04-05 18:57:02
【问题描述】:

如何使用 Python3 生成 Ansible YAML 剧本,如下所示:

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

我尝试在 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

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

这里有几个问题:没有双引号,行不按顺序。

解决方案:
我能够解决我在使用 ruamel.yaml 和 round_trip 时遇到的问题,以保留我想要生成的 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)

给予:

- 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

【问题讨论】:

  • 看看这个页面。它应该回答你的问题pyyaml.org/wiki/PyYAMLDocumentation
  • 到目前为止你尝试了什么?
  • 更新了原帖
  • 主要问题
  • 主要问题:#1 我无法让脚本按照原始 yaml 文件中的顺序写入行 #2 我无法在 yaml 文件中添加双引号,方式与原始文件相同

标签:
python
ansible
yaml