Pyyaml:修改aws-auth-cm.yaml,保留多行字符串

Pyyaml: Modify aws-auth-cm.yaml, preserve the multi line string

我正在尝试加载 python 中的 YAML 文件,对其进行修改并将其转储回来。 YAML 如下所示:

data:
  mapRoles: |
    - username: system:node:{{EC2PrivateDNSName}}
      groups:
      - system:bootstrappers
      - system:nodes
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system

我想修改它以便输出文件在 mapRoles 中包含一个新行 rolearn: awsarn:

data:
  mapRoles: |
    - username: system:node:{{EC2PrivateDNSName}}
      groups:
        - system:bootstrappers
        - system:nodes
      rolearn: awsarn
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system

但我得到一个输出,其中 mapRoles 值像字符串一样被引用并包含文字 \n:

apiVersion: v1
data:
  mapRoles: "- username: system:node:{{EC2PrivateDNSName}}\n  groups:\n    - system:bootstrappers\n\
    \    - system:nodes\n  rolearn: arnaws"
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system

我使用的代码:

with open ('/tmp/aws-auth-cm.yaml') as f:
  content = yaml.safe_load(f)
  content['data']['mapRoles'] = content['data']['mapRoles'] + '  rolearn: awsarn' 
with open("/tmp/aws-auth-cm.yaml", "w") as f:
  yaml.safe_dump(content, f, default_flow_style=False)

我也试过使用 yaml.safe_dump(content, f, default_flow_style=False, default_style='|') 然后所有的值都有 |- 并且键用双引号引起来:

"apiVersion": |-
  v1
"data":
  "mapRoles": |-
    - username: system:node:{{EC2PrivateDNSName}}
      groups:
        - system:bootstrappers
        - system:nodes
      rolearn: arnaws
"kind": |-
  ConfigMap
"metadata":
  "name": |

有什么方法可以应用样式='|'仅针对字符串并确保不引用键?

您可以尝试使用 PyYAML 执行此操作,但您需要加载块样式文字标量 (这就是所谓的 | 指示的 multi-line 构造)在 字符串的子类,确保你可以修改它,然后在转储时使用一个特殊的代表 对于再次转储为文字标量的子类。

完成此操作的简单方法是从 PyYAML 升级到 ruamel.yaml(免责声明:我是那个包的作者),它不仅保留 文字块它还支持更多 up-to-date YAML 1.2 规范(2009 年发布), 保留注释和标签、整数和浮点数格式,以及(可选)多余的引号:

import sys
import ruamel.yaml


yaml = ruamel.yaml.YAML()
with open('aws-auth-cm.yaml') as f:
    content = yaml.load(f)
content['data']['mapRoles'] += '  rolearn: awsarn\n'
with open('aws-auth-cm.yaml', 'w') as f:
    yaml.dump(content, f)

给出:

data:
  mapRoles: |
    - username: system:node:{{EC2PrivateDNSName}}
      groups:
      - system:bootstrappers
      - system:nodes
      rolearn: awsarn
kind: ConfigMap
metadata:
  name: aws-auth
  namespace: kube-system

请注意,我使用 += 来更改 "string" 从文字标量加载。而且我添加了一个换行符 添加字符串的末尾,否则你的文字标量将是 与 |- 一起引入,- 阻塞 operator 表示 剥离.