The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jayant Kaushal
ServiceNow Employee
ServiceNow Employee

Python Script Execution in CMP: If there is a requirement to execute python scripts through resource operations, here is a step by step execution flow.

Create new CAPI Interface, Operation and Parameter : In order to expose specific operations, we will create a new interface, add a sample ExecuteScript operation and add 3 parameters as Param1, Param2,Param3 under that operation.

find_real_file.png

 

find_real_file.png

 

Create New Cloud API : Now by using this interface, we will create a new Cloud API for which we will set the script type as "Python".

find_real_file.png

 

 

Add your python script : You can copy your python scripts in the method mapper's  Request Script section, Paste content of script in the script section, All your Interface Parameters will be available as Environment variable which you can access through environment variable as os.environ['PARAMETER_NAME'], the API will return the output whichever is written in output stream, here we have just printed the output of getData as print statement, which will be available as standard output in your route results,  then this output can be used in the Resource response processors for processing in CMDB.

you can upload a directory/project as part of one operation and update as attachment.

find_real_file.png

 

Now you can wire this CAPI and operation as one of the resource step, We have created an Extension interface in Virtual Server to override the ExecuteScript operation for python execution, You can always create your own operation and wire them up in your orchestration.Generate and configure your catalog.

find_real_file.png

Passing Inputs to API , Look at Param1,Param2,Param3, these also can be passed from Catalog/Policy, As start, We will expose them in the portal and let user pass the values for the parameters.

find_real_file.png

A look at the Step which has been added as part of ExecuteScript.

find_real_file.png

 

As we have done this on Virtual Server, This operation can be invoked by going to Virtual Server on Cloud Portal and invoke this as a Day2.

Find your server and Operation,

find_real_file.png

 

 

Execute: 

find_real_file.png

 

Once the Execution finishes. you can go to Cloud API trail and Look at the route result.

 

find_real_file.png

You can click on route result and look at the output, notice route result will have stderror and stdout which will contain the script output like this. [{"stderr":"","exitcode":"0","stdout":"jayant kaushal","executedon":null,"response":null}]

 

find_real_file.png

 

Requirement: These scripts get executed on mid, so one basic requirement is to have python installed on the mid, you can also have multiple versions of python installed, CMP will execute the script against the python version which is pointing to /usr/bin/python on a Linux machines, the incase you need specific version, you will need to make sure that that specific has a symbolic link to this path,

For windows, required python version should be available at default 'python' command.

 

 

You can find update set for this implementation here. link to servicenow share

Comments
Jareth1
Tera Contributor

This is really helpful to get started with this process, do you have any idea how to work with "Config Overrides" to get the API credentials sent through?

Version history
Last update:
‎08-06-2019 01:41 AM
Updated by: