Introduction to NetBox and Ansible Core
NetBox is an open-source tool designed for network automation and infrastructure management, specializing in managing IP address allocations and data center infrastructure. Ansible Core is a widely used automation tool that allows you to automate software provisioning, configuration management, and application deployment.
Integrating NetBox with Ansible Core can centralize your network inventory, making it easier to manage and automate your network devices. This integration leverages NetBox’s API to dynamically pull inventory data into Ansible using a YAML configuration file.
Prerequisites
Before starting, ensure you have the following:
- NetBox: Installed and configured. You can find installation instructions here
- Ansible Core: Installed on your control node. Install Ansible Core by following the official Ansible installation guide
- NetBox API Token: Generate an API token in NetBox for authentication purposes. NetBox API documentation
Configure NetBox as an Inventory Source Using YAML File
To use NetBox as an inventory source in Ansible, follow these steps:
- Install the netbox.netbox Ansible Collection
Install the netbox.netbox collection which provides the inventory plugin:
ansible-galaxy collection install netbox.netbox
- Create a YAML Configuration File
Create a YAML configuration file named netbox_inventory.yml in your Ansible directory. This file will define how Ansible pulls inventory data from NetBox.
nano /etc/ansible/netbox_inventory.yml
plugin: netbox.netbox.nb_inventory
api_endpoint: {{ netbox IP }}
token: {{ netbox Token. }}
validate_certs: false
config_context: false
flatten_custom_fields: true
group_by:
# - sites
# - device_roles
# - platforms
# - locations
query_filters:
# restrict inventory to specific tenant
# - tenants: {{ tenant name }}
device_query_filters:
# - has_primary_ip: 'true'
# You can also filter by platform, role, e.g.:
# - platform: cisco-ios-xe
# - role: router
# - role: switch
- Update Ansible Configuration
Modify your ansible.cfg to use the YAML file as the inventory source:
Open your Ansible config file with your favorite editor. I’m using nano
nano /etc/ansible/ansible.cfg
find the inventory line under [defaults] and update it with the location of your new inventory file
inventory=/etc/ansible/netbox_inventory.yml
- Test Your Inventory Configuration
ansible-inventory --list
This command should return your inventory in JSON format, showing the hosts pulled from NetBox.
- Example Playbook Using NetBox Inventory
Create a sample Ansible playbook to ensure your inventory is working. Here’s a simple playbook that pings all devices:
nano playbook.yml
playbook contents:
- name: Test NetBox Inventory
hosts: all
tasks:
- name: Ping the hosts
ansible.builtin.ping:
Run the playbook:
ansible-playbook playbook.yml
- Enhancing Your Configuration
Here’s an example of grouping by device roles and platforms:
group_by:
- device_roles
- platforms
And a filter to include only devices with a specific tenant:
query_filters:
- tenants: YourTenantName
You can also add device_query_filters to only include devices with specific details:
device_query_filters:
- has_primary_ip: 'true'
# You can also filter by platform, role, e.g.:
- platform: cisco-ios-xe
- role: router
- role: switch
- Best Practices
Explore the NetBox and Ansible documentation for more advanced configurations and integrations. Happy automating!
References: