执行任务模板中使用普通参数

当多个执行模板存在相同的配置且需要赋相同的值,可以将配置值存储在参数仓库中(类似代码中的全局变量),然后在多个执行模板中复用,提升操作效率与执行一致性。本文为您介绍如何在执行模板中使用普通参数。

前提条件

已在参数仓库创建普通参数,更多信息,请参见创建普通参数

操作步骤

OOS控制台使用

本示例以发送远程命令公共任务模板为例,使用普通参数time_out(值为600)来设置模板超时时间为600。

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

  2. 在左侧导航栏选择 自动化任务> > 公共任务模板选择一个目标公共模板,点击创建执行image

  3. 填写好基本信息后点击下一步:设置参数

  4. 超时时间左侧单击image,选择已创建的普通参数(本文示例time_out)为超时时间赋值为500,之后选择需要执行的实例点击下一步。image

  5. 查看任务信息是否正确后,点击创建

  6. 单击创建后,进入执行详情页面,查看目标字段的值是否符合预期。image

在模板中使用

在模板中使用{{ oos:参数名 }}格式引用普通参数。

本示例以创建一个在ECS执行云助手命令的模板为例,使用普通参数time_out(值为600)来设置超时时间为600。

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

  2. 在左侧导航栏选择 自动化任务> > 自定义任务模板点击创建模板,模板内容如下。其中第98行,timeout参数使用'{{ oos:time_out }}'格式,表示引用普通参数time_out。

    FormatVersion: OOS-2019-06-01
    Description: 批量在多台ECS实例上运行云助手命令,适用于需要同时管理多台ECS实例的场景,如应用程序管理和资源标记操作等。在配置过程中,用户需要提供以下必填参数信息,地域ID(regionId):用于指定需要执行命令的目标ECS实例所在的区域,资源类型(resourceType):用于确定目标ECS实例的类型(如ECS实例或受管实例),目标实例(targets):用于选择要操作的具体实例列表。参数还包括命令类型(commandType):指定要执行的命令类型(如Shell脚本、Python脚本等),以及命令内容(commandContent):具体的命令或脚本内容。模板在执行后会返回批量操作的输出结果,方便用户验证命令执行的状态。
    Parameters:
      regionId:
        Type: String
        Label: 地域ID
        AssociationProperty: RegionId
        Default: '{{ ACS::RegionId }}'
      resourceType:
        Type: String
        Label: 资源类型
        AssociationPropertyMetadata:
          LocaleKey: TargetResourceType
        AllowedValues:
          - ALIYUN::ECS::Instance
          - ALIYUN::ECS::ManagedInstance
        Default: ALIYUN::ECS::Instance
      targets:
        Type: Json
        Label: 目标实例
        AssociationProperty: Targets
        AssociationPropertyMetadata:
          ResourceType: resourceType
          RegionId: regionId
          Status: Running
      commandType:
        Label: 命令类型
        AssociationPropertyMetadata:
          LocaleKey: ECSCommandType
        Type: String
        AllowedValues:
          - RunShellScript
          - RunPythonScript
          - RunPerlScript
          - RunBatScript
          - RunPowerShellScript
        Default: RunShellScript
      commandContent:
        Label: 命令内容
        Type: String
        MaxLength: 16384
        AssociationProperty: ALIYUN::OOS::Command::CommandContent
        AssociationPropertyMetadata:
          CommandType: ${commandType}
      workingDir:
        Description: Linux系统实例默认在管理员(root用户)的home目录下,即/root。Windows系统实例默认在云助手客户端进程所在目录,例如C:\Windows\System32。请确保您输入的目录是正确的。'
        Label: 运行目录
        Type: String
        Default: ''
      enableParameter:
        Label: 命令中是否包含加密参数或自定义参数
        Type: Boolean
        Default: false
      username:
        Description: 在ECS实例中执行命令的用户名称,长度不得超过255个字符。Linux系统的实例,默认以root用户执行命令。Windows系统的实例,默认以System用户执行命令。请确保您输入的用户名是正确的。<a href="https://www.alibabacloud.com/help/doc-detail/203771.html" target="_blank">了解更多</a>
        Label: 执行命令的用户名称
        Type: String
        Default: ''
      windowsPasswordName:
        Label: 在Windows实例中执行命令的用户的密码名称
        Type: String
        Default: ''
      rateControl:
        Label: 任务执行的并发比率
        Type: Json
        AssociationProperty: RateControl
        Default:
          Mode: Concurrency
          MaxErrors: 0
          Concurrency: 10
      OOSAssumeRole:
        Label: OOS扮演的RAM角色
        Type: String
        Default: ''
    RamRole: '{{ OOSAssumeRole }}'
    Tasks:
      - Name: getInstance
        Description: 获取ECS实例
        Action: ACS::SelectTargets
        Properties:
          ResourceType: '{{ resourceType }}'
          RegionId: '{{ regionId }}'
          Filters:
            - '{{ targets }}'
        Outputs:
          instanceIds:
            Type: List
            ValueSelector: Instances.Instance[].InstanceId
      - Name: runCommand
        Action: ACS::ECS::RunCommand
        Description: 执行云助手命令
        Properties:
          regionId: '{{ regionId }}'
          commandContent: '{{ commandContent }}'
          instanceId: '{{ ACS::TaskLoopItem }}'
          commandType: '{{ commandType }}'
          workingDir: '{{ workingDir }}'
          timeout: '{{ oos:time_out }}'
          enableParameter: '{{ enableParameter }}'
          username: '{{ username }}'
          windowsPasswordName: '{{ windowsPasswordName }}'
        Loop:
          RateControl: '{{ rateControl }}'
          Items: '{{ getInstance.instanceIds }}'
          Outputs:
            commandOutputs:
              AggregateType: Fn::ListJoin
              AggregateField: commandOutput
        Outputs:
          commandOutput:
            Type: String
            ValueSelector: invocationOutput
    Outputs:
      commandOutputs:
        Type: List
        Value: '{{ runCommand.commandOutputs }}'
  3. 创建模板后,点击执行。填入必要的参数,然后启动执行。

  4. 点击执行云助手命令任务->子执行,点击执行ID进入子执行。

  5. 在执行步骤和结果栏,点击输入可以看到超时时间,已被替换为time_out参数的值600。