aabouzaid / netbox-as-ansible-inventory

Ansible dynamic inventory script for Netbox.
GNU General Public License v3.0
174 stars 54 forks source link

This PR supports nested group name. #10

Open quenhulu opened 6 years ago

quenhulu commented 6 years ago

Add support for grouping by nested group. This patch splits group name with dot when get group value. Example of nested group name is `device_type.manufacturer'.

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 99.2% when pulling 1ba9d836f9b095ff172caa429f7b5db9d8694310 on quenhulu:nested-group into f88b5f295cf400181133fb99e992a1f98a03adf6 on AAbouZaid:master.

aabouzaid commented 6 years ago

@quenhulu Could you please add more details about your change? Thanks :-)

quenhulu commented 6 years ago

I want to group devices by manufacturer, e.g. Arista, Cisco and Juniper. However manufacturer is not a section, but one of the device_type attributes. So, I chaned code to group by an even attribute of section too.

manufacturer is an attribute of device_type:

$ curl http://localhost/api/dcim/devices/ | jq .
{
  "count": 99,
  "next": "http://localhost/api/dcim/devices/?limit=50&offset=50",
  "previous": null,
  "results": [
    {
      "id": 1,
      "name": "switch00",
      "display_name": "switch00",
      "device_type": {
        "id": 1,
        "url": "http://localhost/api/dcim/device-types/1/",
        "manufacturer": {
          "id": 1,
          "url": "http://localhost/api/dcim/manufacturers/1/",
          "name": "ARISTA",
          "slug": "arista"
        },
        "model": "DCS-7050QX-32",
        "slug": "dcs-7050qx-32"
      },
      ...

netbox.yml:

netbox:
    main:
        api_url: 'http://localhost/api/dcim/devices/'

    group_by:
        default:
            # group by manufacturer
            - device_type.manufacturer