Chapter 7. Using the Nuage Networks events and alarms for automation

7.1. Creating an example automation workflow

In the following section we will setup an automation task that will log the events for all the Nuage Networks VSP events that do not have a specific workflow available.

7.1.1. Creating the Automate Class

We will start by setting up a new Automate Class with its own Method and Instance.

  1. Go to Automation > Automate > Explorer.
  2. Select Datastore.
  3. (Optional) If you do not have an Automate Domain yet, click on Configuration and select Add a New Domain.

    1. Enter a Name and Enable the Automate Domain.
    2. Click Add.
  4. Select your Automate Domain.
  5. (Optional) If you do not have a Namespace yet, click on Configuration and select Add a New Namespace.

    1. Enter a Name.
    2. Click Add.
  6. Select your Namespace.
  7. Click Configuration and select Add a New Class.
  8. Fill in the Name field.
  9. Click Add.
  10. Select your Class.
  11. Click the Schema tab.
  12. Click Configuration and select Edit selected Schema.
  13. Click the plus sign next to New Field.
  14. Enter "execute" as the Name.
  15. From the Type list, select "Method".
  16. From the Data Type list, select "String".
  17. Click the V sign to save the new field.
  18. Click Save to save the Schema changes.
  19. Select the Methods tab.
  20. Click Configuration and select Add a New Method.
  21. From the Type list, select "inline".
  22. Enter a Name.
  23. In the Data field add the following code:

    $evm.log(:info, "******** HANDLING NUAGE EVENT ********")
    $evm.log(:info, "EVENT DATA: #{$evm.root['event_stream']['full_data']}")
    $evm.log(:info, "**************************************")
  24. Click Add.
  25. Select the Instances tab.
  26. Click Configuration and select Add a New Instance.
  27. Enter a Name.
  28. In the (execute) field, enter the Name of your Method in the Value field.
  29. Click Add.

7.1.2. Attaching the Automate Class to Nuage Networks VSP Events

This part will make sure that the Automate Class created in the previous section is executed when an event comes in that does not have a specific assignment already.

  1. Open the Domain, Namespace, Automate Class and Instance ManageIQ (Locked) > System > Event > EmsEvent > Nuage > .missing.
  2. Click Configuration and select Copy this Instance.
  3. From the To Domain list, select your Domain.
  4. Check Copy to same path.
  5. Click Copy. This will create a .missing instance in your Domain, in the System > Event > EmsEvent > Nuage Class.
  6. Select your private .missing Instance in your Domain.
  7. Click Configuration and select Edit this Instance.
  8. In the (rel1) Value field, enter the path to the Automate Instance you created in the previous section. For instance, if the Namespace you created is Automation, the Class you created is LogInfo and the Instance is logging, you would enter /Automation/LogInfo/logging in this field.
  9. Click Save.

7.1.3. Seeing the Automation in action

