创建CentOS系统时,通常需要手动配置yum源(软件集合地)下载安装常用软件,不同版本的CentOS系统需要配置不同的yum源。资源编排服务ROS(Resource Orchestration Service)支持通过创建资源栈的方式使用Mappings自动化配置不同版本CentOS系统的yum源。

步骤一:编辑模板

以下模板中定义了mappings:{...}对象内容,记录多个CentOS系统版本对应的阿里yum源地址。在创建ECS进行系统初始化操作时,Fn::FindInMap函数会根据所选择镜像参数(InstanceImageId)的版本匹配对应的yum源,然后完成yum源的替换。

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "Creates ECS Centos system instance, and set yum repo.",
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "VSwitchZoneId",
            "InstanceImageId",
            "InstanceType",
            "SystemDiskSize",
            "InstancePublicIP",
            "InstancePassword",
            "SystemDiskCategory"
          ],
          "Label": {
            "default": "ECS"
          }
        }
      ],
      "TemplateTags": [
        "Creates VPC ECS instance"
      ]
    }
  },
  "Parameters": {
    "SystemDiskCategory": {
      "Type": "String",
      "Label": {
        "en": "System Disk Type",
        "zh-cn": "系统盘类型"
      },
      "Description": {
        "en": "<font color='blue'><b>Optional values:</b></font><br>[cloud_efficiency: <font color='green'>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color='green'>SSD Cloud Disk</font>]<br>[cloud_essd: <font color='green'>ESSD Cloud Disk</font>]<br>[cloud: <font color='green'>Cloud Disk</font>]<br>[ephemeral_ssd: <font color='green'>Local SSD Cloud Disk</font>]",
        "zh-cn": "<font color='blue'><b>可选值:</b></font><br>[cloud_efficiency: <font color='green'>高效云盘</font>]<br>[cloud_ssd: <font color='green'>SSD云盘</font>]<br>[cloud_essd: <font color='green'>ESSD云盘</font>]<br>[cloud: <font color='green'>普通云盘</font>]<br>[ephemeral_ssd: <font color='green'>本地SSD盘</font>]"
      },
      "AllowedValues": [
        "cloud_efficiency",
        "cloud_ssd",
        "cloud",
        "cloud_essd",
        "ephemeral_ssd"
      ],
      "Default": "cloud_ssd"
    },
    "InstanceImageId": {
      "Type": "String",
      "Default": "centos_7",
      "AllowedValues": [
        "centos_6",
        "centos_7",
        "centos_8"
      ],
      "Description": {
        "zh-cn": "镜像ID, 支持选择 [centos_6,centos_7,centos_8]",
        "en": "Image ID,support [centos_6,centos_7,centos_8]"
      },
      "Label": {
        "zh-cn": "镜像",
        "en": "Image"
      }
    },
    "InstanceType": {
      "Type": "String",
      "Description": {
        "zh-cn": "填写VSwitch可用区下可使用的规格;<br>通用规格:<font color='red'><b>ecs.c5.large</b></font><br>注:可用区可能不支持通用规格<br>规格详见:<a href='https://help.aliyun.com/document_detail/25378.html' target='_blank'><b><font color='blue'>实例规格族</font></a></b>",
        "en": "Fill in the specifications that can be used under the VSwitch availability zone;</b></font><br>general specifications:<font color='red'><b>ecs.c5.large</b></font><br>note: a few zones do not support general specifications<br>see detail: <a href='https://www.alibabacloud.com/help/en/doc-detail/25378.html' target='_blank'><b><font color='blue'>Instance Specification Family</font></a></b>"
      },
      "Label": {
        "zh-cn": "实例规格",
        "en": "Instance Type"
      },
      "AssociationProperty": "ALIYUN::ECS::Instance::InstanceType",
      "AssociationPropertyMetadata": {
        "ZoneId": "VSwitchZoneId"
      }
    },
    "InstancePassword": {
      "NoEcho": true,
      "Type": "String",
      "Description": {
        "en": "Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).",
        "zh-cn": "服务器登录密码。长度为8~30个字符,必须包含大写英文字母、小写英文字母、数字和特殊字符(`()~!@#$%^&*_-+=|{}[]:;'<>,.?/)中的三项。"
      },
      "AllowedPattern": "[0-9A-Za-z\\_\\-\\&:;'<>,=%`~!@#\\(\\)\\$\\^\\*\\+\\|\\{\\}\\[\\]\\.\\?\\/]+$",
      "Label": {
        "en": "Instance Password",
        "zh-cn": "实例密码"
      },
      "ConstraintDescription": {
        "en": "Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).",
        "zh-cn": "长度为8~30个字符,必须包含大写英文字母、小写英文字母、数字和特殊字符(`()~!@#$%^&*_-+=|{}[]:;'<>,.?/)中的三项。"
      },
      "MinLength": 8,
      "MaxLength": 30
    },
    "InstancePublicIP": {
      "Type": "Boolean",
      "Description": {
        "zh-cn": "是否分配公共IP。",
        "en": "Whether to assign a common IP."
      },
      "Label": {
        "zh-cn": "分配公网IP",
        "en": "Allocate Public IP"
      },
      "Default": false
    },
    "VSwitchZoneId": {
      "AssociationProperty": "ALIYUN::ECS::Instance:ZoneId",
      "Type": "String",
      "Description": {
        "zh-cn": "可用区ID。<br><b>注: <font color='blue'>选择前请确认该可用区是否支持创建ECS资源的规格,建议设置与其他交换机不同的可用区</font></b>",
        "en": "Availability Zone ID.<br><b>note:<font color='blue'>before selecting, please confirm that the Availability Zone supports the specification of creating ECS resources,which is recommended to be different from other VSwitch Availability Zone</font></b>"
      },
      "Label": {
        "zh-cn": "交换机可用区",
        "en": "VSwitch Availability Zone"
      }
    },
    "SystemDiskSize": {
      "Default": 40,
      "Type": "Number",
      "Description": {
        "zh-cn": "系统盘大小, 取值范围:40~500, 单位:GB。",
        "en": "System disk size, range of values: 40-500, units: GB."
      },
      "Label": {
        "zh-cn": "系统盘空间",
        "en": "System Disk Space"
      }
    }
  },
  "Mappings": {
    "YumMap": {
      "centos_6": {
        "YumSource": "http://mirrors.aliyun.com/repo/Centos-6.repo"
      },
      "centos_7": {
        "YumSource": "http://mirrors.aliyun.com/repo/Centos-7.repo"
      },
      "centos_8":{
        "YumSource": "http://mirrors.aliyun.com/repo/Centos-8.repo"
      }
    }
  },
  "Resources": {
    "RosConditionHandle": {
      "Type": "ALIYUN::ROS::WaitConditionHandle"
    },
    "RosWaitCondition": {
      "Type": "ALIYUN::ROS::WaitCondition",
      "DependsOn": "EcsInstance",
      "Properties": {
        "Timeout": 1800,
        "Count": 1,
        "Handle": {
          "Ref": "RosConditionHandle"
        }
      }
    },
    "EcsVSwitch": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "EcsVpc"
        },
        "ZoneId": {
          "Ref": "VSwitchZoneId"
        },
        "CidrBlock": "192.168.0.0/24"
      }
    },
    "EcsInstance": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "UserData": {
          "Fn::Replace": [
            {
              "ros-notify": {
                "Fn::GetAtt": [
                  "RosConditionHandle",
                  "CurlCli"
                ]
              }
            },
            {
              "Fn::Join": [
                "",
                [
                  "#!/bin/bash \n",
                  "yum_repo=",
                  {
                    "Fn::FindInMap": [
                      "YumMap",
                      {
                        "Ref": "InstanceImageId"
                      },
                      "YumSource"
                    ]
                  },
                  "\n",
                  "mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup\n",
                  "curl -o /etc/yum.repos.d/CentOS-Base.repo $yum_repo \n",
                  "yum clean all \n",
                  "yum makecache \n",
                  "ros-notify -d '{\"data\" : \"Install Centos success.\"}'\n"
                ]
              ]
            }
          ]
        },
        "IoOptimized": "optimized",
        "PrivateIpAddress": "192.168.0.1",
        "VpcId": {
          "Ref": "EcsVpc"
        },
        "SecurityGroupId": {
          "Ref": "EcsSecurityGroup"
        },
        "VSwitchId": {
          "Ref": "EcsVSwitch"
        },
        "ImageId": {
          "Ref": "InstanceImageId"
        },
        "AllocatePublicIP": {
          "Ref": "InstancePublicIP"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "SystemDiskSize": {
          "Ref": "SystemDiskSize"
        },
        "SystemDiskCategory": {
          "Ref": "SystemDiskCategory"
        },
        "Password": {
          "Ref": "InstancePassword"
        }
      }
    },
    "EcsSecurityGroup": {
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "VpcId": {
          "Ref": "EcsVpc"
        },
        "SecurityGroupIngress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "SourceCidrIp": "0.0.0.0/0",
            "IpProtocol": "all",
            "NicType": "intranet"
          }
        ],
        "SecurityGroupEgress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "IpProtocol": "all",
            "DestCidrIp": "0.0.0.0/0",
            "NicType": "intranet"
          }
        ]
      }
    },
    "EcsVpc": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "CidrBlock": "192.168.0.0/16",
        "VpcName": {
          "Fn::Join": [
            "-",
            [
              "StackId",
              {
                "Ref": "ALIYUN::StackId"
              }
            ]
          ]
        }
      }
    }
  },
  "Outputs": {
    "EcsInstanceId": {
      "Value": {
        "Fn::GetAtt": [
          "EcsInstance",
          "InstanceId"
        ]
      }
    }
  }
}

