小言_互联网的博客

基于OOS批量修改资源标签值

220人阅读  评论(0)

场景

当资源上有个标签键: 部门,标签值:零售,由于部门资源整合要把标签(键: 部门,值:零售)修改为标签(键: 部门,值:新零售),如何快速批量修改标签呢?

背景信息

运维编排服务OOS通过模板定义您需要编排的运维任务。模板内容支持YAML和JSON两种格式,模版分为公共模版和自定义模版两种类型。为了方便您快速使用OOS,OOS提供了公共模板供您直接使用和参考。。在使用模板前您需要仔细审查模板所要执行的运维任务,并优先在测试环境观察使用效果。

您也可以编写自定义模板来编写您所需要的运维任务。更多详情,请参见模板结构。本文中的TagResourcesFilterByTags是自定义模板

操作步骤

登录ECS管理控制台
在左侧导航栏,选择 运维与监控 > 运维编排
在顶部状态栏处,选择地域
选择 我的模板 点击 创建模版
选择JSON
以下示例是针对ecs实例及redis实例进行标签值的更新
operateId : 操作ID,区分每次操作使用
tagKey:标签键,用来过滤资源
tagValue:标签值,用来过滤资源
tagKey:标签键,要修改目标标签键
newTagValue:新标签值,要修改后的标签值


  
  1. {
  2. "Description": "根据标签查找资源并更新标签",
  3. "FormatVersion": "OOS-2019-06-01",
  4. "Parameters": {
  5. "operateId": {
  6. "Description": "操作ID,调用者自定义",
  7. "Type": "String",
  8. "MinLength": 1,
  9. "MaxLength": 64
  10. },
  11. "tagKey": {
  12. "Description": "当前key",
  13. "Type": "String",
  14. "MinLength": 1,
  15. "MaxLength": 64
  16. },
  17. "tagValue": {
  18. "Description": "当前value",
  19. "Type": "String",
  20. "MinLength": 1,
  21. "MaxLength": 64
  22. },
  23. "newTagValue": {
  24. "Description": "更新后value",
  25. "Type": "String",
  26. "MinLength": 1,
  27. "MaxLength": 64
  28. }
  29. },
  30. "Tasks": [
  31. {
  32. "Name": "DescribeInstances_ECS",
  33. "Action": "ACS::ExecuteAPI",
  34. "Description": {
  35. "zh-cn": "通过标签过滤ECS实例",
  36. "en": "filter ecs instances by tags"
  37. },
  38. "Properties": {
  39. "Service": "ECS",
  40. "API": "DescribeInstances",
  41. "AutoPaging": true,
  42. "Parameters": {
  43. "Tags": [
  44. {
  45. "Key": "{{ tagKey }}",
  46. "Value": "{{ tagValue }}"
  47. }
  48. ]
  49. }
  50. },
  51. "Outputs": {
  52. "Instances": {
  53. "Type": "List",
  54. "ValueSelector": "Instances.Instance[].InstanceId"
  55. }
  56. }
  57. },
  58. {
  59. "Name": "TagResources_ECS_Instances",
  60. "Action": "ACS::ExecuteAPI",
  61. "Description": {
  62. "zh-cn": "更新ECS实例标签",
  63. "en": "tag ecs instances"
  64. },
  65. "Properties": {
  66. "Service": "ECS",
  67. "API": "TagResources",
  68. "Parameters": {
  69. "Tags": [
  70. {
  71. "Key": "{{ tagKey }}",
  72. "Value": "{{ newTagValue }}"
  73. }
  74. ],
  75. "ResourceType": "Instance",
  76. "ResourceIds": [
  77. "{{ACS::TaskLoopItem}}"
  78. ]
  79. }
  80. },
  81. "Loop": {
  82. "MaxErrors": "100%",
  83. "Concurrency": 20,
  84. "Items": "{{DescribeInstances_ECS.Instances}}"
  85. }
  86. },
  87. {
  88. "OnError": "ACS::NEXT",
  89. "Name": "DescribeInstances_REDIS",
  90. "Action": "ACS::ExecuteApi",
  91. "Description": {
  92. "zh-cn": "通过标签过滤REDIS实例",
  93. "en": "filter redis(kvstore) instances by tags"
  94. },
  95. "Properties": {
  96. "Service": "R-kvstore",
  97. "API": "DescribeInstances",
  98. "AutoPaging": true,
  99. "Parameters": {
  100. "Tags": [
  101. {
  102. "Key": "{{ tagKey }}",
  103. "Value": "{{ tagValue }}"
  104. }
  105. ]
  106. }
  107. },
  108. "Outputs": {
  109. "Instances": {
  110. "Type": "List",
  111. "ValueSelector": "Instances.KVStoreInstance[].InstanceId"
  112. }
  113. }
  114. },
  115. {
  116. "Name": "TagResources_REDIS_Instances",
  117. "Action": "ACS::ExecuteAPI",
  118. "Description": {
  119. "zh-cn": "更新REDIS实例标签",
  120. "en": "tag redis instances"
  121. },
  122. "Properties": {
  123. "Service": "R-kvstore",
  124. "API": "TagResources",
  125. "Parameters": {
  126. "Tags": [
  127. {
  128. "Key": "{{ tagKey }}",
  129. "Value": "{{ newTagValue }}"
  130. }
  131. ],
  132. "ResourceType": "Instance",
  133. "ResourceIds": [
  134. "{{ACS::TaskLoopItem}}"
  135. ]
  136. }
  137. },
  138. "Loop": {
  139. "MaxErrors": "100%",
  140. "Concurrency": 20,
  141. "Items": "{{DescribeInstances_REDIS.Instances}}"
  142. }
  143. }
  144. ],
  145. "Outputs": {}
  146. }

创建执行:

执行详情:

当执行状态显示成功,表示运维任务已完成。
当执行状态显示失败,您可以单击操作列下的详情查看执行日志,并根据日志信息分析和调整执行内容。

可以根据本文的代码修改为自己需要的cases。


原文链接
本文为云栖社区原创内容,未经允许不得转载。


转载:https://blog.csdn.net/yunqiinsight/article/details/103252162
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场