在这种情况下,不允许使用YAML映射值


70

我正在尝试以以下格式配置YAML文件:

jobs:
 - name: A
   - schedule: "0 0/5 * 1/1 * ? *"
   - type: mongodb.cluster
    - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120
 - name: B
   - schedule: "0 0/5 * 1/1 * ? *"
   - type: mongodb.cluster
    - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120

我的想法是,我可以读取job元素内的内容,并具有可以解析的一系列不同的job配置。

但是,yamllint.com告诉我,由于该行在mapping values are not allowed in this context at line 2哪里,因此这是非法的YAML 。line 2jobs:

我究竟做错了什么?

Answers:


93

这是有效的YAML:

jobs:
 - name: A
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120
 - name: B
   schedule: "0 0/5 * 1/1 * ? *"
   type: mongodb.cluster
   config:
     host: mongodb://localhost:27017/admin?replicaSet=rs
     minSecondaries: 2
     minOplogHours: 100
     maxSecondaryDelay: 120

请注意,每个“-”都会在序列中开始新的元素。同样,映射中键的缩进应该完全相同


-3

序列的元素需要在同一级别缩进。假设您想要两个作业(A和B),每个作业都有键值对的有序列表,则应使用:

jobs:
 - - name: A
   - schedule: "0 0/5 * 1/1 * ? *"
   - - type: mongodb.cluster
     - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120
 - - name: B
   - schedule: "0 0/5 * 1/1 * ? *"
   - - type: mongodb.cluster
     - config:
       - host: mongodb://localhost:27017/admin?replicaSet=rs
       - minSecondaries: 2
       - minOplogHours: 100
       - maxSecondaryDelay: 120

也可以像@Tsyvarrev一样将(单项)映射的序列转换为映射,但是会使您失去顺序。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.