使用擴充性動作,可以將 Cloud Assembly 與企業 ITSM (例如 ServiceNow) 整合。

ServiceNow 整合流程經過幾個 Cloud Assembly、Amazon Web Services 以及 ServiceNow 服務和 API。
企業使用者通常會將其雲端管理平台與 IT 服務管理 (ITSM) 和設定管理資料庫 (CMDB) 平台整合以實現符合性。按照此範例,您可以使用擴充性動作指令碼將 Cloud Assembly 與適用於 CMDB 和 ITSM 的 ServiceNow 整合。
備註: 您也可以使用 vRealize Orchestrator 工作流程將 ServiceNow 與 Cloud Assembly 整合。如需有關使用工作流程整合 ServiceNow 的資訊,請參閱 如何使用 vRealize Orchestrator 工作流程將適用於 ITSM 的 Cloud Assembly 與 ServiceNow 整合

若要建立此整合,請使用四個擴充性動作指令碼。在佈建期間,前三個指令碼在發生計算資源佈建後事件時按順序起始。第四個指令碼在發生計算資源移除後事件時觸發。

如需有關事件主題的詳細資訊,請參閱Cloud Assembly 隨附的事件主題

四個擴充性動作指令碼具有不同的優先順序。「取得虛擬機器詳細資料」和「淘汰 ServiceNow CMDB CI」擴充性動作指令碼具有最高優先順序。

取得虛擬機器詳細資料

「取得虛擬機器詳細資料」指令碼可取得建立 CI 所需的其他裝載詳細資料,以及儲存在 Amazon Web Services Systems Manager 參數存放區 (SSM) 中的身分識別 Token。此外,此指令碼還可以使用其他內容更新 customProperties 以供日後使用。

建立 ServiceNow CMDB CI
「建立 ServiceNow CMDB CI」指令碼將 ServiceNow 執行個體 URL 做為輸入傳遞,並將執行個體儲存在 SSM 中以滿足安全性需求。此指令碼還會讀取 ServiceNow CMDB 唯一記錄識別碼回應 (sys_id)。此指令碼將回應做為輸出傳遞,並在建立期間寫入自訂內容 serviceNowSysId。此值用於在銷毀執行個體時將 CI 標記為已淘汰。
備註: 可能需要向 vRealize 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)

結果

Cloud Assembly 已成功與 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)

如需有關如何使用擴充性動作在 Cloud Assembly 中整合 ServiceNow 的詳細資訊,請參閱〈為 ServiceNow 整合使用基於動作的擴充性延伸 Cloud Assembly〉