Skip to content

Simple inventory script for netbox that is compatible with AWX and ansible-inventory

License

Notifications You must be signed in to change notification settings

netsiphon/netbox_inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

netbox_inventory

An ansible-inventory compatible script that uses the Rest API for netbox directly without pynetbox. The script can be used with Ansible or AWX and accepts environment variables or command line variables.

Compatibility checked against:

  • Netbox v2.9.3
  • AWX 14.1.0
  • Ansible 2.9.11

The script will output a list of devices or virtual machines based on the tags and/or custom filters provided.

usage: netbox_device_inventory.py [options]

Netbox inventory script.

optional arguments:
  -h, --help  show this help message and exit
  --list      list inventory
  -t TOKEN    Authentication Token
  -u URL      URL
  -d          Device list
  -v          Virtual-machine list
  -g TAGS     List of tags ex: switch,managed
  -c CUSTOM   List of custom filters ex: role=switch,model=blah
  -n DOMAIN   Domain name to append to device/virtual object name for ansible_host values ex: example.com

Environment variables supported (used to pass variables to AWX):

NETBOX_URL: URL for the Netbox site to be queried
NETBOX_TOKEN: Access Token to be used to access the Netbox site
NETBOX_FILTER_TAGS: List of tags(csv) to be used to return matching objects
NETBOX_FILTER_CUSTOM: Custom list of Netbox attributes to filter
NETBOX_DEVICE: Match on device objects (Default:True)
NETBOX_VIRTUAL: Virtual-Machine (Default:False)
ANSIBLE_HOST_DOMAIN: Use domain with device/virtual object name

Setup

Install the required libraries

pip install -r requirements.txt

Using venv:

Create a virtual environment using python 3.x venv option.

python -m venv ~/.venv/inventory

Activate the new virtual environment.

. ~/.venv/inventory/bin/activate .

Update pip

pip install pip --upgrade

Install the required libraries.

pip install -r requirements.txt

Creating an Access Token for Netbox

Navigate to your user profile by clicking on your name at the top right. Select API Tokens. netbox

Click on Add a token and fill out the required fields. netbox

Usage

Ansible Examples

https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html

Export your environment variables for silent operation.

export NETBOX_URL='https://netbox.example.com'  
export NETBOX_TOKEN='<token>'
export NETBOX_FILTER_TAGS='tag1'
export NETBOX_FILTER_CUSTOM="role=server"
export NETBOX_VIRTUAL=True
export ANSIBLE_HOST_DOMAIN=example.com

Use ansible-inventory to output to a host file in yaml format

ansible-inventory -i '<../path/>netbox_inventory.py' --list --output inventories/netbox/hosts --yaml

Call the inventory script at runtime when launching a playbook

ansible-playbook -i '<../path/>netbox_inventory.py' -u root -k -v playbook.yml
AWX

Add the inventory script contents to a new inventory script. new custom inventory

Create a new inventory and associate the script with the inventory under sources create source

Associate a schedule with the inventory to update as necessary.

About

Simple inventory script for netbox that is compatible with AWX and ansible-inventory

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages