error: failed to read input object (not a Template?): error converting YAML to JSON: yaml: mapping values are not allowed in this context

huangapple go评论136阅读模式
英文:

error: failed to read input object (not a Template?): error converting YAML to JSON: yaml: mapping values are not allowed in this context

问题

这个错误是什么意思?是因为缩进问题吗?有没有办法自动对齐yaml文件?

我尝试通过对齐等方式修复它,但没有成功,我的Jenkins构建失败了。

  • name: SPLUNK_LOG_TOKEN
    valueFrom:
    secretKeyRef:
    name: splunk-secret
    key: splunk.token
  • name: SPLUNK_LOG_URL //这一行有错误
    valueFrom:
    configMapKeyRef:
    name: splunk-config
    key: splunk.url
英文:

What does this error mean? Is it due to indentation? Is there any way to auto align yaml files

I tried fixing it by aligning etc but it didn't work and my jenkins build is failing.

- name: SPLUNK_LOG_TOKEN
   valueFrom:
    secretKeyRef:
        name: splunk-secret
        key: splunk.token
- name: SPLUNK_LOG_URL             //error in this line
   valueFrom:
    configMapKeyRef:
       name: splunk-config
       key: splunk.url

答案1

得分: 3

以下是无效的YAML(在两个项目中均如此):

- name: SPLUNK_LOG_TOKEN
   valueFrom:

这是因为valueFrom的缩进比name更多。因此,YAML认为valueFrom是标量SPLUNK_LOG_TOKEN的继续部分。然而,随后有一个:,它结束了隐式映射键。而在多行标量上下文中,隐式映射键是被禁止的,因此会出错。

是否有办法自动对齐YAML文件?

YAML的缩进是语义的一部分(就像在Python中一样)。您基本上在问是否可以猜测糟糕缩进文件的预期语义。虽然从理论上讲这并非不可能(我的意思是,您可以在StackOverflow上的YAML语法问题上训练一个神经网络;我已经看过这个特定问题无数次了),但我认为目前没有现成可用的解决方案。

您问题的可能解决方案是:

- name: SPLUNK_LOG_TOKEN
 valueFrom:

不过,我无法百分之百确定;也许您更希望像这样做:

- name:
    id: SPLUNK_LOG_TOKEN
    valueFrom:

如果valueFrom应该是name的子项。

英文:

The following is invalid YAML (in both items):

- name: SPLUNK_LOG_TOKEN
   valueFrom:

This is because valueFrom is more indented than name. Thus YAML thinks that valueFrom is a continuation of the scalar SPLUNK_LOG_TOKEN. However, a : follows which ends an implicit mapping key. And implicit mapping keys are forbidden in a multi-line scalar context, hence the error.

> Is there any way to auto align yaml files

Indentation in YAML is part of the semantic (like in Python). You are basically asking whether you can guess the intended semantic for badly indented files. While this is theoretically not impossible (I mean, you could train a neural network on YAML syntax questions on StackOverflow; I have seen this particular problem countless times), I don't think a readily usable solution exists.

The probable solution for your problem is

- name: SPLUNK_LOG_TOKEN
 valueFrom:

however, I can't be 100% sure; perhaps you rather want to do something like

- name:
    id: SPLUNK_LOG_TOKEN
    valueFrom:

if valueFrom should be a child of name.

答案2

得分: 2

以下是已翻译的部分:

高亮显示的部分是无效的 YAML 模式。

- name: SPLUNK_LOG_TOKEN 
  valueFrom:
    secretKeyRef:
      name: splunk-secret
      key: splunk.token
- name: SPLUNK_LOG_URL 
  valueFrom:
    configMapKeyRef:
      name: splunk-config
      key: splunk.url

如果你喜欢 JSON,你可以尝试这个在线转换器 https://www.json2yaml.com/convert-yaml-to-json

当我刚开始使用 YAML 时,我会写小块的 JSON 数据,然后使用 https://www.json2yaml.com/convert-yaml-to-json 进行转换成 YAML 格式。还有其他在线工具可用,但从个人经验来看,我更喜欢这个。

英文:

The highlighted one is the invalid yaml pattern's.
- name: SPLUNK_LOG_TOKEN - name: SPLUNK_LOG_URL.

It should be something like this

- name: 
   name: SPLUNK_LOG_TOKEN
   valueFrom:
    secretKeyRef:
        name: splunk-secret
        key: splunk.token
- name:             
   name: SPLUNK_LOG_URL 
   valueFrom:
    configMapKeyRef:
       name: splunk-config
       key: splunk.url

If you are JSON lover like me, then you can try this online converter https://www.json2yaml.com/convert-yaml-to-json

When I was new to yaml, I used to write smaller chunks of JSON data and get it converted using the online https://www.json2yaml.com/convert-yaml-to-json into YAML format.
There are also other tools available online, but from personal experience I preferred this.

huangapple
  • 本文由 发表于 2020年8月11日 22:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63360606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定