得到"无法解析YAML错误"将JSON转换为yml时

时间:2018-01-15 09:35:31

标签: json yaml converter pyyaml

我使用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"
        ]
      }
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

它抱怨的YAML线(36)是:

         - Ref: ALIYUN::StackId

并且验证者无法处理的事情之一是未加引号的标量字符串中的冒号(即ALIYUN::StackId)。由于没有空格的冒号或(在YAML 1.2中)之前和之后是双引号,从不表示键值对,因此对此有效YAML不会产生混淆。< / p>

只是尝试在PyYAML中加载转换的输出,或者将其粘贴到this站点(基于PyYAML)上,您将看到没有问题。虽然PyYAML也不是完美的(并且仅支持大多数YAML 1.1),但它比您使用的站点更好的验证。

答案 1 :(得分:0)

我使用json2yaml package将JSON转换为YML,它运行正常。

相关问题