【问题标题】:Got "Unable to parse YAML error" when converting JSON to yml将 JSON 转换为 yaml 时出现“无法解析 YAML 错误”
【发布时间】:2018-06-23 22:05:20
【问题描述】:

我使用JSON converter to YML 格式。它工作正常。

但是Validate YAML检查的YAML结果无效

错误之一:

Error: Unable to parse.
Line: 36    - Ref: ALIYUN::StackId

请帮我从 JSON 转换为有效的 YML。谢谢。

我的 JSON 文件:

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Resources": {
    "k8s_node_cloudinit_wait_cond": {
      "Type": "ALIYUN::ROS::WaitCondition",
      "Properties": {
        "Count": "1",
        "Handle": {
          "Ref": "k8s_node_cloudinit_wait_cond_handle"
        },
        "Timeout": 1000
      }
    },
    "k8s_sg": {
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "SecurityGroupIngress": [
          {
            "Priority": 1,
            "IpProtocol": "all",
            "NicType": "internet",
            "SourceCidrIp": "0.0.0.0/0",
            "PortRange": "-1/-1"
          }
        ],
        "VpcId": {
          "Ref": "k8s_vpc"
        },
        "SecurityGroupEgress": [
          {
            "Priority": 1,
            "IpProtocol": "all",
            "DestCidrIp": "0.0.0.0/0",
            "NicType": "internet",
            "PortRange": "-1/-1"
          }
        ],
        "SecurityGroupName": "k8s_sg"
      }
    },
    "SubAccount": {
      "Type": "ALIYUN::RAM::User",
      "Properties": {
        "UserName": {
          "Fn::Join": [
            "",
            [
              "SubAccount",
              {
                "Ref": "ALIYUN::StackId"
              }
            ]
          ]
        }
      }
    },
    "k8s_node_cloudinit_wait_cond_handle": {
      "Type": "ALIYUN::ROS::WaitConditionHandle"
    },
    "ECSManagePolicy": {
      "Type": "ALIYUN::RAM::ManagedPolicy",
      "Properties": {
        "PolicyName": {
          "Fn::Join": [
            "",
            [
              "ECSManagePolicy",
              {
                "Ref": "ALIYUN::StackId"
              }
            ]
          ]
        },
        "PolicyDocument": {
          "Version": "1",
          "Statement": [
            {
              "Action": [
                "*"
              ],
              "Resource": [
                "*"
              ],
              "Effect": "Allow"
            },
            {
              "Action": [
                "vpc:DescribeVpcs",
                "vpc:DescribeVSwitches"
              ],
              "Resource": [
                "*"
              ],
              "Effect": "Allow"
            }
          ]
        },
        "Users": [
          {
            "Fn::GetAtt": [
              "SubAccount",
              "UserName"
            ]
          }
        ]
      }
    },
    "AccessKey": {
      "Type": "ALIYUN::RAM::AccessKey",
      "Properties": {
        "UserName": {
          "Fn::GetAtt": [
            "SubAccount",
            "UserName"
          ]
        }
      }
    },
    "k8s_master": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "UserData": {
          "Fn::Replace": [
            {
              "ros-notify": {
                "Fn::GetAtt": [
                  "k8s_master_cloudinit_wait_cond_handle",
                  "CurlCli"
                ]
              }
            },
            {
              "Fn::Join": [
                "",
                [
                  "#!/bin/sh\n",
                  "until yum -y install expect || ! cat /etc/os-release|grep centos; do echo 'wait yum ready ...'; sleep 1; done\n",
                  "curl -sSL http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.7.2.sh | ",
                  "bash -s nice --node-type master --oss-region ap-southeast-1 --key-id ",
                  {
                    "Fn::GetAtt": [
                      "AccessKey",
                      "AccessKeyId"
                    ]
                  },
                  " --key-secret ",
                  {
                    "Fn::GetAtt": [
                      "AccessKey",
                      "AccessKeySecret"
                    ]
                  },
                  "\n",
                  "TOKEN=`kubeadm token list | grep token |awk '{print $1}'`\n",
                  "ros-notify -d \"{\\\"data\\\": \\\"$TOKEN\\\"}\" \n"
                ]
              ]
            }
          ]
        },
        "SystemDiskCategory": "cloud_ssd",
        "VpcId": {
          "Ref": "k8s_vpc"
        },
        "SecurityGroupId": {
          "Ref": "k8s_sg"
        },
        "ImageId": "centos_7",
        "VSwitchId": {
          "Ref": "k8s_vswitch"
        },
        "IoOptimized": "optimized",
        "Password": "abcABC@147896325",
        "InstanceType": "ecs.n1.medium",
        "PrivateIpAddress": "192.168.0.1"
      }
    },
    "k8s_vswitch": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "k8s_vpc"
        },
        "CidrBlock": "192.168.0.0/16",
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        }
      }
    },
    "k8s_master_cloudinit_wait_cond": {
      "Type": "ALIYUN::ROS::WaitCondition",
      "Properties": {
        "Count": 1,
        "Handle": {
          "Ref": "k8s_master_cloudinit_wait_cond_handle"
        },
        "Timeout": 900
      }
    },
    "k8s_nodes": {
      "Type": "ALIYUN::ECS::InstanceGroup",
      "Properties": {
        "UserData": {
          "Fn::Replace": [
            {
              "ros-notify": {
                "Fn::GetAtt": [
                  "k8s_node_cloudinit_wait_cond_handle",
                  "CurlCli"
                ]
              }
            },
            {
              "Fn::Join": [
                "",
                [
                  "#!/bin/sh\n",
                  "until yum -y install expect || ! cat /etc/os-release|grep centos; do echo 'wait yum ready ...'; sleep 1; done\n",
                  "export TOKEN=`echo '",
                  {
                    "Fn::GetAtt": [
                      "k8s_master_cloudinit_wait_cond",
                      "Data"
                    ]
                  },
                  "' | awk -F '\"' '{print $4}'`\n",
                  "curl -sSL http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.7.2.sh | ",
                  "bash -s nice --node-type node --oss-region ap-southeast-1 --key-id ",
                  {
                    "Fn::GetAtt": [
                      "AccessKey",
                      "AccessKeyId"
                    ]
                  },
                  " --key-secret ",
                  {
                    "Fn::GetAtt": [
                      "AccessKey",
                      "AccessKeySecret"
                    ]
                  },
                  " --token $TOKEN ",
                  " --endpoint 192.168.0.1:6443\n",
                  "num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')\n",
                  "seconds=$(($num%30))\n",
                  "sleep $seconds\n",
                  "ros-notify -d \"{\\\"data\\\": \\\"$TOKEN\\\"}\" \n"
                ]
              ]
            }
          ]
        },
        "SystemDiskCategory": "cloud_ssd",
        "VpcId": {
          "Ref": "k8s_vpc"
        },
        "MinAmount": "1",
        "SecurityGroupId": {
          "Ref": "k8s_sg"
        },
        "ImageId": "centos_7",
        "VSwitchId": {
          "Ref": "k8s_vswitch"
        },
        "IoOptimized": "optimized",
        "Password": "abcABC@147896325",
        "InstanceType": "ecs.n1.medium",
        "MaxAmount": "1"
      }
    },
    "k8s_master_cloudinit_wait_cond_handle": {
      "Type": "ALIYUN::ROS::WaitConditionHandle"
    },
    "k8s_vpc": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "VpcName": "k8s_vpc",
        "CidrBlock": "192.168.0.0/16"
      }
    }
  },
  "Outputs": {
    "k8s_master_token": {
      "Value": {
        "Fn::GetAtt": [
          "k8s_master_cloudinit_wait_cond",
          "Data"
        ]
      }
    },
    "DashboardURL": {
      "Description": "Kubernetes dashboard URL.",
      "Value": {
        "Fn::Join": [
          "",
          [
            "http://",
            {
              "Fn::GetAtt": [
                "k8s_master",
                "PublicIp"
              ]
            },
            ":80"
          ]
        ]
      }
    },
    "k8s_node_token": {
      "Value": {
        "Fn::GetAtt": [
          "k8s_node_cloudinit_wait_cond",
          "Data"
        ]
      }
    }
  }
}

【问题讨论】:

    标签: json yaml converter pyyaml


    【解决方案1】:

    你不应该使用那个验证器,它声称是最好的,但它实际上并没有确认 YAML 1.2 和 YAML 1.1。

    它抱怨的 YAML 行 (36) 是:

             - Ref: ALIYUN::StackId
    

    并且损坏的验证器无法处理的事情之一是不带引号的标量字符串中的冒号(即ALIYUN::StackId)。由于冒号后面没有空格或(在 YAML 1.2 中)前后有双引号,因此 从不 表示键值对,因此不会与此有效的 YAML 混淆。

    只需尝试在 PyYAML 中加载转换的输出,或将其粘贴到this 站点(基于 PyYAML),您会发现没有问题。虽然 PyYAML 也不完美(并且仅支持 YAML 1.1 的大部分内容),但它比您一直使用的网站要好得多。

    【讨论】:

    • 感谢您的回答。我使用 YAML 模板与阿里云 ROS 一起工作,但没有通过验证。所以,我必须验证模板。对我有更多建议吗?
    【解决方案2】:

    我使用json2yaml package将JSON转换为YML,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2021-02-04
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      相关资源
      最近更新 更多