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#
- Choose ‘Add’
- Name: ‘Netbox Connection’
- Credential Type: ‘Netbox Credentials’
- Choose Organization: ‘your org’
- Enter Token
- Enter URL
- Save
Create new inventory for your project#
- Choose ‘Add’
- Name: ‘Netbox Inventory’
- Choose Organization: ‘your org’
- Save
Goto Sources in the inventory
- Add new source
- Name: ‘Inventory via Netbox’
- Source: ‘Sourced from a Project’
- Credential: ‘Select Netbox Connection’
- Project: ‘Testproject’
- Inventory file: ‘/ (root)’
- 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'
```