如何使用Perl通过YAML :: Tiny或其他方式修改YAML文件值?

时间:2019-08-08 12:59:24

标签: perl parsing yaml

早上,我正在寻找如何在Yaml中修改一些很深的值(即与内存和CPU相关的值)。

 ---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sentry-agent
spec:
  minReadySeconds: 30
  progressDeadlineSeconds: 240
  replicas: 1
  revisionHistoryLimit: 5
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 0%
    type: RollingUpdate
  selector:
    matchLabels:
      app: sentry-agent
  template:
    metadata:
      creationTimestamp: null
    spec:
      serviceAccountName: sentry-agent-usr
      serviceAccount: sentry-agent-usr
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - sentry-agent
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: sentry-agent
        image: <image>
        imagePullPolicy: Always
        envFrom:
          - configMapRef:
              name: sentry-agent
          - secretRef:
              name: sentry-creds
        resources:
          requests:
            memory: "512Mi"
            cpu: "1000m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
             port: 8080
            path: /healthz

早上,我试图弄清楚如何使用perl的YAML :: Tiny模块来修改内存和cpu值。

示例的深度不够。我看到他们有

use YAML::Tiny;

# Open the config
my $yaml = YAML::Tiny->read( 'file.yml' );

# Get a reference to the first document
my $config = $yaml->[0];    

这是很标准的。我尝试过类似的事情:

print "$config->{spec}->{template}->{containers}->{resources}->{memory}\n";

这不起作用。任何专家都知道我的道路出了什么问题吗?

1 个答案:

答案 0 :(得分:2)

YAML的第一行和最后一行缩进了错误。解决此问题后,您可以通过以下方式访问值1Gi

print "$config->{spec}{template}{spec}{containers}[0]{resources}{limits}{memory}\n";

提示:使用转储器模块可视化复杂的嵌套数据。