Creating a Dynamic Inventory for OpenShift Virtual Machines

Lab Overview

Dynamic inventories allow Ansible Automation Platform (AAP) to automatically fetch and update the inventory of systems from external sources, eliminating the need for manual inventory management.

In this lab, we will configure a dynamic inventory to pull data from OpenShift Virtualization. This will enable AAP to manage OpenShift VMs residing in the vms-app-day2 namespace of an OCP cluster.

Create an Inventory

  1. Navigate to Automation Execution → Infrastructure → Inventories.

  2. Select the Create inventory button.

  3. In the Create Inventory form:

    • Provide a name: OpenShift Virtual Machines.

    • Select Default as the Organization.

  4. Click the Create inventory button.

    create inventory
    Figure 1. Create Inventory

Add a Source to the Inventory

  1. After creating the inventory, switch to the Sources tab.

    sources
    Figure 2. Sources
  2. Select the Create source button.

  3. In the Create Source form:

    • Provide a name: OpenShift Virtual Machines Source.

    • Select OpenShift Virtualization as the Source type.

    • Choose Default Execution Environment as the execution environment.

    • Add the OpenShift Credential (Bearer Token was pre-created)

    • Select Update on Launch checkbox within Options.

    • Set Cache timeout to 0 seconds

  4. Click the Create source button to save the configuration.

    create inventory source
    Figure 3. Create Inventory Source

Update the Inventory

  1. Launch the inventory update:

    • Click the Launch Inventory Update button in the top-right corner.

      update inventory
      Figure 4. Update Inventory
  2. Wait for the Last Job Status to show Success.

Verify the Inventory

  1. Once the update completes successfully:

    • Click the Back to Inventory Sources button.

    • Switch to the Hosts tab.

  2. Confirm that the Virtual Machines from the vms-app-day2 namespace of your OpenShift cluster are listed as inventory hosts.

    verify hosts
    Figure 5. Verify Hosts

Test Connectivity of the Virtual Machines

In this lab, there will be three virtual machines running within your OpenShift environment.

Ensure that you can ping those VMs within the AAP dashboard https://aap-aap.apps.cluster-guid.dynamic.redhatworkshops.io as follows:

  1. Login to the AAP Dashboard using the credentials above.

  2. Navigate to Automation Execution → Infrastructure → Inventories

    • Select the OpenShift Virtual Machines Inventory

    • Within the Details page of the OpenShift Virtual Machines Inventory, select the Hosts tab.

  3. Select the 3 VMs and click the Run command button

    • Within the Run command Details, select the ping module and click Next.

    • Within the Run command Execution Environment, Select the Day2 EE execution environment and click Next.

    • Within the Run command Credential, select the pre-created Workshop Credential and click Next.

    • Within the Run command Review, select Finish.

  4. Verify Results: You should see a list of VMs along with their statuses similar to:

    vms-aap-day2-rhel9-vm1 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": false,
        "ping": "pong"
    }
    vms-aap-day2-rhel9-vm2 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": false,
        "ping": "pong"
    }
    vms-aap-day2-rhel9-vm3 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": false,
        "ping": "pong"
    }

You may also confirm the VMs are running using the OpenShift UI dashboard https://console-openshift-console.apps.cluster-guid.guid.sandbox.opentlc.com:

Virtualization → VirtualMachines within the vms-aap-day2 project.

vms ocp dashboard
Figure 6. Virtual Machines Running on OpenShift

Conclusion

You have successfully created a dynamic inventory in Ansible Automation Platform that reflects your OpenShift Virtual Machines. This inventory is now ready for use in automation tasks.