用途

该动作可以用在运维任务完成后用户需要被告知的场景。如在购买ECS实例完成以后,发送一个钉钉通知便于您对账单有实时的了解。

语法

当前支持Webhook和Mail两种通知类型,定义方式如下。

Webhook

  • YAML格式
Tasks:
  - Name: notifyTask
    Action: ACS::Notify
    Properties:
      NotifyType: WebHook
	  AppendExecutionLink: 'false' # 是否附加执行详情链接,可选项true和false。ACS::Notify动作默认为false。
      WebHook:
        URI: url # 必填,webhook地址,如https://oapi.dingtalk.com/robot/send?access_token=xxxxxx
        Headers: # 可选,Http请求的Headers,如Content-Type
          Content-Type: 'application/json; charset=utf-8'
        Content: # 必填,根据具体的Webhook要求提供,如钉钉webhook要求如下:https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1 
          msgtype: text
          text: 
            content: 'this is a dingtalk message with a parameter {{ p1 }}'
  • JSON格式(请参照YAML注释说明)
{
  "Tasks": [
    {
      "Name": "notifyTask",
      "Action": "ACS::Notify",
      "Properties": {
        "NotifyType": "WebHook",
        "AppendExecutionLink": "false",
        "WebHook": {
          "URI": "url",
          "Headers": {
            "Content-Type": "application/json; charset=utf-8"
          },
          "Content": {
            "msgtype": "text",
            "text": {
              "content": "this is a dingtalk message with a parameter {{ p1 }}"
            }
          }
        }
      }
    }
  ]
}

Mail

  • YAML格式
Tasks:
  - Action: ACS::Notify
    Name: testNotifyByMail
    Properties:
        NotifyType: Mail
        Mail:
        	Host: SMTPHostAddress # SMTP 服务器主机地址如 smtp.example1.com
            Port: SMTPPort # SMTP 服务器端口如 465
            Username: sender  # 代发送的邮箱用户如 usr001@example1.com
            Password: senderPassword # 代发送的邮箱密码如 123456zxcv
            Subject: mailSubject # 邮件主题如 hello world
            Body: mailBody # 邮件正文如 hello world !!!
            From: senderAddress # 邮件来自如 usr001@example1.com
            To:  receiverAddresses	# 收件人邮箱地址列表如 [usr1234@example2.com,usr123@example2.com]
  • JSON格式(请参照YAML注释说明)
{
  "Tasks": [
    {
      "Action": "ACS::Notify",
      "Name": "testNotifyByMail",
      "Properties": {
        "NotifyType": "Mail",
        "Mail": {
          "Host": "SMTPHostAddress",
          "Port": "SMTPPort",
          "Username": "sender",
          "Password": "senderPassword",
          "Subject": "mailSubject",
          "Body": "mailBody",
          "From": "senderAddress",
          "To": "receiverAddresses"
        }
      }
    }
  ]
}

示例

  • YAML格式
---
FormatVersion: OOS-2019-06-01
Outputs:
    InstanceIds:
        Type: List
        Value: ' {{ runInstances.InstanceIds }} '
Parameters:
    ImageId:
        Description: the image id for the new instances.
        Type: String
    InstanceType:
        Description: the Instance Type for the new instances.
        Type: String
    OOSAssumeRole:
        Default: OOSServiceRole
        Description: oos assume this role to execution task
        Type: String
    SecurityGroupId:
        Description: the security group id for the new instances.
        Type: String
    VSwitchId:
        Description: the virtual switch id for the new instances.
        Type: String
RamRole: '{{OOSAssumeRole}}'
Tasks:
-   Action: ACS::ExecuteAPI
    Description: 'run instances with specified parameters, refer them here: https://help.aliyun.com/document_detail/63440.html'
    Name: runInstances
    Outputs:
        InstanceIds:
            Type: List
            ValueSelector: .InstanceIdSets.InstanceIdSet[]
    Properties:
        API: RunInstances
        Parameters:
            ImageId: '{{ ImageId }}'
            InstanceType: '{{ InstanceType }}'
            SecurityGroupId: '{{ SecurityGroupId }}'
            VSwitchId: '{{ VSwitchId }}'
        Service: ECS
-   Action: ACS::Notify
    Name: test_notify
    Properties:
        NotifyType: WebHook
        WebHook:
            Content:
                msgtype: text
                text: 
                	content: 
            Headers:
                Content-Type: application/json
            URI: https://oapi.dingtalk.com/robot/send?access_token=your_access_token
  • JSON格式
{
    "FormatVersion": "OOS-2019-06-01",
    "Outputs": {
        "InstanceIds": {
            "Type": "List"
            "Value": " {{ runInstances.InstanceIds }} "
        }
    },
    "Parameters": {
        "ImageId": {
            "Description": "the image id for the new instances.",
            "Type": "String"
        },
        "InstanceType": {
            "Description": "the Instance Type for the new instances.",
            "Type": "String"
        },
        "SecurityGroupId": {
            "Description": "the security group id for the new instances.",
            "Type": "String"
        },
        "VSwitchId": {
            "Description": "the virtual switch id for the new instances.",
            "Type": "String"
        },
          "OOSAssumeRole": {
      		"Type": "String",
      		"Description":  "oos assume this role to execution task",
      		"Default": "OOSServiceRole"
    }
    },
    "RamRole": "{{OOSAssumeRole}}",
    "Tasks": [
        {
            "Action": "ACS::ExecuteAPI",
            "Description": "run instances with specified parameters, refer them here: https://help.aliyun.com/document_detail/63440.html",
            "Name": "runInstances",
            "Outputs": {
                "InstanceIds": {
                    "Type": "List",
                    "ValueSelector": ".InstanceIdSets.InstanceIdSet[]"
                }
            },
            "Properties": {
                "API": "RunInstances",
                "Parameters": {
                    "ImageId": "{{ ImageId }}",
                    "InstanceType": "{{ InstanceType }}",
                    "SecurityGroupId": "{{ SecurityGroupId }}",
                    "VSwitchId": "{{ VSwitchId }}"
                },
                "Service": "ECS"
            }
        },
       {
      "Name": "test_notify",
      "Action": "ACS::Notify",
      "Properties": {
        "NotifyType": "WebHook",
        "WebHook": {
          "URI": "https://oapi.dingtalk.com/robot/send?access_token=your_access_token",
          "Headers": {
            "Content-Type": "application/json"
          },
          "Content": {
            "msgtype": "text",
            "text": {
              "content":"test"
            }
          }
        }
      }
    } 
    ]
}