本文以批量在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 实例,在创建执行时填写相关信息。详情,请参见创建执行。
在命令内容区域,编辑脚本内容,单击创建模板。
填写模板名称,单击完成创建。