ALIYUN::ECS::RunCommand类型用于在一台或多台ECS实例中执行一段Shell、PowerShell或者Bat类型的脚本。

语法

{
  "Type": "ALIYUN::ECS::RunCommand",
  "Properties": {
    "Parameters": Map,
    "Description": String,
    "Timeout": Integer,
    "Timed": Boolean,
    "ContentEncoding": String,
    "Name": String,
    "WorkingDir": String,
    "CommandContent": String,
    "Type": String,
    "Frequency": String,
    "EnableParameter": Boolean,
    "InstanceIds": List,
    "KeepCommand": Boolean
  }
}

属性

属性名称 类型 必须 允许更新 描述 约束
Parameters Map 脚本中包含自定义参数时,执行脚本时指定的自定义参数的键值对。例如:脚本内容为echo {{name}},则可以通过Parameters参数指定键值对{"name":"Jack"}。自定义参数将自动替换变量值name,得到一条新的脚本,实际执行的是echo Jack。 最多支持10个自定义参数。

键不允许为空字符串,最多支持64个字符。 值允许为空字符串。

自定义参数与原始脚本内容在Base64编码后,综合长度不能超过16KB。

设置的自定义参数名集合必须为创建脚本时定义的参数集的子集。

对于未指定的参数,您可以使用空字符串代替。
Description String 脚本描述。 支持全字符集,长度不超过512个字符。
Timeout Integer 执行脚本的超时时间。 单位:秒。

默认值:60。

当因为进程原因、缺失模块、缺失云助手客户端等原因无法运行脚本时,会出现超时现象。超时后,会强制终止脚本进程。
Timed Boolean 是否周期执行脚本。 取值:
  • true:根据参数Frequency设置的时间频率定时执行。上次的执行结果不对下一次执行产生任何影响。
  • false(默认值):只执行一次。
ContentEncoding String 脚本内容(CommandContent)的编码方式。 取值:
  • PlainText(默认值):不编码,采用明文传输。
    说明 如果填写了其他值,则取值为PlainText。
  • Base64:Base64编码。
Name String 脚本名称。 支持全字符集,长度不超过128个字符。
WorkingDir String 脚本在ECS实例中的运行目录。 默认值:
  • Linux实例:/root
  • Windows实例:C:\Windows\System32\
CommandContent String 脚本的明文内容或者Base64编码后的内容。 脚本内容Base64编码后不能超过16KB。

EnableParameter取值为true时,可以在脚本内容中启用自定义参数功能:自定义参数写作两个大括号({{}})中,在两个大括号({{}})内参数名前后的空格以及换行符会被忽略。

自定义参数个数不能超过20个。自定义参数名可包含英文字母、数字、短划线(-)和下划线(_)。 单个自定义参数名不能超过64个字符。
Type String 运维脚本的语言类型。 取值:
  • RunBatScript:适用于Windows实例的Bat脚本。
  • RunPowerShellScript:适用于Windows实例的PowerShell脚本。
  • RunShellScript:适用于Linux实例的Shell脚本 。
Frequency String 周期任务的执行周期。 当参数Timed取值为true时,参数Frequency为必选参数。两次周期任务的时间间隔不能低于10秒。 该参数取值详情请参见Cron表达式
EnableParameter Boolean 脚本中是否包含自定义参数。 取值:
  • true
  • false(默认值)
InstanceIds List ECS实例ID列表。 最多指定20台ECS实例,且ECS实例的状态必须是运行中。
KeepCommand Boolean 执行完该脚本后是否保留。 取值:
  • true:保留。可以通过InvokeCommand再次执行。会占用云助手脚本的保有量配额。
  • false(默认值):不保留,执行完成后自动删除,不占用云助手脚本的保有量配额。

返回值

Fn::GetAtt

  • CommandId:脚本ID。
  • InvokeId:脚本执行ID。

示例

