Use common parameters in a task template

更新时间:
复制 MD 格式

If multiple task templates share a configuration value, you can store it in Parameter Store like a global variable. Reusing the value across templates improves operational efficiency and ensures consistency. This topic describes how to use common parameters in a task template.

Prerequisites

You have created a common parameter in Parameter Store. For more information, see Create a common parameter.

Procedure

Use the OOS console

This example uses a public task template to run a remote command. A common parameter named time_out with a value of 600 sets the template timeout to 600.

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Tasks > > Common Task Templates. Find the target template and click Create an execution.image

  3. Enter the basic information and click Next: Set Parameters.

  4. To the left of Timeout Period, click the image icon. Select the common parameter that you created (time_out in this example) to set the timeout to 600. Then, select the target instances and click Next.image

  5. Verify that the task information is correct and click Create.

  6. After you click Create, the execution details page appears. Verify that the value of the target field is correct.image

Use a parameter in a template

To use a common parameter in a template, reference it using the {{ oos:parameter_name }} format.

This example shows how to create a template that runs a Cloud Assistant command on an ECS instance. In this template, a common parameter named time_out with a value of 600 sets the timeout to 600.

  1. Log on to the CloudOps Orchestration Service console.

  2. In the left-side navigation pane, choose Automated Tasks > > Custom Task Templates and click Create Template. The following template shows how to reference the time_out common parameter. On line 98, the timeout parameter is set to '{{ oos:time_out }}'.

    FormatVersion: OOS-2019-06-01
    Description: Runs Cloud Assistant commands on multiple ECS instances in batches. This is useful for managing multiple ECS instances at once for tasks such as application management and resource tagging. During configuration, you must provide the following required parameters: Region ID (regionId) to specify the region of the target ECS instances, Resource Type (resourceType) to determine the target instance type, such as an ECS instance or a managed instance, and Target Instances (targets) to select the specific instances to operate on. Other parameters include Command Type (commandType) to specify the command type, such as a shell script or Python script, and Command Content (commandContent) for the specific command or script content. After the execution, the template returns the output of the batch operation, which allows you to verify the execution status.
    Parameters:
      regionId:
        Type: String
        Label: Region ID
        AssociationProperty: RegionId
        Default: '{{ ACS::RegionId }}'
      resourceType:
        Type: String
        Label: Resource Type
        AssociationPropertyMetadata:
          LocaleKey: TargetResourceType
        AllowedValues:
          - ALIYUN::ECS::Instance
          - ALIYUN::ECS::ManagedInstance
        Default: ALIYUN::ECS::Instance
      targets:
        Type: Json
        Label: Target Instances
        AssociationProperty: Targets
        AssociationPropertyMetadata:
          ResourceType: resourceType
          RegionId: regionId
          Status: Running
      commandType:
        Label: Command Type
        AssociationPropertyMetadata:
          LocaleKey: ECSCommandType
        Type: String
        AllowedValues:
          - RunShellScript
          - RunPythonScript
          - RunPerlScript
          - RunBatScript
          - RunPowerShellScript
        Default: RunShellScript
      commandContent:
        Label: Command Content
        Type: String
        MaxLength: 16384
        AssociationProperty: ALIYUN::OOS::Command::CommandContent
        AssociationPropertyMetadata:
          CommandType: ${commandType}
      workingDir:
        Description: 'For Linux instances, the default directory is the home directory of the administrator (root user), which is /root. For Windows instances, the default directory is where the Cloud Assistant client process is located, such as C:\Windows\System32. Make sure that the directory you enter is correct.'
        Label: Working Directory
        Type: String
        Default: ''
      enableParameter:
        Label: Specifies whether the command contains encrypted or custom parameters.
        Type: Boolean
        Default: false
      username:
        Description: The username for running the command on the ECS instance. The username cannot exceed 255 characters. On Linux instances, the command is run as the root user by default. On Windows instances, the command is run as the System user by default. Make sure that the username you enter is correct. <a href="https://www.alibabacloud.com/help/doc-detail/203771.html" target="_blank">Learn more</a>
        Label: Username for command execution
        Type: String
        Default: ''
      windowsPasswordName:
        Label: The name of the password for the user on a Windows instance.
        Type: String
        Default: ''
      rateControl:
        Label: The concurrency rate for the task execution.
        Type: Json
        AssociationProperty: RateControl
        Default:
          Mode: Concurrency
          MaxErrors: 0
          Concurrency: 10
      OOSAssumeRole:
        Label: The RAM role that OOS assumes.
        Type: String
        Default: ''
    RamRole: '{{ OOSAssumeRole }}'
    Tasks:
      - Name: getInstance
        Description: Obtains ECS instances.
        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: Runs the Cloud Assistant command.
        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. After you create the template, click Execute. Enter the required parameters and start the execution.

  4. Click the Run Cloud Assistant Command task, and then click Child Executions. Click an execution ID to view the details of the child execution.

  5. In the Execution Steps and Results section, click Input. You can see that the timeout parameter has been set to 600, the value of the time_out common parameter.