全部產品
Search
文件中心

CloudOps Orchestration Service:建立模板

更新時間:Nov 26, 2024

本文以批量在ECS執行個體中執行命令為例,介紹如何在CloudOps Orchestration Service (OOS)控制台建立自訂模板。

  1. 登入CloudOps Orchestration Service控制台

  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

相關文檔

  • 建立模板後,需為其建立並啟動執行,請參見建立執行

  • 如果您已充分瞭解模板中所有任務的風險,可選擇無需確認模式直接啟動執行,請參見全自動執行

  • 如果您想手動執行模板中的單個任務,可在建立執行時選擇單步模式,請參見逐步執行