Skip to main content
  1. Posts/

Netbox <-> Ansible Tower inventory integration

·220 words·2 mins·
netdevops blog ansible netbox inventory
Maximilian Thoma
Author
Maximilian Thoma
network engineer
Table of Contents

How to integrate NetBox inventory into Ansible Tower as an inventory source? NetBox provides an inventory plugin that dynamically loads NetBox hosts into Ansible inventory, along with additional values such as configuration context, site, and more.

Create new credential type in Ansible Tower
#

Goto Adminitration > Credential Types

Set ‘Name’ to ‘Netbox Credentials’.

Input configuration
#

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

Injector configuration
#

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

Save the new credential type.

Create new credential in Resources > Credentials in Ansible Tower
#

  1. Choose ‘Add’
  2. Name: ‘Netbox Connection’
  3. Credential Type: ‘Netbox Credentials’
  4. Choose Organization: ‘your org’
  5. Enter Token
  6. Enter URL
  7. Save

Create new inventory for your project
#

  1. Choose ‘Add’
  2. Name: ‘Netbox Inventory’
  3. Choose Organization: ‘your org’
  4. Save

Goto Sources in the inventory

  1. Add new source
  2. Name: ‘Inventory via Netbox’
  3. Source: ‘Sourced from a Project’
  4. Credential: ‘Select Netbox Connection’
  5. Project: ‘Testproject’
  6. Inventory file: ‘/ (root)’
  7. Update options: X Overwrite X Overwrite variables X Update on launch

Modify your project GIT repo
#

Add following requirements.yml file in /collections folder

collections:
- name: netbox.netbox

Modify inventory.yml in root folder

plugin: netbox.netbox.nb_inventory
validate_certs: False
config_context: True
 
group_by:
  - device_roles
 
device_query_filters:
  - has_primary_ip: 'true'
```

Related

Create ansible config file with all default values
·34 words·1 min
netdevops blog ansible
If you need in your venv environment a plane ansible.
Ansible: Disable / Enable proxy for single task
·152 words·1 min
netdevops blog ansible proxy tower
In my AWX Tower, I have globally set up a proxy.
Rollout new images to Cisco devices with Ansible
·307 words·2 mins
netdevops blog ansible cisco rollout update
Depending on the hardware model, this playbook will copy the appropriate image from an HTTP web server to the switch.