首页 > 解决方案 > 将 YAML 文本的子集复制到单独的文件中,然后在处理时出现语法错误

问题描述

我正在研究 [Jenkins 管道 - YAML - ansible] 组合。在管道中,有一个输入部分,它看起来有点像这样:

student_input = input(
message: 'Input Student details', parameters: [text(defaultValue: 
'''---
student: 
  ID: 100
  grade: 5
  info: 
    email: abc@xyz.com
    home_town: efg
    marks: 
      math: 95
      science: 92
''',
description: '', 
name: 'student'
 )]
)

然后,我使用readYaml 文件将此输入写入变量:“File.yml”实用程序。

在为此的实现中,使用 map.each 循环的嵌套地图来到达信息,然后到达标记部分。

Groovy 映射代码示例:

value.each { k, v ->

           sh """
           echo '$k: $v' >> student.yml
           """
           }

使用它,我试图将info部分的整个value部分写入一个单独的 YAML 文件,稍后将与 ansible 的include_vars模块一起使用。

在运行 groovy 管道时,我在 Ansible 中遇到了以下问题:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "Syntax Error while loading YAML.
The error appears to have been in 'True': line 9, column 26, 
but may\nbe elsewhere in the file depending on the exact syntax problem.(could not open file to display line)"}

当我检查最终的 YAML 文件时,标记部分看起来像这样:

marks: [math:95, science:92]

这些列表元素写入文件的方式(冒号后没有空格)似乎导致错误。

请帮我解决这个问题。我错过了什么,或者做错了什么?

标签: jenkinsgroovyansibleyamljenkins-groovy

解决方案


推荐阅读