├── README.md └── netbox-awx.py /README.md: -------------------------------------------------------------------------------- 1 | # Netbox AWX inventory script 2 | A simple inventory script to populate data from netbox to AWX inventory. This script is tested with Netbox v2.6.2 3 | 4 | ## Grouping and Host vars 5 | It groups hosts based on site, rack, platform, tenant, tag. 6 | host vars are populated from hosts config_context. 7 | 8 | ## Filtering hosts 9 | You might not want to import all devices from netbox to AWX inventory. For such cases, a list of tags to filter the hosts may be added to FILTER_TAGS variable. If FILTER_TAGS list is empty, all devices will be imported 10 | -------------------------------------------------------------------------------- /netbox-awx.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import json 4 | import requests 5 | 6 | # Netbox URL 7 | URL = '' 8 | 9 | # Netbox API Token 10 | TOKEN = '' 11 | 12 | # AWX Filter Tags 13 | FILTER_TAGS = [] 14 | 15 | headers = { 16 | 'Accept': 'application/json ; indent=4', 17 | 'Authorization': 'Token %s' % (TOKEN), 18 | } 19 | 20 | device_url = URL + '/api/dcim/devices/' 21 | devices = [] 22 | sites = {} 23 | racks = {} 24 | platforms = {} 25 | tenants = {} 26 | tags = {} 27 | inventory = {} 28 | hostvars = {} 29 | 30 | # Get data from netbox 31 | 32 | def get_data(api_url): 33 | out = [] 34 | while api_url: 35 | api_output = requests.get(api_url, headers=headers, verify=False) 36 | api_output_data = api_output.json() 37 | 38 | if isinstance(api_output_data, dict) and "results" in api_output_data: 39 | out += api_output_data["results"] 40 | api_url = api_output_data["next"] 41 | return out 42 | 43 | hosts_list = get_data(device_url) 44 | 45 | # Filter hosts for AWX 46 | for i in hosts_list: 47 | if FILTER_TAGS: 48 | tag_list = [] 49 | if i['tags']: 50 | for tag_item in i['tags']: 51 | tag_list.append(tag_item['name']) 52 | if any(item in FILTER_TAGS for item in tag_list): 53 | if i['status']: 54 | if i['status']['label'] == 'Active': 55 | devices.append(i) 56 | 57 | else: 58 | if i['status']: 59 | if i['status']['label'] == 'Active': 60 | devices.append(i) 61 | # Populate inventory 62 | 63 | for i in devices: 64 | if i['name']: 65 | if i['config_context']: 66 | hostvars.setdefault('_meta', {'hostvars': {}})['hostvars'][i['name']] = i['config_context'] 67 | if i['site']: 68 | sites.setdefault(i['site']['slug'], {'hosts': []})['hosts'].append(i['name']) 69 | hostvars['_meta']['hostvars'][i['name']].setdefault('tags', {})['site'] = i['site']['slug'] 70 | if i['rack']: 71 | racks.setdefault(i['rack']['name'], {'hosts': []})['hosts'].append(i['name']) 72 | hostvars['_meta']['hostvars'][i['name']].setdefault('tags', {})['rack'] = i['rack']['name'] 73 | if i['platform']: 74 | platforms.setdefault(i['platform']['slug'], {'hosts': []})['hosts'].append(i['name']) 75 | hostvars['_meta']['hostvars'][i['name']].setdefault('tags', {})['platform'] = i['platform']['slug'] 76 | if i['tenant']: 77 | tenants.setdefault(i['tenant']['slug'], {'hosts': []})['hosts'].append(i['name']) 78 | hostvars['_meta']['hostvars'][i['name']].setdefault('tags', {})['tenant'] = i['tenant']['slug'] 79 | for t in i['tags']: 80 | tags.setdefault(t['name'], {'hosts': []})['hosts'].append(i['name']) 81 | hostvars['_meta']['hostvars'][i['name']].setdefault('tags', {})[t['name']] = True 82 | 83 | inventory.update(sites) 84 | inventory.update(racks) 85 | inventory.update(platforms) 86 | inventory.update(tenants) 87 | inventory.update(tags) 88 | inventory.update(hostvars) 89 | 90 | print(json.dumps(inventory, indent=4)) 91 | --------------------------------------------------------------------------------