本文以批量在ECS執行個體中執行命令為例,介紹如何在CloudOps Orchestration Service (OOS)控制台建立自訂模板。
選擇自動化任務 > 自訂工作範本,單擊建立模板。
使用樣本模板建立或使用本地檔案建立。
樣本模板選擇批量執行命令,單擊下一步。
如果您已經預先編寫好批量執行命令YAML或JSON格式的模板檔案,您可以選擇使用本地檔案建立,上傳後,單擊下一步。
批量執行命令模板內容
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 }}" } } }
(可選)擷取ECS執行個體已經預置,如需修改可以在模板參數-targets部分進行配置。
說明選擇需要執行命令的 ECS 執行個體,在建立執行時填寫相關資訊。詳情,請參見建立執行。
在命令內容地區,編輯指令碼內容,單擊建立模板。
填寫模板名稱,單擊完成建立。