Exercise 2: AAP Dynamic Inventory


Overview

Understanding Dynamic Inventories

An inventory in Ansible Automation Platform is a collection of hosts for running jobs, similar to an Ansible inventory file in the command line. It defines which nodes are managed by the control machine.

If your inventory changes dynamically, static solutions may not be ideal. You might need to track hosts from multiple sources like cloud providers or CMDBs. Dynamic Inventories automate this process.

Ansible Automation Platform connects with external inventories using plugins or scripts, with plugins being the preferred option. You can also create custom plugins for additional sources.

Integrating NetBox with Ansible

NetBox’s Certified Ansible Collection includes modules for:

  1. Dynamic Inventory: Fetching real-time data

  2. State Definition: Ensuring configurations align with design

  3. Querying Network Data: Extracting details for playbooks

We’ll explore how Red Hat Ansible Automation Platform syncs with NetBox and integrates with Event-Driven Ansible for real-time network updates.

Lab Environment

The following services are available in your lab environment:

Service Purpose Access Port

AAP

Ansible Automation Platform Web UI

443

NetBox Web

NetBox Web Interface

8000

Gitea

Git Repository Service

3000

VS Code

Visual Studio Code Editor

80

NetBox Term

NetBox Command Line

N/A

Lab Credentials

Ansible Automation Platform Credentials:

  • Username: admin

  • Password: ansible123!

NetBox Credentials:

  • Username: admin

  • Password: netbox


Task 1: Create the Inventory Project

Overview

We are going to use a Dynamic Inventory plugin file from our partner NetBox Labs and their Certified Collection. The NetBox collection provides the netbox.netbox.nb_inventory plugin to connect Ansible Automation Platform to NetBox.

Create the Project

Step 1: In the AAP left sidebar, navigate to menu:Automation Execution[Projects].

Step 2: Click the blue Create project button.

Step 3: Fill in the form with the following details:

Field Value

Name

netbox-inventory-project

Organization

Default

Execution Environment

netbox-ee

Source Control Type

Git

Source Control URL

`https://github.com/ansible-tmm/netbox-inventory.git`

Step 4: Click the blue Create project button at the bottom.

You might notice there are some pre-loaded Projects, Templates, Execution Environments, or other resources. These might have been created as part of supporting the infrastructure for the workshop. Unless we mention them as part of a task, avoid modifying or changing them.

What Did Our Inventory Project Include?

To configure NetBox as the Dynamic Inventory, we are going to use an inventory plugin file. Within netbox-inventory-project, we have a netbox_inventory file with the settings to configure the module.

Step 5: Look at the content of the netbox_inventory file below:

plugin: netbox.netbox.nb_inventory
config_context: true
validate_certs: False
group_by:
  - platforms
  - device_roles
  - sites

compose:
  ansible_network_os: platform.name
  ansible_host: custom_fields.host
  ansible_port: custom_fields.port

This configuration defines how Ansible will query NetBox and organize the inventory data.


Task 2: Configure NetBox Credentials

Creating a Custom Credential Type

We are going to create a Custom Credential Type in Ansible Automation Platform to safely access NetBox through their API.

Step 1: Navigate to menu:Automation Execution[Infrastructure > Credential Types].

Step 2: Click the blue Create credential type button.

Step 3: Fill in the following fields:

  • Name: NetBox

  • Description: NetBox API custom credential

Step 4: In the Input configuration field, copy and paste the following:

fields:
  - id: NETBOX_API
    type: string
    label: NetBox Host URL
  - id: NETBOX_TOKEN
    type: string
    label: NetBox API Token
    secret: true
required:
  - NETBOX_API
  - NETBOX_TOKEN

Step 5: In the Injector configuration field, copy and paste the following:

env:
  NETBOX_API: '{{ NETBOX_API }}'
  NETBOX_TOKEN: '{{ NETBOX_TOKEN }}'

Step 6: Click the blue Create credential type button at the bottom.

Getting the NetBox API Token

Step 1: Switch to the NetBox web tab.

Step 2: To create our Credential, we need to get our NetBox API Token.

Step 3: Log in to NetBox using:

  • Username: admin

  • Password: netbox

Step 4: On the left sidebar of NetBox, scroll to the bottom and expand the Admin menu section.

Step 5: Click on the API Tokens option in the menu.

Step 6: Copy the Key Token to your clipboard.

