ALIYUN::SWAS::RunCommand

更新时间:
复制为 MD 格式

ALIYUN::SWAS::RunCommand类型用于在轻量应用服务器上执行命令。

语法

{
  "Type": "ALIYUN::SWAS::RunCommand",
  "Properties": {
    "CommandContent": String,
    "InstanceId": String,
    "Name": String,
    "Type": String,
    "ContentEncoding": String,
    "EnableParameter": Boolean,
    "Parameters": Map,
    "Sync": Boolean,
    "Timeout": Integer,
    "WorkingDir": String,
    "WorkingUser": String,
    "WindowsPasswordName": String
  }
}

属性

属性名称

类型

必须

允许更新

描述

约束

CommandContent

String

命令内容。

您需要注意:

  • 指定参数EnableParameter=true可在命令内容中启用自定义参数功能:

  • 用{{}}包含的方式定义自定义参数,在{{}}内参数名前后的空格以及换行符会被忽略。

  • 自定义参数个数不能超过 20 个。

  • 自定义参数名允许 a-zA-Z0-9-_的组合,不支持其余字符,参数名不区分大小写。

  • 单个自定义参数名不能超过 64 字节。

InstanceId

String

轻量应用云服务器实例 ID。

Name

String

执行命令名称。

Type

String

命令的语言类型。

取值范围:

  • RunBatScript:适用于 Windows 实例的 Bat 命令。

  • RunPowerShellScript:适用于 Windows 实例的 PowerShell 命令。

  • RunShellScript:适用于 Linux 实例 Shell 命令。

ContentEncoding

String

命令内容的编码模式。

取值:

PlainText:命令内容未经过编码,以明文形式传输。(默认值)

Base64:进行 base64 编码。

EnableParameter

Boolean

命令中是否包含自定义参数。

默认值:false。

Parameters

Map

命令中包含自定义参数时,执行命令时传入的自定义参数的键值对。

例如,命令内容为echo {{name}},则可以通过Parameters参数传入键值对{"name":"Jack"}。自定义参数将自动替换变量值name,得到一条新的命令,实际执行的是echo Jack

自定义参数的个数范围为 0~20,且您需要注意:

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

  • 值允许为空字符串。

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

  • 设置的自定义参数名集合必须为创建命令时定义的参数集的子集。对于未传入的参数,您可以使用空字符串代替。

默认值为空,表示取消设置该参数从而禁用自定义参数。

Sync

Boolean

是否同步运行命令。

Timeout

Integer

设置命令在服务器实例中的超时时间,

单位为秒。

当执行命令的任务超时后,命令助手将强制终止任务进程。可设定超时时间取值范围为 10~86400 秒(24 小时)。

默认值:60。

WorkingDir

String

您可自定义命令的执行路径。

默认路径如下:

  • Linux 实例:执行路径默认在管理员 root 用户的/root 目录下。

  • Windows 实例:执行路径默认在 C:\Windows\system32 目录下。

WorkingUser

String

您可以指定以某个轻量应用服务器实例中存在的用户执行命令,

建议使用普通用户执行命令可降低安全风险。 默认值:

  • root:Linux 实例中默认以 root 用户执行命令。

  • system:Windows 实例中默认以 system 用户执行命令。

WindowsPasswordName

String

Windows 实例中执行命令的用户的密码名称。

当您希望以非默认用户(System)在 Windows 实例中执行命令时,需要同时传入 WorkingUser 和该参数。为降低密码泄露的风险,需要将密码明文托管在运维编排服务的参数仓库中,此处仅传入密码的名称。

返回值

Fn::GetAtt

  • InvokeResult:命令执行的结果。

  • InvokeId:命令执行 ID。

示例

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  CommandContent:
    AssociationProperty: TextArea
    Type: String
    Description:
      en: The plaintext content of the command to run.
    Required: true
  Type:
    Type: String
    Description:
      en: |-
        The language type of the O&M script. Valid values:
        RunShellScript: shell scripts for Linux instances
        RunBatScript: batch scripts for Windows instances
        RunPowerShellScript: PowerShell scripts for Windows instances
    AllowedValues:
      - RunShellScript
      - RunBatScript
      - RunPowerShellScript
    Required: true
  InstanceId:
    Type: String
    Description:
      en: The ID of the SWAS instance on which to run the command. The instance must be in 'Running' status.
    Required: true
  Name:
    Type: String
    Description:
      en: The name of the command to run.
    Required: true
Resources:
  RunCommand:
    Type: ALIYUN::SWAS::RunCommand
    Properties:
      CommandContent:
        Ref: CommandContent
      Type:
        Ref: Type
      InstanceId:
        Ref: InstanceId
      Name:
        Ref: Name
Outputs:
  InvokeResult:
    Description: The result of the command execution.
    Value:
      Fn::GetAtt:
        - RunCommand
        - InvokeResult
  InvokeId:
    Description: The ID of the command execution.
    Value:
      Fn::GetAtt:
        - RunCommand
        - InvokeId
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "CommandContent": {
      "AssociationProperty": "TextArea",
      "Type": "String",
      "Description": {
        "en": "The plaintext content of the command to run."
      },
      "Required": true
    },
    "Type": {
      "Type": "String",
      "Description": {
        "en": "The language type of the O&M script. Valid values:\nRunShellScript: shell scripts for Linux instances\nRunBatScript: batch scripts for Windows instances\nRunPowerShellScript: PowerShell scripts for Windows instances"
      },
      "AllowedValues": [
        "RunShellScript",
        "RunBatScript",
        "RunPowerShellScript"
      ],
      "Required": true
    },
    "InstanceId": {
      "Type": "String",
      "Description": {
        "en": "The ID of the SWAS instance on which to run the command. The instance must be in 'Running' status."
      },
      "Required": true
    },
    "Name": {
      "Type": "String",
      "Description": {
        "en": "The name of the command to run."
      },
      "Required": true
    }
  },
  "Resources": {
    "RunCommand": {
      "Type": "ALIYUN::SWAS::RunCommand",
      "Properties": {
        "CommandContent": {
          "Ref": "CommandContent"
        },
        "Type": {
          "Ref": "Type"
        },
        "InstanceId": {
          "Ref": "InstanceId"
        },
        "Name": {
          "Ref": "Name"
        }
      }
    }
  },
  "Outputs": {
    "InvokeResult": {
      "Description": "The result of the command execution.",
      "Value": {
        "Fn::GetAtt": [
          "RunCommand",
          "InvokeResult"
        ]
      }
    },
    "InvokeId": {
      "Description": "The ID of the command execution.",
      "Value": {
        "Fn::GetAtt": [
          "RunCommand",
          "InvokeId"
        ]
      }
    }
  }
}