步骤二:创建资源栈

  1. 登录资源编排控制台
  2. 在左侧导航栏,单击资源栈
  3. 在页面左上角的地域下拉列表,选择资源栈的所在地域。
  4. 资源栈列表页面,单击创建资源栈,然后在下拉列表中选择使用新资源(标准)
  5. 选择模板页面,选择已有模板,选择模板录入方式输入模板,输入步骤一中JSON格式的模板,然后单击下一步
  6. 配置模板参数页面,输入资源栈名称,并设置以下参数。
    参数 说明 示例
    交换机可用区 资源所属的可用区。 华北1可用区C
    镜像 ECS实例的镜像ID。

    默认值:centos_7。

    更多信息,请参见镜像概述

    centos_7
    实例规格 ECS实例规格。

    请选用有效的实例规格。更多信息,请参见实例规格族

    ecs.c5.large
    系统盘空间 ECS实例的系统盘空间。

    取值范围:40~500。

    单位:GB。

    40
    分配公网IP 是否为ECS实例分配公网地址。
    • 选中方框:分配公网IP地址。
    • 不选中方框:不分配公网IP地址。
    选中方框
    实例密码 ECS实例登录密码。 Test_12****
    系统盘类型 ECS实例的系统盘规格。取值:
    • cloud_efficiency:高效云盘。
    • cloud_ssd:SSD云盘。
    • cloud_essd:ESSD云盘。
    • cloud:普通云盘。
    • ephemeral_ssd:本地SSD盘。

    更多信息,请参见云盘概述

    cloud_efficiency
  7. 单击创建
  8. 资源栈信息页签查看资源栈状态。资源栈创建成功后,单击输出页签,获取ECS实例ID。
  9. 在ECS控制台连接相应的ECS实例,查看yum源。
    关于如何连接ECS实例,请参见连接方式概述

步骤三:查看资源

  1. 登录资源编排控制台
  2. 在左侧导航栏,单击资源栈
  3. 资源栈列表页面,单击目标资源栈名称。
  4. 单击资源页签,查看资源信息。
    本示例中,资源信息如下表所示。
    资源 数量 资源说明 规格说明
    ALIYUN::ECS::Instance 1 创建一台云服务器。
    • 总数量:1台。
    • 实例规格:ecs.c5.large。
    • 磁盘类别:高效云盘。
    • 系统盘空间:40 GB。
    • 分配公网IP:是。
    说明 资源收费情况,请参见官网报价或各产品定价文档。