使用可扩展性操作,可以将 Automation Assembler 与企业 ITSM(例如 ServiceNow)集成。

ServiceNow 集成流经过几个 Automation Assembler、Amazon Web Services 以及 ServiceNow 服务和 API。
企业用户通常将其云计算管理平台与 IT 服务管理 (IT Service Management, ITSM) 和配置管理数据库 (Configuration Management Database, CMDB) 平台集成以实现合规性。按照此示例,可以使用可扩展性操作脚本将 Automation Assembler 与适用于 CMDB 和 ITSM 的 ServiceNow 集成。
注: 还可以使用 Automation Orchestrator 工作流将 ServiceNow 与 Automation Assembler 集成。有关使用工作流集成 ServiceNow 的信息,请参见 如何使用 Automation Orchestrator 工作流将 Automation Assembler 与 ServiceNow 集成以符合 ITSM

要创建此集成,需要使用四个可扩展性操作脚本。在置备期间,前三个脚本在发生计算资源置备后事件时按顺序启动。第四个脚本在发生计算资源移除后事件时触发。

有关事件主题的详细信息,请参阅随 Automation Assembler 提供的事件主题

四个可扩展性操作脚本具有不同的优先级。“获取虚拟机详细信息”和“注销 ServiceNow CMDB CI”可扩展性操作脚本具有最高优先级。

获取虚拟机详细信息

“获取虚拟机详细信息”脚本可获取创建 CI 所需的其他负载详细信息,以及存储在 Amazon Web Services Systems Manager 参数存储 (SSM) 中的标识令牌。此外,此脚本还可以使用其他属性更新 customProperties 供以后使用。

创建 ServiceNow CMDB CI
“创建 ServiceNow CMDB CI”脚本将 ServiceNow 实例 URL 作为输入传递,并将实例存储在 SSM 中以满足安全要求。此脚本还会读取 ServiceNow CMDB 唯一记录标识符响应 (sys_id)。此脚本将该响应作为输出传递,并在创建期间写入自定义属性 serviceNowSysId。此值用于在销毁实例时将 CI 标记为已注销。
注: 可能需要向 VMware Aria Automation services Amazon Web Services 角色分配其他权限,以允许 Lambda 访问 SSM 参数存储。

创建 ServiceNow 变更

此脚本通过将 ServiceNow 实例 URL 作为输入传递,并将 ServiceNow 凭据存储在 SSM 中以满足安全要求,完成 ITSM 集成。

创建 ServiceNow 变更

“注销 ServiceNow CMDB CI”脚本根据在创建脚本中创建的自定义属性 serviceNowSysId 提示 ServiceNow 停止并将 CI 标记为已注销。

前提条件

  • 配置此集成之前,筛选具有条件云模板属性的所有事件订阅:event.data["customProperties"]["enable_servicenow"] === "true"
    注: 此属性存在于需要 ServiceNow 集成的云模板上。
  • 下载并安装 Python。

有关筛选订阅的详细信息,请参见创建可扩展性订阅