JSON格式

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "Parameters": {
      "Type": "Json",
      "Description": "The key-value pairs of custom parameters passed in when the script contains custom parameters.\nNumber of custom parameters: 0 to 10.\nThe key cannot be an empty string. It can be up to 64 characters in length.\nThe value can be an empty string.\nAfter the custom parameters and the original script content are Base64 encoded, the total size cannot exceed 16 KB.\nThe set of custom parameter names must be a subset of the parameter set that is defined when you created the script. You can use an empty string to represent the parameters that are not passed in.\nDefault value: null, indicating that this parameter is canceled and customer parameters are disabled.",
      "MaxLength": 10
    },
    "Description": {
      "Type": "String",
      "Description": "The description of the script, which supports all character sets. It can be up to 512 characters in length."
    },
    "Timeout": {
      "Type": "Number",
      "Description": "The timeout period for script execution. Unit: seconds. A timeout error occurs when a script cannot be run because the process slows down, a specific module or the Cloud Assistant client does not exist. When the script times out, the script process is forcibly terminated.\nDefault value: 60."
    },
    "Timed": {
      "Type": "Boolean",
      "Description": "Specifies whether to periodically run the script. Valid values:\ntrue: runs the script on a regular basis based on the value set for the Frequency parameter. The result of the previous execution task does not affect the next execution task.\nfalse: runs once only.\nDefault value: false",
      "AllowedValues": [
        "true",
        "false"
      ]
    },
    "ContentEncoding": {
      "Type": "String",
      "Description": "The encoding mode of script content (CommandContent). Valid values (case insensitive):\nPlainText: The script content is not encoded, and transmitted in plaintext.\nBase64: base64-encoded.\nDefault value: PlainText. If the specified value of this parameter is invalid, PlainText is used by default."
    },
    "Name": {
      "Type": "String",
      "Description": "The name of the script, which supports all character sets. It can be up to 128 characters in length."
    },
    "WorkingDir": {
      "Type": "String",
      "Description": "The running directory of the script in the ECS instance.\nDefault value:\nLinux instances: under the home directory of the administrator (root user): /root.\nWindows instances: under the directory where the process of the Cloud Assistant client is located, such as C:\\ProgramData\\aliyun\\assist\\$(version)."
    },
    "CommandContent": {
      "Type": "String",
      "Description": "The plaintext content or the Base64-encoded content of the script. The Base64-encoded script content cannot exceed 16 KB.\nYou can enable the custom parameter function by setting EnableParameter=true in the script content:\nDefine custom parameters in the {{}} format. Within {{}}, the spaces and line breaks before and after the name of the parameter are ignored.\nThe number of custom parameters cannot exceed 20.\nA custom parameter name can contain only letters, digits, underscores (_), and hyphens (-). It is case insensitive.\nEach custom parameter key cannot exceed 64 bytes."
    },
    "Type": {
      "Type": "String",
      "Description": "The language type of the OM script. Valid values:\nRunBatScript: batch scripts for Windows instances\nRunPowerShellScript: PowerShell scripts for Windows instances\nRunShellScript: shell scripts for Linux instances"
    },
    "Frequency": {
      "Type": "String",
      "Description": "The execution period of recurring tasks. If the Timed parameter is set to True, you must specify the Frequency parameter. The interval between two recurring tasks cannot be less than 10 seconds.\nThe parameter value follows the cron expression. For more information, see Configure scheduled commands."
    },
    "EnableParameter": {
      "Type": "Boolean",
      "Description": "Specifies whether the script contains custom parameters.\nDefault value: false",
      "AllowedValues": [
        "true",
        "false"
      ]
    },
    "InstanceIds": {
      "Type": "Json",
      "Description": "The instance id list. Select up to 20 instances at a time.Instances status must be running.",
      "MinLength": 1,
      "MaxLength": 20
    },
    "KeepCommand": {
      "Type": "Boolean",
      "Description": "Specifies whether to retain the script after it is run. Valid values:\ntrue: The script is retained. You can call the InvokeCommand operation to run the script again, call the DescribeCommands operation to query the script, and call the DeleteCommands operation to delete the script. The retained script takes up the quota of Cloud Assistant scripts.\nfalse: The script is not retained. It is automatically deleted after running, without taking up the quota of Cloud Assistant scripts.\nDefault value: false",
      "AllowedValues": [
        "true",
        "false"
      ]
    }
  },
  "Resources": {
    "RunCommand": {
      "Type": "ALIYUN::ECS::RunCommand",
      "Properties": {
        "Parameters": {
          "Ref": "Parameters"
        },
        "Description": {
          "Ref": "Description"
        },
        "Timeout": {
          "Ref": "Timeout"
        },
        "Timed": {
          "Ref": "Timed"
        },
        "ContentEncoding": {
          "Ref": "ContentEncoding"
        },
        "Name": {
          "Ref": "Name"
        },
        "WorkingDir": {
          "Ref": "WorkingDir"
        },
        "CommandContent": {
          "Ref": "CommandContent"
        },
        "Type": {
          "Ref": "Type"
        },
        "Frequency": {
          "Ref": "Frequency"
        },
        "EnableParameter": {
          "Ref": "EnableParameter"
        },
        "InstanceIds": {
          "Ref": "InstanceIds"
        },
        "KeepCommand": {
          "Ref": "KeepCommand"
        }
      }
    }
  },
  "Outputs": {
    "CommandId": {
      "Description": "The id of command created.",
      "Value": {
        "Fn::GetAtt": [
          "RunCommand",
          "CommandId"
        ]
      }
    },
    "InvokeId": {
      "Description": "The invoke id of command.",
      "Value": {
        "Fn::GetAtt": [
          "RunCommand",
          "InvokeId"
        ]
      }
    }
  }
}