NetBox API Token

If you have issues getting the API Token from NetBox, don’t worry, it’s available below!

Creating the NetBox Credential in AAP

Step 1: Now let’s go back to the AAP tab and create the credential.

Step 2: Navigate to menu:Automation Execution[Infrastructure > Credentials].

Step 3: Click the blue Create credential button.

Step 4: Fill in the form with the following details:

Field Value

Name

NetBox API

Organization

Default

Credential Type

NetBox (you can type it to find it faster)

NetBox Host URL

http://netbox:8000

NetBox API Token

0123456789abcdef0123456789abcdef01234567

Step 5: Click the blue Create credential button.

NetBox Credential Configuration

Task 3: Create the Dynamic Inventory

Create the Inventory

Step 1: Go to the AAP tab.

Step 2: In the left sidebar, click the Automation Execution menu option.

Step 3: Click the Infrastructure section to expand it, then click on Inventories.

Step 4: Click the blue Create inventory button.

Step 5: Select Create inventory from the dropdown.

Create Inventory Button

Step 6: Fill in the following fields:

  • Name: NetBox Dynamic Inventory

  • Organization: Default

Step 7: Leave all the other fields as they are.

Step 8: Click the blue Create inventory button.


Task 4: Add a Dynamic Source

Overview

Now we’ll add NetBox as a dynamic source to our newly created inventory.

If you are not in the NetBox Dynamic Inventory, click on it again to edit it.

Verify the Inventory is Empty

Step 1: Inside the NetBox Dynamic Inventory, you will see a tab bar.

Step 2: On the tab bar, click on Hosts. You will notice it’s empty.

Empty Hosts Tab

Create the Dynamic Source

Step 1: Now on the same tab bar, click on Sources.

Sources Tab

Step 2: Click the blue Create Source button.

Step 3: In the Name text box, enter netbox-inventory-source.

Step 4: Click the Execution Environment field and select netbox-ee from the dropdown.

Step 5: Click the Source field and select Sourced from a project from the dropdown.

Step 6: A new section titled Source Details will expand below.

Step 7: Click the Credential field and select NetBox API from the dropdown.

Step 8: Click the Project field and select netbox-inventory-project.

Step 9: Click the Inventory file field and select netbox_inventory.

Step 10: Click the Verbosity field and select 1 (Info).

Step 11: In the Options section, tick the checkboxes for:

  • Overwrite

  • Update on launch

These two options will be useful to save time during our workshop. Check the tooltip to learn more about them!

Step 12: In the Cache timeout (seconds) field, enter 120.

Step 13: Click the blue Create source button.

Create Source Configuration

Sync the Inventory

Step 1: Now in the Details view of the netbox-inventory-source we just created, press the Launch inventory update button in the top right to sync the devices.

Launch Inventory Update

Verify the Synchronized Devices

Step 1: Go back to the NetBox Dynamic Inventory details page.

Step 2: Click the Hosts tab to verify the Cisco Catalyst 8000v device is there.

Synchronized Hosts

Congratulations! You have successfully configured NetBox as a Dynamic Inventory source. The inventory will now automatically stay in sync with your NetBox instance.


Next Steps

Excellent work! You’ve successfully completed the Dynamic Inventory configuration and verified that devices from NetBox are now available in Ansible Automation Platform.

Step 1: Press the Next button below to go to the next challenge once you’ve completed the task.


Troubleshooting

NetBox Startup Issues

NetBox needs a couple of minutes to get started.

If you can’t see the NetBox login screen:

Step 1: Go to the netbox term tab.

Step 2: Run the following command to stop NetBox:

docker compose --project-directory=/tmp/netbox-docker stop

Step 3: Run the following command to restart NetBox services:

docker compose --project-directory=/tmp/netbox-docker up -d netbox netbox-worker

Step 4: Wait 2-3 minutes for the services to fully start, then try accessing NetBox again.

Missing NetBox Pre-loaded Content

For the Dynamic Inventory to work, we need some NetBox pre-loaded content.

If you can’t see devices in the NetBox tab:

Step 1: Run the following command:

su - rhel -c 'cd /home/rhel/netbox-setup; ansible-navigator run /home/rhel/netbox-setup/netbox-setup.yml --mode stdout --penv _SANDBOX_ID'

Step 2: Wait for the setup playbook to complete, then refresh the NetBox web interface.


End of Exercise 2