Save-Write an Attribute to a Virtual Machine

I making a call to a CMDB for each VM that is provisioned. When I make this API call, the record provides a response structure back, with a sys_id field that can be used as a “coat check receipt” to go back later and refer to the record you just created. This is used, apparently, because the hostname and IP addresses are subject to change and can not be a unique identifier. I would like to save this sys_id as part of the Morpheus JSON, so that when the VM is deprovisioned, we can use this sys_id field to pull the CMDB record and mark it for retirement or deletion.

In Python, how can I save some new information, some new metadata, like a name value pair, to the VMs JSON?

Could use the update instance or update server/host API endpoints.

import requests

url = "https://FQDN/api/instances/1234"

payload = {"instance": {"tags": [
            {
                "name": "sys_id",
                "value": "1234535"
            }
        ]}}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": "Bearer 123456789"
}

response = requests.put(url, json=payload, headers=headers)

print(response.text)

Basically push another tag to the system with an additional task that follows your CMDB script, and then you can ref that tag in your teardown scripts.

1 Like

Also another note on your CMDB script. If you can write everything out to null so only the sys_id data is outputted to stdout (or whatever amount of text/body you can parse) then you can pass this into another script easily.

Set code/result type in the CMDB task and consume in your next to update the instance:

This is a clean way to do it, but I was trying not to manage to API handles (authenticating to two APIs, et al).

‘<%=morpheus.apiAccessToken%>’
‘<%=morpheus.applianceUrl%>’ will be your friend :wink: Though language consumed may matter. I don’t recall if that is readily available in Python.

This is a Python script. So if I set the result type to Name Value Pairs, how is this result type mapped from the Python script? Is it Standard Output? I have to echo “just” the sys_id=xxxxx or sys_id : xxxxxx? Or is there some other superior way to actually map that so that the sys_id gets returned and set?

I think I was having some issues with the ‘<%=xxxxxx%>’ Javascript tags in Python. Although I can certainly try to re-test that.

It’s there, I think it’s these in Python.

host = morpheus["morpheus"]["applianceHost"]
token = "Bearer %s" %(morpheus["morpheus"]["apiAccessToken"])
2 Likes

Just saw this. Very cool. Let me try that out.

Do you know how to get the Instance ID?

morpheus[“instance”][“id”]

https://docs.morpheusdata.com/en/latest/troubleshooting/Variables_Examples.html