全部产品
Search
文档中心

系统运维管理:创建模板

更新时间:Nov 25, 2024

本文以批量在ECS实例中执行命令为例,介绍如何在系统运维管理 OOS(CloudOps Orchestration Service)控制台新建自定义模板。

  1. 登录系统运维管理控制台

  2. 选择自动化任务 > 自定义任务模板,单击创建模板image

  3. 使用示例模板创建或使用本地文件创建。

    • 示例模板选择批量执行命令,单击下一步image

    • 如果您已经预先编写好批量执行命令YAML或JSON格式的模板文件,您可以选择使用本地文件创建,上传后,单击下一步image

      批量执行命令模板内容

      FormatVersion: OOS-2019-06-01
      Description: 批量在ECS实例中执行命令
      Parameters:
        targets:
          Label:
            en: TargetInstance
            zh-cn: 目标实例
          Type: Json
          AssociationProperty: Targets
          AssociationPropertyMetadata:
            ResourceType: ALIYUN::ECS::Instance
            RegionId: regionId
        rateControl:
          Label:
            en: RateControl
            zh-cn: 任务执行的并发比率
          Type: Json
          AssociationProperty: RateControl
          Default:
            Mode: Concurrency
            MaxErrors: 0
            Concurrency: 10
      Tasks:
        - Name: getInstance
          Description: 获取ECS实例
          Action: ACS::SelectTargets
          Properties:
            ResourceType: ALIYUN::ECS::Instance
            Filters:
              - '{{ targets }}'
          Outputs:
            instanceIds:
              Type: List
              ValueSelector: Instances.Instance[].InstanceId
        - Name: runCommand
          Action: ACS::ECS::RunCommand
          Description: 执行云助手命令
          Properties:
            instanceId: '{{ ACS::TaskLoopItem }}'
            commandType: RunShellScript
            commandContent: |-
              #!/bin/bash
      
              ##### 可在脚本开始运行时调用,打印当时的时间戳及PID。
              function job_start()
              {
                  now=`date +'%Y-%m-%d %H:%M:%S'`
                  echo "[$now][$$] job_start"
              }
      
              ##### 如果返回值为0,则认为此执行成功,如果非0,则认为执行失败
              job_start
            regionId: '{{ ACS::RegionId }}'
            workingDir: /root
            enableParameter: false
            timeout: 600
            username: ''
            parameters: {}
            windowsPasswordName: ''
          Outputs:
            invocationOutput:
              Type: String
              ValueSelector: invocationOutput
          Loop:
            Items: '{{ getInstance.instanceIds }}'
            RateControl: '{{ rateControl }}'
            Outputs:
              invocationOutputs:
                AggregateType: Fn::ListJoin
                AggregateField: invocationOutput
      Outputs:
        invocationOutputs:
          Type: List
          Value: '{{ runCommand.invocationOutputs }}'
      
      {
        "FormatVersion": "OOS-2019-06-01",
        "Description": "批量在ECS实例中执行命令",
        "Parameters": {
          "targets": {
            "Label": {
              "en": "TargetInstance",
              "zh-cn": "目标实例"
            },
            "Type": "Json",
            "AssociationProperty": "Targets",
            "AssociationPropertyMetadata": {
              "ResourceType": "ALIYUN::ECS::Instance",
              "RegionId": "regionId"
            }
          },
          "rateControl": {
            "Label": {
              "en": "RateControl",
              "zh-cn": "任务执行的并发比率"
            },
            "Type": "Json",
            "AssociationProperty": "RateControl",
            "Default": {
              "Mode": "Concurrency",
              "MaxErrors": 0,
              "Concurrency": 10
            }
          }
        },
        "Tasks": [
          {
            "Name": "getInstance",
            "Description": "获取ECS实例",
            "Action": "ACS::SelectTargets",
            "Properties": {
              "ResourceType": "ALIYUN::ECS::Instance",
              "Filters": [
                "{{ targets }}"
              ]
            },
            "Outputs": {
              "instanceIds": {
                "Type": "List",
                "ValueSelector": "Instances.Instance[].InstanceId"
              }
            }
          },
          {
            "Name": "runCommand",
            "Action": "ACS::ECS::RunCommand",
            "Description": "执行云助手命令",
            "Properties": {
              "instanceId": "{{ ACS::TaskLoopItem }}",
              "commandType": "RunShellScript",
              "commandContent": "#!/bin/bash\n\n##### 可在脚本开始运行时调用,打印当时的时间戳及PID。\nfunction job_start()\n{\n    now=`date +'%Y-%m-%d %H:%M:%S'`\n    echo \"[$now][$$] job_start\"\n}\n\n##### 如果返回值为0,则认为此执行成功,如果非0,则认为执行失败\njob_start",
              "regionId": "{{ ACS::RegionId }}",
              "workingDir": "/root",
              "enableParameter": false,
              "timeout": 600,
              "username": "",
              "parameters": {},
              "windowsPasswordName": ""
            },
            "Outputs": {
              "invocationOutput": {
                "Type": "String",
                "ValueSelector": "invocationOutput"
              }
            },
            "Loop": {
              "Items": "{{ getInstance.instanceIds }}",
              "RateControl": "{{ rateControl }}",
              "Outputs": {
                "invocationOutputs": {
                  "AggregateType": "Fn::ListJoin",
                  "AggregateField": "invocationOutput"
                }
              }
            }
          }
        ],
        "Outputs": {
          "invocationOutputs": {
            "Type": "List",
            "Value": "{{ runCommand.invocationOutputs }}"
          }
        }
      }
  4. (可选)获取ECS实例已经预置,如需修改可以在模板参数-targets部分进行配置。

    说明

    选择需要执行命令的 ECS 实例,在创建执行时填写相关信息。详情,请参见创建执行

    image

  5. 命令内容区域,编辑脚本内容,单击创建模板

    image

  6. 填写模板名称,单击完成创建image

相关文档

  • 创建模板后,需为其创建并启动执行,请参见创建执行

  • 如果您已充分了解模板中所有任务的风险,可选择无需确认模式直接启动执行,请参见全自动执行

  • 如果您想手动执行模板中的单个任务,可在创建执行时选择单步模式,请参见单步执行