Managing IT systems like managing most things in life requires us to repeat certain tasks over and over again often at a certain interval. This is especially important as IT environments span clouds and include hundreds or thousands of systems. The execution of scheduled automation frees up IT personnel to focus high level tasks instead of babysitting a daily repeatable operation. Morpheus enables administrators to schedule Morpheus tasks and workflows to run on a defined schedule. In this blog post we’ll walk through scheduling automation in the Morpheus the platform. We’ll look at scheduling a Python script that runs daily to cleanup old AMIs (Amazon Machine Images) in an AWS account.
The Morpheus platform supports the execution of automation using Bash, PowerShell, Python, Ansible playbooks and other automation tools. In this case we’re creating a Python task to run a Python script that handles AMI cleanup in our AWS account. The following script is an example that fetches the AMIs in the AWS us-east-1 region and removes it if it was created more than 30 days ago.
import boto3 from tabulate import tabulate import datetime import time from morpheuscypher import Cypher # Expiration days expiration_days = 30 timeLimit = datetime.datetime.now() - datetime.timedelta(days=expiration_days) data =  # Fetch AWS account credentials from Cypher access_key=Cypher(morpheus=morpheus).get('secret/aws-access-key') secret_key=Cypher(morpheus=morpheus).get('secret/aws-secret-key') ec2 = boto3.resource('ec2', region_name='us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key) client = boto3.client('ec2', region_name='us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key) response = client.describe_images(Owners=['self']) # Iterate over the AMIs for ami in response['Images']: row =  row.append(ami['Name']) row.append(ami['ImageId']) # Convert the AMI creation date to a date format to compare with the current date creation_date_raw=ami['CreationDate'] creation_date_raw_1=tuple(creation_date_raw.split('T')) creation_date=creation_date_raw_1 creation_date_object = datetime.datetime.strptime(creation_date, '%Y-%m-%d').date() row.append(creation_date_object) if creation_date_object < timeLimit.date(): row.append(True) ec2.deregister_image(ImageId=ami['ImageId']) else: row.append(False) data.append(row) # Print the formatted table print(tabulate(data, headers=["Name", "ID", "CreationDate", "Expired"]))
Now that we’ve got our python code we’re ready to create our Python task that will run on a daily schedule. We’ve included the boto3, tabulate and morpheus-cypher python libraries as additional packages.
We need to create two secrets in Morpheus Cypher to securely the AWS credentials used in the python script.
Execution schedules in Morpheus provide a generic method for defining when something should be executed. Now that we have our task created we’ll create an execution schedule to define when we want our task to run. We can add the task by going to the Execute Scheduling section of the UI under Provisioning > Automation. Click Add to open the Create Execution Schedule modal to create a new execution schedule. We’ll provide a name and description for our schedule. Select your timezone, in this case the task should run at 7 AM every day Central Standard Time.
Jobs in Morpheus tie together an execution schedule with one of the three Morpheus job types detailed below.
Task Job: Scheduling of Morpheus tasks such as Ansible playbooks or bash scripts.
Workflow Job: Scheduling of Morpheus workflows that stitch together Morpheus tasks.
Security Scan Job: Scheduling of Open SCAP scans on workloads.
In our example we’ll use the task job type to schedule our previously created task.
Select the AWS AMI Cleanup task and the Daily AMI Cleanup execution schedule. The context type is None since we want the script to execute on the Morpheus node.
Review the job configuration and click COMPLETE to create the job.
Now that we’ve scheduled our automation we can view the job details such as the last run and next run. We can also trigger the execution of the job before the scheduled execution.
Try Morpheus Community Edition or Get a Demo
The Morpheus Community Edition lets you fully experience the Morpheus platform including nearly all features and capabilities! Register at Morpheus Hub and try it in your home lab or test environment today! Interested in learning more about Morpheus from one of our cloud transformation experts? Schedule a demo to walk through how Morpheus can help your organization here.