To see the automation in action, follow these steps:

  1. Go to Automation > Automate > Log.
  2. In your Nuage Networks VSP environment, take an action, for instance: create or delete a redirection target.
  3. Click the refresh button.
  4. In the log, you will see something similar as the following appear:

    [----] I, [2018-03-07T03:40:45.609931 #24569:559eebc]  INFO -- : <AEMethod logmissing> ******** HANDLING NUAGE EVENT ********
    [----] I, [2018-03-07T03:40:45.618829 #24569:559eebc]  INFO -- : <AEMethod logmissing> EVENT DATA: {"userName"=>"csproot", "enterpriseName"=>"csp", "type"=>"DELETE", "entityType"=>"redirectiontarget", "entityVersion"=>nil, "assoicatedEvent"=>false, "eventReceivedTime"=>1520390432802, "entities"=>[{"children"=>nil, "parentType"=>"domain", "entityScope"=>"ENTERPRISE", "lastUpdatedBy"=>"8a6f0e20-a4db-4878-ad84-9cc61756cd5e", "lastUpdatedDate"=>1520390262000, "creationDate"=>1520390262000, "virtualNetworkID"=>"8988894", "name"=>"Demo Redirection Target", "description"=>nil, "endPointType"=>"L3", "redundancyEnabled"=>false, "triggerType"=>"NONE", "destinationType"=>"REDIRECTION_TARGET", "owner"=>"8a6f0e20-a4db-4878-ad84-9cc61756cd5e", "ID"=>"29a4905d-5a5d-480d-b5f7-8db549814b1a", "parentID"=>"d6a213c7-6595-4dab-b7df-669c86ee53a5", "externalID"=>nil, "ESI"=>"0xFFFFFF00000000002710", "templateID"=>nil}], "diffMap"=>nil, "ignoreDiffInMediationEvents"=>false, "updateMechanism"=>"DEFAULT", "requestID"=>"f6440c04-ebf5-423f-a95c-b385ac1eb1d8", "sourceEnterpriseID"=>"4e68985d-54e8-46e7-b541-16b98193ea00"}
    [----] I, [2018-03-07T03:40:45.620338 #24569:559eebc]  INFO -- : <AEMethod logmissing> **************************************

Note You might need to wait a few seconds before doing the refresh.

7.2. Using Embedded Ansible to run Automation tasks

Red Hat CloudForms comes with the Nuage Ansible module which can be used to manage any Nuage Networks VSP environment and supports all features and Nuage Networks VSP API capabilities.

This Nuage Ansible module can be used from within the Red Hat CloudForms Automation framework. This chapter will go in more detail of how to set this up and demonstrate this automation.

7.2.1. Enabling Embedded Ansible

Before you can use the Nuage Ansible module and the Ansible Automation inside Red Hat CloudForms, you need to enable the Embedded Ansible functionality. To do this, follow these steps:

  1. Log in as an Administrator in Red Hat CloudForms.
  2. Open the Red Hat CloudForms configuration.
  3. Select a Red Hat CloudForms server.
  4. In the Server Control > Server Roles make sure the Embedded Ansible switch is toggled to On.
  5. Click the Save button.

More details can be found in the Red Hat CloudForms documentation in the Introduction to Embedded Ansible chapter.

7.2.2. Create a Git Ansible playbook repository

Once you have enabled Embedded Ansible, the next step is to setup a Git Ansible playbook repository that will contain the playbooks you want to use in your automation tasks.

Use your preferred Git environment to setup a repository and make sure that repository is reachable by the Red Hat CloudForms appliance so it can clone the repository locally.

7.2.3. Create an Ansible playbook

Inside the Git repository, you will need to have one or more Ansible playbooks. These playbooks contain tasks that need to be executed in order when they are called.

A very simple playbook that is designed to automatically be executed by the Red Hat CloudForms Automation framework when a certain Nuage event comes in, could be:

printout.yaml

---
- name: Nuage event callback
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    manageiq_validate_certs: False
  pre_tasks:
  - debug: msg="Running printout"
  roles:
  - role: xlab_si.nuage_miq_automate
    validate_certs: False
  tasks:
  - debug: msg="I am a playbook running as event callback and I am able to access nuage credentials"
  - debug: var=nuage_auth
  - debug: msg="As well as details of the event that triggered me"
  - debug: var=event
  - debug: msg="So I'm now ready to do some work!"

This playbook will capture the event and print out some information about the event in the automation log file.

The important part to note in this playbook is that it includes a role called xlab_si.nuage_miq_automate. This role is provided by Nuage Networks to support the Ansible integration. It’s purpose is to get the necessary information from Red Hat CloudForms and provide it as Ansible facts to the other tasks in the playbook.

A more advanced playbook, using the Nuage Ansible module, looks like:

printout-and-fetch.yaml

---
- name: Nuage event callback
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    manageiq_validate_certs: False
  pre_tasks:
  - debug: msg="Running printout with fetch entity"
  roles:
  - xlab_si.nuage_miq_automate
  tasks:
  - debug: msg="I am a playbook running as event callback and I am able to access nuage credentials"
  - debug: var=nuage_auth
  - debug: msg="As well as details of the event that triggered me"
  - debug: var=event
  - debug: msg="So I'm now ready to do some work!"

  - name: Fetch entity from Nuage
    nuage_vspk:
      auth: "{{ nuage_auth }}"
      type: "{{ event.entity_type | capitalize }}"
      command: find
      id: "{{ event.entity.ID }}"
    register: entity
    when: event.type == 'CREATE' or event.type == 'UPDATE'
  - debug: var=entity

In this playbook, the Nuage VSPK Ansible module is used to fetch the data from the Nuage environment on the entity on which the event happened.

More examples, and details on the xlab_si.nuage_miq_automate role, can be found in our Example GitHub repository.

7.2.4. Adding a Ansible playbook repository to Red Hat CloudForms

After enabling the Embedded Ansible functionality and creating your playbooks, the next step is to add the Ansible playbook repository to Red Hat CloudForms. This can be done following these steps:

  1. In Red Hat CloudForms, open Automation > Ansible > Repositories.
  2. Click Configuration > Add New Repository.
  3. Provide a Name, select the SCM type to GIT and fill in the URL to your Git repository. If you have specific credentials that are needed for the repository, or a specific branch, please provide that information too.
  4. Click Add.

This will make the Red Hat CloudForms appliance clone the repository locally. Once that has been completed, you can check the Automation > Ansible > Playbooks page to see your playbooks showing up.

7.2.5. Setting up the automation

Now that we have the playbooks available in Red Hat CloudForms, you can use them in your Automation.

First, you will need to create an Automate domain:

  1. Go to Automation > Automate > Explorer.
  2. Select Datastore.
  3. Click on Configuration and select Add a New Domain.
  4. Enter a Name and Enable the Automate Domain.
  5. Click Add.

Using this Automate Domain, you can either configure your own set of Namespaces, Classes and instances; or you can copy the existing examples and modify them to your needs. The following section describes the copy method:

  1. Select an example Automate Instance in the ManageIQ (Locked) > System > Event > EmsEvent > Nuage Automate Class.
  2. Click Configuration > Copy this instance.
  3. Select your new Automate Domain and where you want to copy it to.

Now that you have a copied Automate Instance, you can update that class with your own Nuage Authentication information by following these steps:

  1. Select the Automate Class to which your new Automate Instance belongs.
  2. Select the Schema tab.
  3. Click Configuration > Edit this Class.
  4. Fill in the nuage_username, nuage_password, nuage_enterprise and nuage_url with the appropriate information of your environment.
  5. Click Save to save the information.

Setup the playbook that needs to be executed:

  1. Select the Method tab on the Automate Class.
  2. Click Configuration > Add New Method.
  3. Select the Type to be playbook.
  4. Provide a Name.
  5. Select your Ansible repository.
  6. Select the Ansible playbook.
  7. Select the Machine Credentials as CFME Default Credential.
  8. If your Red Hat CloudForms server and/or the Nuage VSD is using self-signed certificates or untrusted certificates, add an Input Parameter called manageiq_validate_certs with a default value False.
  9. Click Add.

The last step is to add the method to the Automate Instance:

  1. Select the previously created Automate Instance.
  2. Click Configuration > Edit this Instance.
  3. In one of the (methX) fields, put the name of the Method you just created.
  4. Click Save.

This will setup an automated event handler that will execute the playbook when an event comes in with the name of the Automate Instance that you copied.

In case you want to execute a playbook based on an event which has not been previously defined, you can create a new Automate Instance with the name of the event on which you want to execute the playbook.

For instance, if you want to make sure a playbook runs every time a Nuage Job is updated, you can create an Automate Instance with the name nuage_job_update.