Run Ansible during provisioning

Run an Ansible Playbook during VM provisioning

With the integration of Ansible into CloudForms, we want to make it easier for customers and partners to modify out of the box behavior and make it easy to integrate with third party solutions.

In this part of the lab, we want to run an Ansible Playbook during VM provisioning to show how to modify the State Machine and how we can use Ansible Playbooks.

Create a new Automate Domain

Since the Automate Domains shipped with CloudForms are read only, we have to create our own Domain first.

  1. Navigate to Automation -> Automate

    Navigate to Automate

  2. Click on Configuration -> Add a new Domain

    add new automate domain

  3. Enter the following details:

    Name: Lab

    Description: Lab Domain

    Enabled: Check

    add new lab domain

  4. Click Add

Copy VM Provisioning State Machine

To be able to make changes to the State Machine, we have to copy it to our writeable Domain first.

  1. Navigate to the VM Provisioning State Machine:

    ManageIQ -> Infrastructure -> VM -> Provisioning -> StateMachines -> VMProvision_VM -> Provision VM from Template (template)

    navigate to provision from template

  2. Click on Configuration -> Copy this Instance

    copy provision from template instance

    :warning: WARNING Make sure you highlight the “Provision VM from Template (template)” instance when initiating the copy!

  3. Accept the defaults when confirming the copy

    copy prevision from

  4. Click Copy to confirm

  5. After the copy was completed, you should see a confirmation page

    confirmation of copy

Copy CheckProvisioned Method

The out of the box code of CloudForms will assume a VM was properly created, when it was successfully copied. Since we want to run an Ansible Playbook inside the deployed Virtual Machine, we have to add an additional check. The State Machine should only continue if the VM has an IP address assigned.

  1. We also need a copy of the CheckProvisioned method, to do that, navigate to ManageIQ -> Infrastructure -> VM -> Provisioning -> StateMachines -> Methods -> CheckProvisioned (check_provisioned)

    navigate to check provisioned

  2. Click on Configuration -> Copy this Method

    copy checkprovisioned Method

  3. Keep the settings unmodified and click on Copy

    copy checkprovisioned Method details

  4. After the copy was created, you should see a confirmation message

    copy checkprovisioned confirmation

  5. Click on Configuration -> Edit this Method

    edit check provisioned method

  6. Add the end of the code, add the following snippet:

    vm = task.vm
    $evm.log("info","Current IP Addresses: #{vm.ipaddresses}") unless vm.nil?
    $evm.log("info", "VM is still nil") if vm.nil?
    
    if not vm.nil?
      if task.destination.ipaddresses.empty?
        $evm.root['ae_result']         = 'retry'
        $evm.root['ae_retry_interval'] = '1.minute'
      end
    end
    

    This code will go into an additional retry, if the Virtual Machine does not have an IP address (yet).

  7. The method should look like this now.

    updated check provisioned method

  8. Click on Validate to perform a basic syntax check and Save if no errors were found.

Create the Ansible Playbook Method

