Using SaltStack Config API (RaaS)

This blog post demonstrate how to use the SaltStack Config API together with vRealize Automation 8.6 and vRealize Orchestrator 8.6. The post provide a step by step instruction, but yoou can also download the vRO package callSalt (save the file as callSalt.package)

Prerequisites
  • Ubuntu 20.04
  • vRealize Automation with embedded vRealize Orchestrator installed and configured
  • Salt Stack Config installed and configured
Short introduction

The API (RaaS) refers to the application server that SaltStack Config clients connect to. These clients include the user interface component of SaltStack Config, properly configured Salt controllers (formerly called the Salt masters), and other users of RaaS. The application server is also known as the eAPI server.

RaaS is organized into modules (called “resources”) and functions (called “methods”). In the API call test.echo(‘hello, world’), the resource is test and the method is echo(). Arguments can be passed to methods by position or by keyword.

RaaS can be accessed in two ways: via an RPC client and via an HTTP (or HTTPS) bridge.

We will concentrate on RPC Client. The HTTP Bridge is out of scope in this post.

More information about the SaltStack Config API can be found here: Salt Stack Config API

The Environment is needed to create a ZIP File which contains all necessary Python Libraries and the vRO handler.py file.

sudo apt update
sudo apt install python3.8
sudo apt install python3-pip

Create a new directory which contains all files. 

cd /
sudo mkdir saltapi
cd /saltapi
sudo mkdir /lib

Download the Salt Stack Config under myVMware (a myVMware account is needed)

Navigate to Product and Accounts -> All Products and search for “saltstack”

Click the link above and download the file shown below

Unzip the file below

Navigate to the folder as shown below and extract the file SSEAPI……

Save the file in the created directory /saltapi and perform the commands below

python3 -m pip install SSEAPE-8.6.1.4-py2.py3-none-any.whl
python3 -m pip install six -t ./lib

Create a file called “callSalt.py”

sudo nano callSalt.py

Add the following code into the file

from sseapiclient import APIClient
import json
def handler(context, inputs):
        outputs = {}
        #connection info
        sec_host = inputs["saltHost"]
        username = inputs["username"]
        password = inputs["password"]
        validate_ssl = inputs["validateSSL"]
        a = json.loads(inputs["obj1"])
        b = json.loads(inputs["obj2"])
        c = json.loads(inputs["obj3"])
        d = json.loads(inputs["obj4"])
        e = json.loads(inputs["obj5"])
        f = json.loads(inputs["obj6"])
        g = json.loads(inputs["obj7"])
        h = json.loads(inputs["obj8"])
        i = json.loads(inputs["obj9"])
        j = json.loads(inputs["obj10"])
        command = inputs["command"]
        client = APIClient(sec_host, username, password, ssl_validate_cert=validate_ssl)
        k = 1
        if "." in command:
                tmp = command.split(".")
                cmd = getattr(client.api, tmp[0])
                for i in range(1,len(tmp)-1):
                        cmd = c(cmd, tmp[k])
                response = getattr(cmd, tmp[len(tmp)-1])(**a,**b,**c,**d,**e,**f,**g,**h,**i,**j)
                print(response)
                outputs["response"]=response
                return outputs

Save the file and create a ZIP file with the command below. Don´t forget the dot at the end.

sudo zip -r ~/callSalt.zip .

Copy the “callSalt.zip” File to the machine where you have access to the vRA Portal.

Create a new Action in vRO

Configure the action as shown below (Input, Return, etc). The import of the ZIP File takes some times.

Create a new Workflow “Call Salt Stack Config Server”
Add a scriptable task “Prepare Inputs” and define Inputs and Outputs

Prepare Input Script

if(parameters != null && parameters.length !=0){
    if(parameters.length > 10){
        throw("Maximal 10 parameters allowed.")
    }else{
        obj1 = getObj(parameters[0])
        obj2 = getObj(parameters[1])
        obj3 = getObj(parameters[2])
        obj4 = getObj(parameters[3])
        obj5 = getObj(parameters[4])
        obj6 = getObj(parameters[5])
        obj7 = getObj(parameters[6])
        obj8 = getObj(parameters[7])
        obj9 = getObj(parameters[8])
        obj10 = getObj(parameters[8])
    }
}
function getObj (parameter){
    if(parameter != null && parameter != ""){
        if(parameter.indexOf("=") != -1){
            var tmp = parameter.split("=")
            var key = tmp[0]
            if(key.indexOf('"') != -1){

            }else{
                key = '"' + key + '"'
            }
            var value = tmp[1]
            return '{' + key + ':' + value + '}'
        }else{
            throw('Parameters must be in format: parameter=parameter_value. eg. job_uuid="1234567890"')
        }
    }else{
        return "{}"
    }
}
Add the “callSalt” Action
Add a scriptable task “Get job ID” and define Inputs and Outputs

Get Job ID Script

if(response){
    jobId = response.response[1]
    System.warn("Job ID: " + jobId )
}

(Save the File as callSalt.package)

Similar Posts

Leave a Reply

Your email address will not be published.

WC Captcha + 35 = 44