Ansible inventory from Netbox

Published Fri 09 November 2018 by Daniel W

We have been using the excellent Netbox IPAM and DCIM tool internally for tracking our networking configurations, as well as our backend infrastructure whilst also increasing the use of Ansible to remove repetetive tasks in the day-to-day. By utilising Netbox as the end-point for accessing service data, this gives us the opportunity to extrapolate the request for information from our billing and management server - siloing off our infrastructure automation behind a layer of security that was otherwise impossible.

Netbox then becomes the single pane of glass that can be polled for information at any time, using a standarised REST interface - great stuff.

The only issue for us was - how to make use of this information? We already make use of Ansible, but it's inventories have bene static up until now but thankfully dynamic inventories to the rescue.

To cut a long story short:

The script included below is a dynamic inventory written in Python, however unfortunately at the moment it is only compatible with Python3. A few key points for this script to run properly:

Ansible will use the flag --list, so you can simulate this by passing in that flag and the script will output something like this:

    'windows-server-standard': {
        'hosts': ['', .. ''],
        'vars': {}
    'linux-ubuntu': {
        'hosts': ['', .. ''],
        'vars': {
            'ansible_connection': 'ssh',
            'ansible_ssh_user': 'user',
            'ansible_ssh_port': '22222'

© – published with Pelican, theme by djw4