本文以批量在ECS實例中執行命令為例,介紹如何在系統運維管理 OOS(CloudOps Orchestration Service)控制臺新建自定義模板。
登錄系統運維管理控制臺。
選擇
,單擊創建模板。使用示例模板創建或使用本地文件創建。
示例模板選擇批量執行命令,單擊下一步。
如果您已經預先編寫好批量執行命令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 實例,在創建執行時填寫相關信息。詳情,請參見創建執行。
在命令內容區域,編輯腳本內容,單擊創建模板。
填寫模板名稱,單擊完成創建。
相關文檔
文檔內容是否對您有幫助?