通过创建或更新资源栈,ROS支持设置不同的条件,从而在不同的操作系统中创建IPv4和IPv6双栈云服务器。
背景信息
当您创建绑定IPv4和IPv6地址的ECS服务器时,需要单独创建IPv6网关,开通IPv6公网带宽并配置公网地址,并且对于不同操作系统的配置方式存在差异,这将增加您的使用成本和出错概率。此时您可以使用模板示例,在不同的操作系统中创建IPv4和IPv6双栈云服务器。
条件(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"
]
]
}
]
}
]
}