基于Conditions在不同操作系统创建ECS
1. 编写ROS模板
模板说明
条件(Conditions)由Fn::And、Fn::Or、Fn::Not和Fn::Equals中的一个或多个函数定义,根据您在创建或更新资源栈时,指定的输入参数值进行计算。在每个条件中,都可以引用其他条件、参数值或映射。本文以创建绑定IPv4和IPv6双栈云服务器为例为您介绍。关于模板示例的更多信息,请参见创建绑定IPv4和IPv6双栈云服务器。
在模板中根据Parameters的InstanceImageId参数是否以centos开头做为判断条件,在Conditions对象中使用Fn::Equals、Fn::Select、Fn::Split函数对所选择的InstanceImageId做数据处理与逻辑判断。代码示例如下:
{ "Parameters": { "InstanceImageId": { "Type": "String", "Default": "centos_7", "Description": { "zh-cn": "镜像ID, <br>Linux系统请选择:<font color='red'><b>centos_7</b></font> <br>Windows系统请选择:<font color='red'><b>win2008r2;win2012r2;win2016</b></font>", "en": "Image ID,<br>Linux System Select:<font color='red'><b>centos_7</b></font> <br>Windows System Select:<font color='red'><b>win2008r2;win2012r2;win2016</b></font>" }, "Label": { "zh-cn": "镜像", "en": "Image" } } }, "Conditions": { "CreateLinux": { "Fn::Equals": [ "centos", { "Fn::Select": [ "0", { "Fn::Split": [ "_", { "Ref": "InstanceImageId" } ] } ] } ] } } }
创建ECS初始化UserData时,使用Fn::If函数判断Conditions对象CreateLinux,实现选择不同操作系统执行不同初始化命令的需求,然后创建IPv4和IPv6双栈云服务器。代码示例如下:
{ "Fn::If": [ "CreateLinux", { "Fn::Replace": [ { "ros-notify": { "Fn::GetAtt": [ "WaitConditionHandle", "CurlCli" ] } }, { "Fn::Join": [ "", [ "#!/bin/sh", " \n", "cd /opt \n", "wget http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/rhel/ecs-utils-ipv6 \n", "chmod +x ./ecs-utils-ipv6 \n", "./ecs-utils-ipv6 \n", "ros-notify -d \"{\\\"Data\\\" : \\\"SUCCESS\\\", \\\"Status\\\" : \\\"SUCCESS\\\"}\" \n" ] ] } ] }, { "Fn::Replace": [ { "ros-notify": { "Fn::GetAtt": [ "WaitConditionHandle", "PowerShellCurlCli" ] } }, { "Fn::Join": [ "", [ "[powershell]\r\n", "New-Item -Path \"C:\\set_ipv6\" -Force -type directory\r\n", "cd C:\\set_ipv6 \r\n", "$install_dir=\"C:\\set_ipv6\" \r\n", "$install_path = \"$install_dir\\ecs-utils-ipv6.exe\" \r\n", "$tool_url = 'http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/win/64/ecs-utils-ipv6.exe' \r\n", "Invoke-WebRequest -uri $tool_url -OutFile $install_path \r\n", "Unblock-File $install_path \r\n", "Start-Process -FilePath \"$install_path\" -ArgumentList \"--noenterkey\" -NoNewWindow \r\n", "ros-notify\r\n" ] ] } ] } ] }
声明创建的资源(Resources)
示例模版如下:
{ "ROSTemplateFormatVersion": "2015-09-01", "Description": { "en": "Create a cloud server with an IPV4/IPV6 stack and automatically assign IPv6 public address to the cloud host.", "zh-cn": "创建一台具备IPV4/IPV6双栈的云服务器,并为云主机自动分配IPv6公网地址。" }, "Metadata": { "ALIYUN::ROS::Interface": { "ParameterGroups": [ { "Parameters": [ "VpcCidrBlock", "VSwitchCidrBlock", "VSwitchZoneId", "InstanceImageId", "InstanceType", "SystemDiskSize", "InstancePublicIP", "InternetChargeType", "IPV6Bandwidth", "IPV6GateWaySpec", "SystemDiskCategory", "InstancePassword" ], "Label": { "default": "ECS" } } ], "TemplateTags": [ "acs:example:云起实验室:基于Conditions在不同操作系统创建ECS" ] } }, "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", "Description": { "zh-cn": "镜像ID, <br>Linux系统请选择:<font color='red'><b>centos_7</b></font> <br>Windows系统请选择:<font color='red'><b>win2008r2;win2012r2;win2016</b></font>", "en": "Image ID,<br>Linux System Select:<font color='red'><b>centos_7</b></font> <br>Windows System Select:<font color='red'><b>win2008r2;win2012r2;win2016</b></font>" }, "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": "是否分配IPV4公共IP。", "en": "Whether to assign a IPV4 common IP." }, "Label": { "zh-cn": "分配IPV4公网IP", "en": "Allocate IPV4 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" } }, "IPV6Bandwidth": { "Default": 10, "Type": "Number", "Description": { "zh-cn": "IPv6网关公网带宽, 取值范围:[1, 5000], 单位:Mbps。当公网带宽计费方式为按使用流量计费时取值范围为1~2000Mbps,当公网带宽计费方式为按带宽计费时取值范围为1~5000Mbps。", "en": "IPv6 network Gateway public network bandwidth, value range: s1, 5000, in Mbps. When the public network bandwidth billing method is based on the use of traffic billing value range of 1 to 2000Mbps, when the public network bandwidth billing method is metered by bandwidth when the value range is 1 to 5000Mbps." }, "Label": { "zh-cn": "IPv6网关公网带宽", "en": "IPv6 Network Gateway Public Network Bandwidth" } }, "IPV6GateWaySpec": { "Default": "Small", "Type": "String", "Label": { "en": "Specifications for the IPv6 Gateway", "zh-cn": "IPv6网关的规格" }, "Description": { "en": "IPv6 gateway specifications, optional values: Small (free version), Medium (enterprise version), Large (enterprise enhancement version)", "zh-cn": "IPv6网关的规格,可选值:Small(免费版),Medium(企业版),Large(企业增强版)" }, "AllowedValues": [ "Small", "Medium", "Large" ] }, "VpcCidrBlock": { "Default": "192.168.0.0/16", "AllowedValues": [ "192.168.0.0/16", "172.16.0.0/12", "10.0.0.0/8" ], "Type": "String", "Description": { "en": "New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>", "zh-cn": "新建专有网络IP地址段范围,推荐使用以下的IP地址段<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>" }, "Label": { "zh-cn": "专有网络IPV4网段", "en": "VPC IPV4 CIDR Block" } }, "VSwitchCidrBlock": { "Default": "192.168.0.0/24", "Type": "String", "Description": { "en": "Network segments of new switches, subnets of virtual proprietary networks", "zh-cn": "新建交换机的网段,所属虚拟专有网络的子网" }, "Label": { "zh-cn": "交换机IPV4网段", "en": "VSwitch IPV4 CIDR Block" } }, "InternetChargeType": { "Type": "String", "AllowedValues": [ "PayByTraffic", "PayByBandwidth" ], "Default": "PayByBandwidth", "Description": { "en": "<font color='blue'><b>Optional values:</b></font><br>[PayByTraffic: <font color='green'>Billing by Usage</font>]<br>[PayByBandwidth: <font color='green'>Billing by Bandwidth</font>]", "zh-cn": "<font color='blue'><b>可选值:</b></font><br>[PayByTraffic: <font color='green'>按流量计费</font>]<br>[PayByBandwidth: <font color='green'>按带宽计费</font>]" }, "Label": { "zh-cn": "IPv6公网带宽的计费方式", "en": "IPv6 Metric Bandwidth is Billed." } } }, "Resources": { "WaitCondition": { "Type": "ALIYUN::ROS::WaitCondition", "Properties": { "Timeout": 900, "Count": 1, "Handle": { "Ref": "WaitConditionHandle" } } }, "WaitConditionHandle": { "Type": "ALIYUN::ROS::WaitConditionHandle" }, "EcsVSwitch": { "Type": "ALIYUN::ECS::VSwitch", "Properties": { "VpcId": { "Ref": "EcsVpc" }, "ZoneId": { "Ref": "VSwitchZoneId" }, "Ipv6CidrBlock": 0, "CidrBlock": { "Ref": "VSwitchCidrBlock" } } }, "EcsInstance": { "Type": "ALIYUN::ECS::Instance", "DependsOn": [ "EcsVpc", "EcsSecurityGroup", "EcsVSwitch" ], "Properties": { "IoOptimized": "optimized", "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" }, "UserData": { "Fn::If": [ "CreateLinux", { "Fn::Replace": [ { "ros-notify": { "Fn::GetAtt": [ "WaitConditionHandle", "CurlCli" ] } }, { "Fn::Join": [ "", [ "#!/bin/sh", " \n", "cd /opt \n", "wget http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/rhel/ecs-utils-ipv6 \n", "chmod +x ./ecs-utils-ipv6 \n", "./ecs-utils-ipv6 \n", "ros-notify -d \"{\\\"Data\\\" : \\\"SUCCESS\\\", \\\"Status\\\" : \\\"SUCCESS\\\"}\" \n" ] ] } ] }, { "Fn::Replace": [ { "ros-notify": { "Fn::GetAtt": [ "WaitConditionHandle", "PowerShellCurlCli" ] } }, { "Fn::Join": [ "", [ "[powershell]\r\n", "New-Item -Path \"C:\\set_ipv6\" -Force -type directory\r\n", "cd C:\\set_ipv6 \r\n", "$install_dir=\"C:\\set_ipv6\" \r\n", "$install_path = \"$install_dir\\ecs-utils-ipv6.exe\" \r\n", "$tool_url = 'http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/win/64/ecs-utils-ipv6.exe' \r\n", "Invoke-WebRequest -uri $tool_url -OutFile $install_path \r\n", "Unblock-File $install_path \r\n", "Start-Process -FilePath \"$install_path\" -ArgumentList \"--noenterkey\" -NoNewWindow \r\n", "ros-notify\r\n" ] ] } ] } ] } } }, "EcsSecurityGroup": { "Type": "ALIYUN::ECS::SecurityGroup", "Properties": { "VpcId": { "Ref": "EcsVpc" }, "SecurityGroupIngress": [ { "Priority": 1, "PortRange": "22/22", "NicType": "internet", "SourceCidrIp": "0.0.0.0/0", "IpProtocol": "tcp" }, { "Priority": 1, "PortRange": "3389/3389", "NicType": "intranet", "SourceCidrIp": "0.0.0.0/0", "IpProtocol": "tcp" }, { "PortRange": "-1/-1", "Priority": 1, "Ipv6SourceCidrIp": "::/0", "IpProtocol": "all", "NicType": "intranet" } ], "SecurityGroupEgress": [ { "Priority": 1, "PortRange": "22/22", "NicType": "internet", "DestCidrIp": "0.0.0.0/0", "IpProtocol": "tcp" }, { "Priority": 1, "PortRange": "3389/3389", "NicType": "intranet", "DestCidrIp": "0.0.0.0/0", "IpProtocol": "tcp" }, { "PortRange": "-1/-1", "Priority": 1, "IpProtocol": "all", "Ipv6DestCidrIp": "::/0", "NicType": "intranet" } ] } }, "VpcIpv6Gateway": { "Type": "ALIYUN::VPC::Ipv6Gateway", "Properties": { "VpcId": { "Ref": "EcsVpc" }, "Name": "MyIpv6Gateway", "Spec": { "Ref": "IPV6GateWaySpec" } } }, "VpcIpv6InternetBandwidth": { "DependsOn": [ "VpcIpv6Gateway", "EcsAssignIpv6Addresses" ], "Type": "ALIYUN::VPC::Ipv6InternetBandwidth", "Properties": { "Bandwidth": { "Ref": "IPV6Bandwidth" }, "Ipv6GatewayId": { "Ref": "VpcIpv6Gateway" }, "Ipv6AddressId": { "Fn::Select": [ 0, { "Fn::GetAtt": [ "EcsAssignIpv6Addresses", "Ipv6AddressIds" ] } ] }, "InternetChargeType": { "Ref": "InternetChargeType" } } }, "EcsVpc": { "Type": "ALIYUN::ECS::VPC", "Properties": { "CidrBlock": { "Ref": "VpcCidrBlock" }, "VpcName": { "Fn::Join": [ "-", [ "StackId", { "Ref": "ALIYUN::StackId" } ] ] }, "EnableIpv6": true } }, "EcsAssignIpv6Addresses": { "Type": "ALIYUN::ECS::AssignIpv6Addresses", "DependsOn": [ "EcsInstance" ], "Properties": { "NetworkInterfaceId": { "Fn::GetAtt": [ "EcsInstance", "PrimaryNetworkInterfaceId" ] }, "Ipv6AddressCount": 1 } } }, "Outputs": { "EcsInstancePrivateIp": { "Description": "EcsInstance PrivateIp", "Value": { "Fn::GetAtt": [ "EcsInstance", "PrivateIp" ] } }, "EcsInstanceId": { "Description": "EcsInstance InstanceId", "Value": { "Fn::GetAtt": [ "EcsInstance", "InstanceId" ] } }, "EcsInstanceIpv6Address": { "Description": "EcsInstance Ipv6Address", "Value": { "Fn::Select": [ "0", { "Fn::GetAtt": [ "EcsAssignIpv6Addresses", "Ipv6Addresses" ] } ] } } }, "Conditions": { "CreateLinux": { "Fn::Equals": [ "centos", { "Fn::Select": [ "0", { "Fn::Split": [ "_", { "Ref": "InstanceImageId" } ] } ] } ] } } }
2. 创建资源栈
以下操作需登录阿里云账号,在资源编排控制台完成以下操作流程。
访问资源编排控制台
1. 进入资源编排控制台。
访问资源栈页面
2. 在资源编排控制台左侧导航栏中单机资源栈。
选择资源创建地域
3. 在页面左上角的地域下拉列表,选择资源栈的所在地域(例如:华北2:北京)。
创建资源栈
4. 在资源栈列表页面,单击创建资源栈,然后在下拉列表中选择使用新资源(标准)。
输入模板
5. 在选择模板页面,选择已有模板:选择输入模板方式,然后将上述示例模板内容复制到模板内容输入框内。然后单击下一步。
模板参数配置
6. 在配置模板参数页面,配置资源栈名称及其余参数,然后单击下一步。
参数配置
参数 |
说明 |
专有网络IPv4网段 |
专有网络的IPv4网段。推荐使用以下网段:
更多信息,请参见专有网络和交换机概述。 |
交换机IPv4网段 |
专有网络下交换机的IPv4网段。 更多信息,请参见专有网络和交换机概述。 |
交换机可用区 |
专有网络下的交换机可用区ID。 |
镜像 |
ECS实例的镜像ID。可选择Linux(Centos)系列镜像或Windows系列镜像。 更多信息,请参见镜像概述。 |
实例规格 |
ECS实例规格。 请选用有效的实例规格。更多信息,请参见实例规格族。 |
系统盘空间 |
ECS实例的系统盘空间。 取值范围:40~500。 单位:GB。 |
分配IPv4公网IP |
是否为ECS实例分配IPv4公网地址。
|
IPv6公网带宽的计费方式 |
IPv6公网带宽的计费方式。取值:
|
IPv6网关公网带宽 |
IPv6网关的公网带宽。 取值范围:1~5000。 单位:Mbps。 |
IPv6网关的规格 |
IPv6网关的规格。取值:
|
系统盘类型 |
ECS实例的系统盘类型。取值:
更多信息,请参见云盘概述。 |
实例密码 |
ECS实例的登录密码。 |
资源栈信息配置
配置项 |
说明 |
资源栈策略 |
取值:
关于资源栈策略的更多信息,请参见资源栈策略。 |
失败时回滚 |
取值:
|
超时设置 |
如果所有资源的创建或更新没有在该时间内完成,系统将自动回滚到创建或更新之前的状态。 取值范围:10~1440。 单位:分钟。 |
删除保护 |
防止资源栈被意外删除。取值:
|
RAM角色 |
您可以创建可信实体为资源编排服务的RAM角色,然后根据ROS模板中资源所需最小权限为RAM角色授权。
|
标签 |
由一对键值对组成,方便您对资源栈进行分类。 您可以单击添加,然后在编辑标签绑定对话框设置标签键和标签值,最后单击确定。 |
资源组 |
您可以选择资源栈所在的资源组。如果不指定资源组,资源栈将加入默认资源组。 关于如何创建资源组,请参见创建资源组。 |
创建资源栈
7. 在检查并确认页面,单击创建。
3. 查看资源栈
本步骤将指导您在完成创建资源栈操作后,如何查询资源栈详情。
1. 进入资源编排控制台。
2. 在左侧导航栏单击资源栈。
3. 在页面左上角的地域下拉列表,选择资源栈的所在地域。
4. 在资源栈列表页面,找到需要查看的资源栈,单击资源栈名称列的资源栈ID。
5. 在资源栈管理页面,您可以执行以下操作:
- 单击资源栈信息页签,查看基本信息和资源栈策略。
查看资源栈事件
6. 单击事件页签,查看资源栈生命周期中发生的每一个事件。
查看资源栈资源
7. 单击资源页签,查看资源栈创建涉及到的资源。
说明:ECS属于付费云产品资源,请您及时释放。您可以点击资源列表下的EcsInstance栏中的ECS实例ID,即可跳转至ECS控制台查看本次创建的ECS资源信息。
4. 释放资源栈
本步骤将指导您如何释放通过资源编排控制台创建资源内容。
进入资源栈列表
1. 在模板页签,单击右上角的 图标。
选择待删除的资源栈
2. 在资源栈列表页面,找到您创建的资源栈,单击操作列下的删除。
选择资源栈删除方式并删除
3. 在删除资源栈对话框中,删除方式选择释放资源,单击确定。
实验链接:https://developer.aliyun.com/adc/scenario/a31cd3aa1aa84d8a854c58d906cdba65