1. 背景
某客户有几千台ECS实例,且存在多种续费方式,需要将按周续费的续费方式改为按月续费,手工操作非常耗时,自己开发脚本调用API也需要一定的工作量,这时批量运维神奇-阿里云运维编排服务(OOS)就派上用场了,批量操作不用愁,只需点击几下鼠标就轻松可完成以上操作。一个模板即可以轻松实现批量ECS实例自动续费时长周、月、年之间的任意转换。本文以按周续费改为按月续费为例进行介绍。
我们先登录ECS控制台实例详情页面来查看一台实例目前的续费时长,可以看到目前此实例的续费方式为自动续费1周。
下面用OOS来批量修改实例续费时长。
2. 步骤
2.1 创建OOS模板
登录OOS控制台创建模板。
为你的模板起个好听的名字并输入模板内容。
模板内容如下:
{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "Bulky modify instance period unit.",
"zh-cn": "批量修改实例的续费时长单位",
"name-en": "ACS-ECS-BulkyModifyInstancePeriodUnit",
"name-zh-cn": "批量修改实例的续费时长单位",
"categories": [
"instance_manage"
]
},
"Parameters": {
"regionId": {
"Type": "String",
"Description": {
"en": "The id of region.",
"zh-cn": "地域ID。"
},
"AssociationProperty": "RegionId",
"Default": "{
{ ACS::RegionId }}"
},
"targets": {
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "regionId"
}
},
"originPeriodUnit": {
"Description": {
"en": "The current period unit of instance.",
"zh-cn": "实例当前续费时长的时间单位"
},
"Type": "String",
"AllowedValues": [
"Month",
"Week",
"Year"
],
"Default": "Week"
},
"targetPeriodUnit": {
"Description": {
"en": "The target period unit of instance.",
"zh-cn": "实例目标续费时长的时间单位"
},
"Type": "String",
"AllowedValues": [
"Month",
"Week",
"Year"
],
"Default": "Month"
},
"rateControl": {
"Description": {
"en": "Concurrency ratio of task execution.",
"zh-cn": "任务执行的并发比率。"
},
"Type": "Json",
"AssociationProperty": "RateControl",
"Default": {
"Mode": "Concurrency",
"MaxErrors": 0,
"Concurrency": 10
}
},
"OOSAssumeRole": {
"Description": {
"en": "The RAM role to be assumed by OOS.",
"zh-cn": "OOS扮演的RAM角色。"
},
"Type": "String",
"Default": "OOSServiceRole"
}
},
"RamRole": "{
{ OOSAssumeRole }}",
"Tasks": [
{
"Name": "getInstance",
"Description": {
"en": "Views the ECS instances.",
"zh-cn": "获取ECS实例。"
},
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "{
{ regionId }}",
"Filters": [
"{
{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
},
{
"Name": "modifyInstanceChargeType",
"Description": {
"en": "Modifies instance period unit",
"zh-cn": "修改实例续费时长单位"
},
"Action": "ACS::Template",
"Properties": {
"TemplateName": "ECS-ModifyInstancePeriodUnit",
"Parameters": {
"regionId": "{
{ regionId }}",
"originPeriodUnit": "{
{ originPeriodUnit }}",
"instanceId": "{
{ ACS::TaskLoopItem }}",
"targetPeriodUnit": "{
{ targetPeriodUnit }}"
}
},
"Loop": {
"RateControl": "{
{ rateControl }}",
"Items": "{
{ getInstance.instanceIds }}"
}
}
]
}
点击创建模板:
2.2 创建执行任务
根据以上模板创建执行任务。
设置参数,选择全部实例(也可根据需要手动选择或通过标签资源组等方式选择实例),付费方式选择包年包月,设置好待转换的续费时长类型(Week)以及目标续费时长类型(Month)。
点击确定执行此模板。
2.3 查看结果
等待任务执行结束,查看执行结果。
在OOS控制台执行管理高级视图中我们可以看到几台实例的续费时长已经修改成功了,其中失败的实例是因为实例状态已过期,可以续费后再进行操作。
也可以登录到ECS控制台实例详情页面查看实例修改后的续费时长信息: