aabouzaid / netbox-as-ansible-inventory

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

Feature Request : new group #7

Open resyst-it opened 7 years ago

resyst-it commented 7 years ago

Hello, It would be great to add regions to group hosts.

We could have a all group with all devices.

Maybe in the yaml a boolean : group_all = True or False

Thanks for your script.

aabouzaid commented 7 years ago

@resyst-it Sorry, I didn't get it. What is th purpose here? You already have all option by ansible. e.g.

ansible all -i netbox.py -m ping

And you can add more groups to netbox.group_by section.

resyst-it commented 7 years ago

Yes ok for all. In group section we can't have a group by regions. The regions in netbox are above the sites.

aabouzaid commented 7 years ago

Could you please give an example with json?

resyst-it commented 7 years ago

Yes. This is a json.

GET /api/dcim/devices/

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 4,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 30,
            "name": "juniper01",
            "display_name": "juniper01",
            "device_type": {
                "id": 34,
                "url": "http://netbox/api/dcim/device-types/34/",
                "manufacturer": {
                    "id": 14,
                    "url": "http://netbox/api/dcim/manufacturers/14/",
                    "name": "Juniper",
                    "slug": "juniper"
                },
                "model": "MX",
                "slug": "mx"
            },
            "device_role": {
                "id": 7,
                "url": "http://netbox/api/dcim/device-roles/7/",
                "name": "Unknown",
                "slug": "unknown"
            },
            "tenant": {
                "id": 6,
                "url": "http://netbox/api/tenancy/tenants/6/",
                "name": "infra",
                "slug": "infra"
            },
            "platform": null,
            "serial": "JN000000",
            "asset_tag": null,
            "site": {
                "id": 7,
                "url": "http://netbox/api/dcim/sites/7/",
                "name": "DC1",
                "slug": "dc1"
            },
            "rack": null,
            "position": null,
            "face": null,
            "parent_device": null,
            "status": {
                "label": "Active",
                "value": 1
            },
            "primary_ip": null,
            "primary_ip4": null,
            "primary_ip6": null,
            "cluster": null,
            "comments": "MX",
            "custom_fields": {
                "test": "my value"
            }
        },

GET /api/dcim/sites/

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 7,
            "name": "DC1",
            "slug": "dc1",
            "region": {
                "id": 48,
                "url": "http://netbox/api/dcim/regions/48/",
                "name": "DE",
                "slug": "de"
            },
            "tenant": {
                "id": 6,
                "url": "http://netbox/api/tenancy/tenants/6/",
                "name": "infra",
                "slug": "infra"
            },
            "facility": "",
            "asn": null,
            "physical_address": "",
            "shipping_address": "",
            "contact_name": "",
            "contact_phone": "",
            "contact_email": "",
            "comments": "",
            "custom_fields": {},
            "count_prefixes": 0,
            "count_vlans": 112,
            "count_racks": 0,
            "count_devices": 4,
            "count_circuits": 0
        },

First json you have devices list. Second json you have a site with an id and a region section. This section is important because we can have a region (country) and several sites in this region.

By exemple in the json we have a device with the id 7 in site section. Now in sites json i would have the name or slug of the region under the site with the id 7.

Is it possible ?

Thank you.

aabouzaid commented 7 years ago

Yeah that's already supported by default, just add site to "netbox.group_by.default" and it will work. Right now it groups by the "name" (e.g. DC1), but later it will support "slug" too.

Here is an example:

netbox:
    # How servers will be grouped.
    # If no group specified here, inventory script will return all servers.
    group_by:
        # Default section in Netbox.
        default:
            - rack
            - site

Here you will have what in rack and site as groups.

resyst-it commented 7 years ago

Yes sites it's ok. I can have the group DC1. But now i want a group with the name DE. Maybe my regions is USA :

"results": [
        {
            "id": 7,
            "name": "DC1",
            "slug": "dc1",
            "region": {
                "id": 48,
                "url": "http://netbox/api/dcim/regions/48/",
                "name": "USA",
                "slug": "usa"
            },

Now i would like a group USA with all my devices in any sites in this region.

aabouzaid commented 7 years ago

Aha, now I got it :+1: I will check how to implement that.