YAML格式

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  Parameters:
    Type: Json
    Description: >-
      The key-value pairs of custom parameters passed in when the script
      contains custom parameters.

      Number of custom parameters: 0 to 10.

      The key cannot be an empty string. It can be up to 64 characters in
      length.

      The value can be an empty string.

      After the custom parameters and the original script content are Base64
      encoded, the total size cannot exceed 16 KB.

      The set of custom parameter names must be a subset of the parameter set
      that is defined when you created the script. You can use an empty string
      to represent the parameters that are not passed in.

      Default value: null, indicating that this parameter is canceled and
      customer parameters are disabled.
    MaxLength: 10
  Description:
    Type: String
    Description: >-
      The description of the script, which supports all character sets. It can
      be up to 512 characters in length.
  Timeout:
    Type: Number
    Description: >-
      The timeout period for script execution. Unit: seconds. A timeout error
      occurs when a script cannot be run because the process slows down, a
      specific module or the Cloud Assistant client does not exist. When the
      script times out, the script process is forcibly terminated.

      Default value: 60.
  Timed:
    Type: Boolean
    Description: >-
      Specifies whether to periodically run the script. Valid values:

      true: runs the script on a regular basis based on the value set for the
      Frequency parameter. The result of the previous execution task does not
      affect the next execution task.

      false: runs once only.

      Default value: false
    AllowedValues:
      - 'true'
      - 'false'
  ContentEncoding:
    Type: String
    Description: >-
      The encoding mode of script content (CommandContent). Valid values (case
      insensitive):

      PlainText: The script content is not encoded, and transmitted in
      plaintext.

      Base64: base64-encoded.

      Default value: PlainText. If the specified value of this parameter is
      invalid, PlainText is used by default.
  Name:
    Type: String
    Description: >-
      The name of the script, which supports all character sets. It can be up to
      128 characters in length.
  WorkingDir:
    Type: String
    Description: >-
      The running directory of the script in the ECS instance.

      Default value:

      Linux instances: under the home directory of the administrator (root
      user): /root.

      Windows instances: under the directory where the process of the Cloud
      Assistant client is located, such as
      C:\ProgramData\aliyun\assist\$(version).
  CommandContent:
    Type: String
    Description: >-
      The plaintext content or the Base64-encoded content of the script. The
      Base64-encoded script content cannot exceed 16 KB.

      You can enable the custom parameter function by setting
      EnableParameter=true in the script content:

      Define custom parameters in the {{}} format. Within {{}}, the spaces and
      line breaks before and after the name of the parameter are ignored.

      The number of custom parameters cannot exceed 20.

      A custom parameter name can contain only letters, digits, underscores (_),
      and hyphens (-). It is case insensitive.

      Each custom parameter key cannot exceed 64 bytes.
  Type:
    Type: String
    Description: |-
      The language type of the OM script. Valid values:
      RunBatScript: batch scripts for Windows instances
      RunPowerShellScript: PowerShell scripts for Windows instances
      RunShellScript: shell scripts for Linux instances
  Frequency:
    Type: String
    Description: >-
      The execution period of recurring tasks. If the Timed parameter is set to
      True, you must specify the Frequency parameter. The interval between two
      recurring tasks cannot be less than 10 seconds.

      The parameter value follows the cron expression. For more information, see
      Configure scheduled commands.
  EnableParameter:
    Type: Boolean
    Description: |-
      Specifies whether the script contains custom parameters.
      Default value: false
    AllowedValues:
      - 'true'
      - 'false'
  InstanceIds:
    Type: Json
    Description: >-
      The instance id list. Select up to 20 instances at a time.Instances status
      must be running.
    MinLength: 1
    MaxLength: 20
  KeepCommand:
    Type: Boolean
    Description: >-
      Specifies whether to retain the script after it is run. Valid values:

      true: The script is retained. You can call the InvokeCommand operation to
      run the script again, call the DescribeCommands operation to query the
      script, and call the DeleteCommands operation to delete the script. The
      retained script takes up the quota of Cloud Assistant scripts.

      false: The script is not retained. It is automatically deleted after
      running, without taking up the quota of Cloud Assistant scripts.

      Default value: false
    AllowedValues:
      - 'true'
      - 'false'
Resources:
  RunCommand:
    Type: 'ALIYUN::ECS::RunCommand'
    Properties:
      Parameters:
        Ref: Parameters
      Description:
        Ref: Description
      Timeout:
        Ref: Timeout
      Timed:
        Ref: Timed
      ContentEncoding:
        Ref: ContentEncoding
      Name:
        Ref: Name
      WorkingDir:
        Ref: WorkingDir
      CommandContent:
        Ref: CommandContent
      Type:
        Ref: Type
      Frequency:
        Ref: Frequency
      EnableParameter:
        Ref: EnableParameter
      InstanceIds:
        Ref: InstanceIds
      KeepCommand:
        Ref: KeepCommand
Outputs:
  CommandId:
    Description: The id of command created.
    Value:
      'Fn::GetAtt':
        - RunCommand
        - CommandId
  InvokeId:
    Description: The invoke id of command.
    Value:
      'Fn::GetAtt':
        - RunCommand
        - InvokeId