Let’s jump right in and start writing our first automation script. In time-honored fashion we’ll write “Hello, World!” to the Automate Engine logfile.
Before we do anything, we need to ensure that the Automation Engine server role is selected on our CloudForms appliance. We do this from the Configure → Configuration menu, selecting the CloudForms server in the Settings accordion.
The Automation Engine server role is now enabled by default in CloudForms 4.1, but it’s still worthwhile to check that this role is set on our CloudForms appliance.
Setting the Automation Engine server role
Setting the Automation Engine role is necessary to be able to run queued Automate tasks. Automate actions initiated directly from the WebUI—such as running instances from Simulation, or processing methods to populate dynamic dialogs—are run on the WebUI appliance itself, regardless of whether it has the Automation Engine role enabled.
Before we create our first automation script, we need to put some things in place. We’ll begin by adding a new domain called ACME. We’ll add all of our automation code into this new domain.
Adding a new domain
ACME Domain
Adding a new namespace
General namespace
Naming a class Methods may seem somewhat confusing, but many of the generic classes in the ManageIQ and RedHat domains in the Automate Datastore are called Methods to signify their general-purpose nature.
Adding a new class
Methods class
Editing the schema
Adding a new schema field
Save schema field
$evm.log(:info, "Hello, World!")
exit MIQ_OK
Adding a new instance to our class
Entering the instance details
Adding a new method to our class
Entering the method details
Get into the habit of using the Validate button; it can save you a lot of time catching Ruby syntactical typos when you develop more complex scripts.
Automate log file
The CloudForms WebUI uses browser session cookies, so if we want two or more concurrent login sessions (particularly as different users), it helps to use different web browsers or private/incognito windows.
Alternatively, ssh into the CloudForms appliance as root and enter:
tail -f /var/www/miq/vmdb/log/automation.log
In the simulation, we actually run an instance called Call_Instance in the /System/Request/ namespace of the ManageIQ domain, and this in turn calls our HelloWorld instance using the namespace, class, and instance attribute/value pairs that we pass to it.
Completing the Simulation details
If all went well, we should see our “Hello, World!” message appear in the automation.log file:
Invoking [inline] method [/ACME/General/Methods/hello_world] with inputs [{}]
<AEMethod [/ACME/General/Methods/hello_world]> Starting
<AEMethod hello_world> Hello, World!
<AEMethod [/ACME/General/Methods/hello_world]> Ending
Method exited with rc=MIQ_OK
Or, like so from a private browser session:
“Hello World!” log message
Success!
In our example we used an exit status code of MIQ_OK. Although with simple methods such as this we don’t strictly need to specify an exit code, it’s good practice to do so. When we build more advanced multi-method classes and state machines, an exit code can signal an error condition to the Automation Engine so that action can be taken.
There are four exit codes that we can use:
MIQ_OK (0)
Continues normal processing. This is logged to automation.log as:
Method exited with rc=MIQ_OK
MIQ_WARN(4)
Warning message, continues processing. This is logged to automation.log as:
Method exited with rc=MIQ_WARN
MIQ_ERROR / MIQ_STOP (8)
Stops processing current object. This is logged to automation.log as:
Stopping instantiation because [Method exited with rc=MIQ_STOP]
MIQ_ABORT (16)
Aborts entire automation instantiation. This is logged to automation.log as:
Aborting instantiation because [Method exited with rc=MIQ_ABORT]
The difference between MIQ_STOP and MIQ_ABORT is subtle but comes into play as we develop more advanced Automate workflows.
MIQ_STOP stops the currently running instance, but if this instance was called via a reference from another “parent” instance, the subsequent steps in the parent instance would still complete.
MIQ_ABORT stops the currently running instance and any parent instance that called it, thereby terminating the Automate task altogether.
In this exercise we’ve seen how simple it is to create our own domain, namespace, class, instance, and method, and run our script from Simulation. These are the fundamental techniques that we use for all of automation scripts!