Package: nb2an
nb2an: Netbox to AnsibleNetBoxandAnsibleboth require a lot of manual
configuration, often with duplicate information. Keeping them in sync
can be a daunting challenge. Thenb2anpackage provides utilities to
map NetBox values to ansiblehost_varsusing a simple mapping file.Read thefull documentation onlineInstallationYou can usepiporpipxto install the nb2an tools:pipx install nb2anUsageUpdating your ansible data from netbox requires the following steps.Step 1: Create a nb2an configuration filenb2antools take a YAML-based master configuration file containing
your netbox API token (which you'll need to get from your NetBox
instance). In this configuration file you can set a number of variables.
Note that some of these can be overridden with command line options.# Your netbox API token:token:YOUR_NETBOX_API_TOKEN# what the API path is for your netbox installapi_url:https://netbox/api/# domain suffix of your ansible FQDNs# (nb2an tools will check NetBox for simple and full names)suffix:.example.com# where th eroot of your ansible code is storedansible_directory:/home/user/ansibleStep 2: create a YAML mapping fileThenb-update-ansibletool is designed to only update variables within
your ansible host_vars that you ask it to, leaving everything else
(including comments) the same. Note that formatting is overwritten by
default, but see below for how to make a patch instead (actually TBD).The YAML file consists of a host_vars YAML structure that you'd like to
have updated. Currently this supports only dictionaries, but array
support is coming shortly. The value of each variable should be a dotted
notation of where to pull the information from in the netbox data for a
given device. See thenb-devicetool below for how to get a dump of a
device for easier reading.Here's an example mapping file, that updates or creates information in ahost_infosection and anetbox_infosection.host_info:serial_number:serialsite:site.namelocation:location.namerack:rack.displayposition:positionnetbox_info:id:idsite_url:site.urlStep 3: Runnb-update-ansibleNote:don't forget to check in to your VC (git) any outstanding
changes you have to your files inhost_varsbefore running this tool.$ np-update-ansible -c sample.yml
INFO      : modifying /home/user/ansible/host_vars/firewall.example.com.yml

$ cd /home/user/ansible/host_vars

$ git diff
diff --git a/host_vars/f1-lab.example.com.yml b/host_vars/f1-lab.example.com.yml
--- a/host_vars/f1-lab.example.com.yml
+++ b/host_vars/f1-lab.example.com.yml
+host_info:
+  serial_number: 00112233
+  site: MIA
+  location: DC9 Room Q
+  rack: Rack1
+  position: 40
+netbox_info:
+  id: 37
+  device_url: https://netbox/api/device/37/
+  site_url: https://netbox/api/dcim/sites/7/Profit!Note about YAML formatting changesnp-update-ansiblecurrently reformats the YAML file with a standard
yaml rewriter. Although it leaves comments in place, white-space changes
will occur. You have two options to handle this:Allow the formatter (python'sruamel.yaml) to rewrite the YAML
files, as use their formatter as style convention (similar to how
much of the planet is shifting toblackfor formatting python
code). One suggesting for starting with this is passing in an empty
mapping file, or by using the-nflag, which has the same effect.
Then check that in and make a second pass with a real mapping file
in order to see what changes are actually made.Generate a diff fromnp-update-ansible -n mapping.ymlandnp-update-ansible mapping.ymland use the diff to view and apply
changes. The recursive diff won't reformat much of the rest of the
file because it'll only consist of changesonlymade bynp-update-ansible.TODO: auto version of this coming...Data access with the nb-* toolsThenb2anpackage contains a number of tools to access NetBox
configuration from within a shell.nb-racks: Display the racks from NetBoxMany later tools taken an option netbox rack number to evaluate. This
tools gives you a numbered list of all your racks.$ nb-racks
Id  Name                      Site                 Location             #devs
1   Rack1                     SEA                  DC1 Room 42          24
2   Rack2                     AMS                  DC2 Room 1           10
3   Rack3                     MIA                  DC9 Room Q            6nb-devices: List the devices from NetboxThis tools lets all of the devices found in a rack, or if no rack number
is specified, will list all the devices in NetBox. Devices will be
listed in rack order from the top down, optionally with blank spots
listed when-bis specified.$ nb-devices 3
Id  Pos Name                      Type
40  40  firewall                  firewall-XX.YY
41  39  switch                    Cisco ZZ
42  38  webserver                 cpu2817
43  37  backend1                  cpu2817
44  36  database                  cpu2817
45  35  backend2                  cpu2817nb-device:nb-devicedumps the details of a particular device, given itsIdwhich can be found from the first column ofnb-devices. This
information will be critical when building a mapping file to be passed
tonp-update-ansible. The output is a YAML structured array.$ nb-device 40
airflow: null
asset_tag: null
cluster: null
...
device_type:
  display: firewall-XX.YY
  id: 2
  manufacturer:
    display: firewall
    id: 2
...nb-outlets:Displays the outlets used in the rack by devices. This is unfinished
(works but will change)nb-networks:Displays networks used in the rack by devices. This is unfinished (works
but will change)nb-check-ansible:TBD