Tag简介和场景
对于一般的资源管理需求,都是针对一个用户下数量较多的情况,当实例等数量较多时,对实例进行运维管理等操作就会变得比较困难,有时候甚至需要采取拆分账号的方式管理不同部门或者不同用途的资源。如果采用Tag进行资源的分类管理,会大大简化这个问题。
首先,我们可以针对实例的使用场景进行分类,在一般的开发场景中,机器一般有多个分类:开发测试环境、打包环境、生产环境等。这些机器的运维管理是绝对隔绝的,因此要在Tag上对其进行区分,在开发测试机器上,可以增加标签(增加方式详见下一节)key为env、value为test;在生产机器上,可以增加标签key为env、value为product。形成如下图的机器分类。
我们可以使用不同的维度来给机器打Tag,
以万网的场景需求为给机器以kernelVersion,osType等场景来打Tag. osType分为linux,windows. kernerlVersion为uname -r的返回结果
解决方案
针对以上需求总结下来
1.根据当前实例的OsType字段给当前实例打个key为osType的tag
2.根据当前实例中的uname -r的运行结果打一个key为kernelVersion的tag
针对以上场景我们可以构建两个运维编排脚本
按OSType打Tag
输入: tagKey的名字,和InstanceId 以及运维编排服务以客户身份扮演的角色名
输出: 当前机器的osType
运行步骤:
a) 指定InstanceId调用DescribeInstances接口并获取到OsType
b) 调用TagResources给Tag的Key设定为输入的TagKey Value指定为a步骤的输出结果,实例ID指定为输入的InstanceId
这样我们就完成了根据OsType打Tag的操作,模板如下
{ "FormatVersion": "OOS-2019-06-01", "Description": "tag instance by os type", "Parameters": { "InstanceId": { "Type": "String", "Description": "the InstanceId to tag", "MinLength": 1, "MaxLength": 30 }, "TagKey": { "Type": "String", "Description": "the tag key you specified" }, "OOSAssumeRole": { "Type": "String", "Description": "The RAM role to be assumed by OOS.", "Default": "OOSServiceRole" } }, "RamRole": "{{OOSAssumeRole}}", "Tasks": [ { "Name": "queryInstanceOsType", "Action": "ACS::ExecuteApi", "Description": "", "Properties": { "API": "DescribeInstances", "Service": "ECS", "Parameters": { "InstanceIds": [ "{{ InstanceId }}" ] } }, "Outputs": { "OsType": { "ValueSelector": "Instances.Instance[].OSType", "Type": "String" } } }, { "Name": "tagResources", "Action": "ACS::ExecuteApi", "Description": "create the command to install logtail agent.", "Properties": { "API": "TagResources", "Service": "ECS", "Parameters": { "ResourceType": "Instance", "ResourceIds": [ "{{ InstanceId }}" ], "Tags": [ { "Key": "{{ TagKey }}", "Value": "{{ queryInstanceOsType.OsType }}" } ] } } } ], "Outputs": { "OsType": { "Type": "String", "Value": "{{ queryInstanceOsType.OsType}}" } } }
AI 代码解读
打开运维编排控制台并创建模板TagByOsType
执行模板
设置参数 指定实例ID和TagKey执行
我们看到刚刚执行已经执行成功
并且可以看到当前执行的详细细节
再看刚刚的实例多了个osType:linux的tag
按KernelVersion打Tag
输入: tagKey的名字,要在机器上执行的命令 当前场景是uname -r 以及运维编排服务以客户身份扮演的角色名
输出:打tag的value
运行步骤:
a) 指定InstanceId调用DescribeInstances检查当前实例是否是Running状态
b) 调用云助手来到当前实例执行命令并等待执行结束获取到执行结果
c) 调用TagResources给Tag的Key设定为输入的TagKey Value指定为b步骤的输出结果,实例ID指定为输入的InstanceId
这样我们就完成了根据在执行机器执行命令根据命令结果打Tag的操作,模板如下
{ "FormatVersion": "OOS-2019-06-01", "Description": "Tag ECS Instance by the RunCommand invocation result.", "Parameters": { "InstanceId": { "Type": "String", "Description": "the Instance Id to operate in linux.", "MinLength": 1, "MaxLength": 30 }, "CommandContent": { "Type": "String", "Description": "command content to run in linux ecs." }, "TagKey": { "Type": "String", "Description": "tag specific key you want to tag on the instance." }, "OOSAssumeRole": { "Type": "String", "Description": "oos assume this role to execution task.", "Default": "OOSServiceRole" } }, "RamRole": "{{OOSAssumeRole}}", "Tasks": [{ "Name": "checkInstanceReady", "Action": "ACS::CheckFor", "Description": "describe instances with specified parameters, refer them here: https://help.aliyun.com/document_detail/63440.html", "Properties": { "API": "DescribeInstances", "Service": "ECS", "PropertySelector": "Instances.Instance[].Status", "DesiredValues": [ "Running" ], "Parameters": { "InstanceIds": ["{{ InstanceId }}"] } } }, { "Name": "queryInstanceCommandOutput", "Action": "ACS::ECS::RunCommand", "Description": "", "Properties": { "commandContent": "{{CommandContent}}", "type": "RunShellScript", "instanceId": "{{InstanceId}}" }, "Outputs": { "CommandOutput": { "Type": "String", "ValueSelector": "InvocationResult[].Output" } } }, { "Name": "tagResources", "Action": "ACS::ExecuteApi", "Description": "create the command to install logtail agent.", "Properties": { "API": "TagResources", "Service": "ECS", "Parameters": { "ResourceType": "Instance", "ResourceIds": [ "{{ InstanceId }}" ], "Tags": [{ "Key": "{{TagKey}}", "Value": { "Fn::Base64Decode": "{{ queryInstanceCommandOutput.CommandOutput }}" } }] } } } ], "Outputs": { "tagValue": { "Type": "String", "Value": { "Fn::Base64Decode": "{{ queryInstanceCommandOutput.CommandOutput}}" } } } }
AI 代码解读
执行结束后我们看结果符合预期实例上多了个kernelVersion:3.10.0-xxx的tag
总结
以上我们介绍了如果使用运维编排方便的给实例打Tag, 我们会把相应的场景抽象成公共模板,方便使用,并挖掘更多的类似运维场景。运维编排致力于解决客户运维的核心场景问题,以Ops As Code的方式提升客户自动化能力。目前处于内测中,欢迎体验和测试。
欢迎使用OOS
OOS管理控制台的链接
如果您遇到链接打不开的问题,请复制此链接到您的浏览器导航栏然后打开:
https://home.console.aliyun.com/redirect.htm?productId=ecs&path=automation/region/
OOS帮助文档的链接
OOS客户支持钉钉群:23330931
系列文章
最佳实践
玩转运维编排服务的权限:Assume Role+Pass Role
场景系列
运维编排场景系列-----给ECS实例自动打TAG
运维编排场景系列----从实例中拷贝文件到OSS
运维编排场景系列----给实例加到SLS机器组