Installing Morpheus Agent on multiple discovered VMs

In this post, I want to share a script I created that automates the process of converting a list of servers to “managed” and installing the Morpheus Agent on them using the Morpheus API.

#!/bin/bash

# Parse JSON content and extract server IDs
data=$(curl -k --request GET \
     --url '<%= morpheus.applianceUrl %>/api/servers?zoneId=3&managed=false&powerState=on&max=50' \
     --header 'accept: application/json' \
     --header 'authorization: Bearer <%= morpheus.apiAccessToken %>' | python -c "import sys, json; print('\n'.join([str(server['id']) for server in json.load(sys.stdin)['servers']]))")

# loop through the server list and install the Morpheus Agent for each server
for id in $data; do
    curl -k --request PUT \
         --url "<%= morpheus.applianceUrl %>/api/servers/$id/make-managed" \
         --header 'accept: application/json' \
         --header 'authorization: Bearer <%= morpheus.apiAccessToken %>' \
         --header 'content-type: application/json' \
         --data '
                {
                  "server": {
                    "sshUsername": "mihai",
                    "sshPassword": "<%=cypher.read('password/aws-vms-pass')%>"
                  },
                  "installAgent": true
                }
        '
done

The script sends an HTTP GET request to the Morpheus API endpoint to retrieve a list of servers that match a specific set of criteria. The filters used in the script are:

  • zoneId=3: This parameter filters the list of servers based on Cloud ID, which is set to 3 in this case.
  • managed=false: This parameter filters the list of servers to exclude any servers that are already “managed”.
  • powerState=on: This parameter filters the list of servers to include only servers that are powered on.
  • max=50: This parameter limits the maximum number of servers that are returned by the API to 50.

These criteria can be adjusted as needed to filter the list of servers based on different conditions.

Once the script receives the JSON response from the API, it parses the data to extract the server IDs. It then loops through the list of server IDs and sends an HTTP PUT request to the API to make each server “managed” and install the Morpheus Agent on it. The script uses the curl command to send HTTP requests and the Python command to parse JSON data, so you’ll need to have these two installed on your system.

As for the SSH credentials, I have created an entry in the Morpheus Cypher module to securely store the password and referenced it in my script.

Hope it helps.

3 Likes