Starting with CloudForms 4.6 we can create Methods of type “Playbook” which, instead of running Ruby code, execute an Ansible Playbook.

  1. Click on the Methods Class and then the Methods tab in the right part of the window

    navigate to methods class

  2. Create a new Method to run an Ansible Playbook. Click on Configuration -> Add a new Method

    add new Ansible method

  3. Switch the Method Type to “Playbook”

    select playbook method type

  4. Use the following details to fill out the form.

    Name: install_package

    Display Name: Install Package

    Repository: GitLab

    Playbook: playbooks/InstallPackage.yml

    Machine Credentials: Virtual Machine Credentials

    Hosts: Specify host values and enter the following string into the text field:

    ${/#miq_provision.destination.ipaddresses.first}
    

    :warning: WARNING Pay attention to the special characters or the Playbook will not be executed on the correct Virtual Machine! The expression will configure CloudForms to use the first IP addresses of the provisioned Virtual Machine as the limit parameter when executing the Playbook.

    Max TTL (mins): 30

  5. Enter the following Input Parameters:

    Input Name: package_name

    Default Value: httpd

    :warning: WARNING Don’t forget to click on the blue plus (+) icon to save the Input Parameter

  6. Click Add to create the Ansible Playbook Method

    create ansible playbook method

    create ansible playbook method

  7. To be able to call the Method from a StateMachine, we need an associated Instance. Click on the Instances tab and Configuration -> Add a new Instance

    add new playbook instance

  8. Enter the following details into the Dialog:

    Name: install_package

    Display Name: Install Package

    Fields: In the table search the row “execute” and put “install_package” into the “value” field

    add execute value

  9. Click Add to save the Instance

Modify the Schema

To run the Ansible Playbook during Virtual Machine Provisioning, we have to add an additional state to the State Machine.

  1. Click on the VMProvision_VM class

    click on VMProvision_VM class

  2. Switch to the Schema tab of the page

    edit provision class schema

  3. Click on Configuration -> Edit selected Schema

    edit selected schema

  4. Add a new row with the following details:

    Name: InstallPackage

    Type: State

    Data Type: String

    Default Value: /Infrastructure/VM/Provisioning/StateMachines/Methods/install_package

  5. Click the little plus symbol at the beginning of the row to apply the change

  6. Click Save to save the new Schema

  7. After the change was applied, the updated Schema should look like this:

    after updating schema
    :heavy_check_mark: NOTE There is a new line “InstallPackage” at the bottom of the table

    :warning: WARNING Do not try to continue with the next step, if you do not see the new “InstallPackage” line.

Change Schema Sequence

To execute this new state after the Virtual Machine was created, we have to change the sequence.

  1. Click on Configuration -> Edit Sequence

    edit sequence of VM provisioning

  2. Use the arrow buttons to move the line “InstallPackage” between “CheckProvisioned” and “PostProvision”.

    modify InstallPackage state in sequence

  3. Click Save to apply the Schema sequence change

Test the updated Virtual Machine Provisioning State Machine

We want to verify the applied changed by deploying a Virtual Machine and check if httpd was actually installed properly.

  1. Navigate to Compute -> Infrastructure -> Virtual Machines

    navigate to infrastructure virtual machines

  2. Click on Lifecycle -> Provision VMs

    provision VMs

  3. Select the “RHEL7” template

    select rhel7 template

  4. On the Requests tab the email address is the only required field. Enter your email address and optionally enter some data into the other fields as well

    provisioning details

  5. Switch to the Catalog tab and specify a VM name

    :warning: *WARNING Make sure to not use an existing name or provisioning will fail. You can use “changeme” and CloudForms will automatically assign a name to the Virtual Machine.

    provisioning catalog

  6. Switch to the Environment tab and check the following fields:

    Choose Automatically: not selected

    Datacenter: Default

    Cluster: Default

    Host: rhvh1

    Datastore: data

    provisioning environment

  7. Click Submit to place your request

  8. You will be redirected to the Requests page. You can use the Refresh button on the top of the page, since it does not automatically reload, to watch your request progressing.

  9. In this lab environment, the Virtual Machine is not accessible directly from the internet. Choose one or more of the following options to verify the Playbook did execute

    1. Open a remote Console: Navigate to the Virtual Machine you just ordered and click on Access -> VM Access

      :heavy_check_mark: NOTE If you do this the first time, your Web Browser might block the Popup Window!

    2. Perform Smart State Analysis: Navigate to the Virtual Machine you just ordered and click on Configuration -> Perform SmartState Analysis. The action can take a few minutes to complete. After it finished, you should see the list of installed packages and can verify httpd is on the list

    3. Check the log files: First you will have to SSH into the workstation and from there you can log into CloudForms. You should find the hostnames in the RHPDS details. The Ansible logs can be found in /var/lib/awx/job_status.