过程

  1. 从虚拟机打开命令提示符。
  2. 运行“获取虚拟机详细信息”脚本。
    from botocore.vendored import requests
    import json
    import boto3
    client = boto3.client('ssm','ap-southeast-2')
    
    def handler(context, inputs):
        baseUri = inputs['url']
        casToken = client.get_parameter(Name="casToken",WithDecryption=True)
        
        url = baseUri + "/iaas/login"
        headers = {"Accept":"application/json","Content-Type":"application/json"}
        payload = {"refreshToken":casToken['Parameter']['Value']}
        
        results = requests.post(url,json=payload,headers=headers)
        
        bearer = "Bearer "
        bearer = bearer + results.json()["token"]
        
        deploymentId = inputs['deploymentId']
        resourceId = inputs['resourceIds'][0]
        
        print("deploymentId: "+ deploymentId)
        print("resourceId:" + resourceId)
        
        machineUri = baseUri + "/iaas/machines/" + resourceId
        headers = {"Accept":"application/json","Content-Type":"application/json", "Authorization":bearer }
        resultMachine = requests.get(machineUri,headers=headers)
        print("machine: " + resultMachine.text)
        
        print( "serviceNowCPUCount: "+ json.loads(resultMachine.text)["customProperties"]["cpuCount"] )
        print( "serviceNowMemoryInMB: "+ json.loads(resultMachine.text)["customProperties"]["memoryInMB"] )
        
        #update customProperties
        outputs = {}
        outputs['customProperties'] = inputs['customProperties']
        outputs['customProperties']['serviceNowCPUCount'] = int(json.loads(resultMachine.text)["customProperties"]["cpuCount"])
        outputs['customProperties']['serviceNowMemoryInMB'] = json.loads(resultMachine.text)["customProperties"]["memoryInMB"]
        return outputs
  3. 运行 CMDB 配置项创建操作。
    from botocore.vendored import requests
    import json
    import boto3
    client = boto3.client('ssm','ap-southeast-2')
    
    def handler(context, inputs):
    
        snowUser = client.get_parameter(Name="serviceNowUserName",WithDecryption=False)
        snowPass = client.get_parameter(Name="serviceNowPassword",WithDecryption=True)
        table_name = "cmdb_ci_vmware_instance"
        url = "https://" + inputs['instanceUrl'] + "/api/now/table/{0}".format(table_name)
        headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
        payload = {
            'name': inputs['customProperties']['serviceNowHostname'],
            'cpus': int(inputs['customProperties']['serviceNowCPUCount']),
            'memory': inputs['customProperties']['serviceNowMemoryInMB'],
            'correlation_id': inputs['deploymentId'],
            'disks_size': int(inputs['customProperties']['provisionGB']),
            'location': "Sydney",
            'vcenter_uuid': inputs['customProperties']['vcUuid'],
            'state': 'On',
            'sys_created_by': inputs['__metadata']['userName'],
            'owned_by': inputs['__metadata']['userName']
            }
        results = requests.post(
            url,
            json=payload,
            headers=headers,
            auth=(snowUser['Parameter']['Value'], snowPass['Parameter']['Value'])
        )
        print(results.text)
    
        #parse response for the sys_id of CMDB CI reference
        if json.loads(results.text)['result']:
            serviceNowResponse = json.loads(results.text)['result']
            serviceNowSysId = serviceNowResponse['sys_id']
            print(serviceNowSysId)
    
            #update the serviceNowSysId customProperty
            outputs = {}
            outputs['customProperties'] = inputs['customProperties']
            outputs['customProperties']['serviceNowSysId'] = serviceNowSysId;
            return outputs
  4. 运行创建操作脚本。
    from botocore.vendored import requests
    import json
    import boto3
    client = boto3.client('ssm','ap-southeast-2')
    
    def handler(context, inputs):
        snowUser = client.get_parameter(Name="serviceNowUserName",WithDecryption=False)
        snowPass = client.get_parameter(Name="serviceNowPassword",WithDecryption=True)
        table_name = "change_request"
        url = "https://" + inputs['instanceUrl'] + "/api/now/table/{0}".format(table_name)
        headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
        payload = {
            'short_description': 'Provision CAS VM Instance'      
                  }
        results = requests.post(
            url,
            json=payload,
            headers=headers,
            auth=(snowUser['Parameter']['Value'], snowPass['Parameter']['Value'])
        )
        print(results.text)

结果

Automation Assembler 已成功与 ITSM ServiceNow 集成。

下一步做什么

如果需要,可以使用 CMDB 配置项注销操作注销 CI:
from botocore.vendored import requests
import json
import boto3
client = boto3.client('ssm','ap-southeast-2')

def handler(context, inputs):
    snowUser = client.get_parameter(Name="serviceNowUserName",WithDecryption=False)
    snowPass = client.get_parameter(Name="serviceNowPassword",WithDecryption=True)
    tableName = "cmdb_ci_vmware_instance"
    sys_id =inputs['customProperties']['serviceNowSysId']
    url = "https://" + inputs['instanceUrl'] + "/api/now/"+tableName+"/{0}".format(sys_id)
    headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
    payload = {
        'state': 'Retired'
        }

    results = requests.put(
        url,
        json=payload,
        headers=headers,
        auth=(inputs['username'], inputs['password'])
    )
    print(results.text)