aabouzaid / netbox-as-ansible-inventory

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

How to use custom_fields #1

Closed damhau closed 7 years ago

damhau commented 7 years ago

Hi,

First i wanted to thank you for this script it's nice.

I'm trying to use some custom_fields but I can't make it work.

Here is the json output from netbox:

{ "id": 34, "name": "XXXXXX-01", "display_name": "XXXXXX-01", "device_type": { "id": 4, "manufacturer": { "id": 1, "name": "Cisco", "slug": "cisco" }, "model": "ISR4351/K9", "slug": "isr4351k9" }, "device_role": { "id": 1, "name": "Router", "slug": "router" }, "tenant": { "id": 1, "name": "aaaaaa", "slug": "aaaaa" }, "platform": { "id": 38, "name": "IOS", "slug": "ios" }, "serial": "FDXXXXXXXXXX", "asset_tag": null, "site": { "id": 5, "name": "XXXX", "slug": "xxxx" }, "rack": { "id": 3, "name": "Rack Vienna", "facility_id": null, "display_name": "Rack Vienna" }, "position": 28, "face": 0, "parent_device": null, "status": true, "primary_ip": { "id": 2, "family": 4, "address": "1.1.1.1/32" }, "primary_ip4": { "id": 2, "family": 4, "address": "1.1.1.1/32" }, "primary_ip6": null, "comments": "", "custom_fields": { "Gi001_IP": "2.2.2.78", "bgp_BT_Router_ip": "2.2.2.77" } }

I've tried to add the following to the netbox-inventory.yml file: custom: Ansiblre_bgp_BT_Router_ip: bgp_BT_Router_ip

But it doesn't work.

By the way, do you think it would be possible for your script to get the device interface ip address as hostvar for ansible ?

Regards,

Damien

aabouzaid commented 7 years ago

Hi Damien,

Which Netbox version do you use? Because this JSON format doesn't match latest Netbox versions. This works with netbox >= 1.6.

In api-sample.json you could check here the format of custom_fields.

Once you use the right version, you could use it normally as you mentioned. Also the IP could be used too as var. Here is an example:

hosts_vars:
    ip:
        ansible_ssh_host: primary_ip
damhau commented 7 years ago

I'm running 1.9, maybe they changed the api format. I'm looking at the release notes to see if I find something.

Do you know an exact release I could test with ?

damhau commented 7 years ago

Hi again,

I've tried with version 1.6.3 (https://github.com/digitalocean/netbox/releases/tag/v1.6.3) and here is the output of the api: http://10.180.3.12/api/dcim/devices/1/

{ "id": 1, "name": "Telt", "display_name": "Telt", "device_type": { "id": 1, "manufacturer": { "id": 1, "name": "Cisco", "slug": "cisco" }, "model": "isr4000", "slug": "isr4000" }, "device_role": { "id": 1, "name": "Router", "slug": "router" }, "tenant": null, "platform": null, "serial": "", "asset_tag": null, "site": { "id": 1, "name": "site1", "slug": "site1" }, "rack": null, "position": null, "face": null, "parent_device": null, "status": true, "primary_ip": null, "primary_ip4": null, "primary_ip6": null, "comments": "", "custom_fields": { "toto": null } }

Here is how I defined the custom fields:

image

and

image

Did I miss something ?

aabouzaid commented 7 years ago

I just tested it with v1.9.2 and seems it's working fine.

When the custom filed is not assigned will have the value "null", so it will not returned. It will have the following format:

"custom_fields": {
    "env": null
}

But when you assign it, it will be like this, and that should work fine:

"custom_fields": {
    "env": {
        "id": 1,
        "value": "Dev"
    }
}

Also one note here, the "main" devices api should be used in script conf file (yaml): /api/dcim/devices/

A certain device api will not work (so that could not be added in script conf file) : /api/dcim/devices/1

damhau commented 7 years ago

I must have done somehting strange on my setup.

Here's the output of /api/dcim/devices

[ { "id": 1, "name": "test01", "display_name": "test01", "device_type": { "id": 1, "manufacturer": { "id": 1, "name": "cisco", "slug": "cisco" }, "model": "isr4000", "slug": "isr4000" }, "device_role": { "id": 1, "name": "router", "slug": "router" }, "tenant": null, "platform": null, "serial": "", "asset_tag": null, "site": { "id": 1, "name": "site01", "slug": "site01" }, "rack": null, "position": null, "face": null, "parent_device": null, "status": true, "primary_ip": null, "primary_ip4": null, "primary_ip6": null, "comments": "", "custom_fields": { "custom_field1": "testtest" } } ]

As you can see I've the field I've addedd "custom_field1" with the value "testtest" but I don't have "env":

Any ideas ? What version of netbox are you using ? have you done any specific config ?

damhau commented 7 years ago

Maybe I'm adding not adding the custom field correctly, I go to Django Administration, I go to EXTRA -> Click on Custom Field and then click on add custom field, is it right ?

I've used docker to deploy netbox, did you also use docker or is it installed ? Maybe I've to add somehting to the netbox config ?

aabouzaid commented 7 years ago

Lol I just recognized the issue :D

This style is only for list (select from drop-down menu):

"custom_fields": {
    "env": {
        "id": 1,
        "value": "Dev"
    }
}

The rest are using that style that you mentioned ... I'm working on a fix for this now :-)

aabouzaid commented 7 years ago

@damhau It has been fixed, pull and try it again :-) And thanks a for reporting this issue :tada:

damhau commented 7 years ago

Great. That was fast. I m testing right now.

On 24 Mar 2017, at 12:37, Ahmed M. AbouZaid notifications@github.com wrote:

@damhau It has been fixed, pull and try it again :-) And thanks a for reporting this issue 🎉

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

aabouzaid commented 7 years ago

I'm think this should be fine now.