Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Group Vars #29

Closed
michaelklose opened this issue May 19, 2018 · 5 comments
Closed

Support for Group Vars #29

michaelklose opened this issue May 19, 2018 · 5 comments

Comments

@michaelklose
Copy link

Is there a way to get group vars with this script? So for example vars for a site or a device role.

@aabouzaid
Copy link
Owner

aabouzaid commented May 28, 2018

@michaelklose Could you please add more details about what you want to do?
Thanks.

@michaelklose
Copy link
Author

michaelklose commented May 28, 2018

So for example I currently get something like this here back when running ansible

ok: [Test-Switch2] => {
    "hostvars[inventory_hostname]": {
        "ansible_check_mode": false,
        "ansible_diff_mode": false,
        "ansible_facts": {},
        "ansible_forks": 5,
        "ansible_inventory_sources": [
            "/home/mikl/netbox.py"
        ],
        "ansible_playbook_python": "/usr/bin/python2",
        "ansible_run_tags": [
            "all"
        ],
        "ansible_skip_tags": [],
        "ansible_version": {
            "full": "2.5.2",
            "major": 2,
            "minor": 5,
            "revision": 2,
            "string": "2.5.2"
        },
        "group_names": [
            "Cisco IOS",
            "Core Switch",
            "Lab"
        ],
        "groups": {
            "Access-Switch": [
                "Test-Switch"
            ],
            "Cisco IOS": [
                "Test-Switch2",
                "Test-Switch"
            ],
            "Core Switch": [
                "Test-Switch2"
            ],
            "Lab": [
                "Test-Switch2",
                "Test-Switch"
            ],
            "all": [
                "Test-Switch2",
                "Test-Switch"
            ],
            "ungrouped": []
        },
        "inventory_dir": "/home/mikl",
        "inventory_file": "/home/mikl/netbox.py",
        "inventory_hostname": "Test-Switch2",
        "inventory_hostname_short": "Test-Switch2",
        "omit": "__omit_place_holder__169b16eaeddb5cd7bfa41b605b4850d2de522c9f",
        "playbook_dir": "/home/mikl"
    }
}

Test-Switch2 is in for example in 3 groups "Cisco IOS", "Core Switch" and "Lab"
"Lab" is a Site so if I retrieve it through the API it looks like similar llike that

{
    "id": 1,
    "name": "Lab",
    "slug": "lab",
    "status": {
        "label": "Active",
        "value": 1
    },
    "region": null,
    "tenant": null,
    "facility": "",
    "asn": null,
    "time_zone": "Europe/Berlin",
    "description": "Testdescription",
    "physical_address": "",
    "shipping_address": "",
    "contact_name": "",
    "contact_phone": "",
    "contact_email": "",
    "comments": "",
    "custom_fields": {},
    "created": "2018-05-19",
    "last_updated": "2018-05-23T12:11:27.044439Z",
    "count_prefixes": 0,
    "count_vlans": 0,
    "count_racks": 0,
    "count_devices": 2,
    "count_circuits": 0
}

It would be great if it dynamically get the vars from the groups to also use them. In this example I might use site description for part of the SNMP Location.

@LuPo
Copy link
Contributor

LuPo commented Jun 29, 2018

I also have a use case for group_vars and before i will start to work on it, I would like to share some thoughts on how I understand it should be implemented. Please share your comments on it.

According to Ansible documentation, already quoted in #19, the inventory structure might look like this:

{
    "group001": {
        "hosts": ["host001", "" "host002"],
        "vars": {
            "var1": true
        },
        "children": ["group002"]
    },
    "group002": {
        "hosts": ["host003","host004"],
        "vars": {
            "var2": 500
        },
        "children":[]
    }
}

Basically we could extend netbox.yml with group_vars at the same level as hosts_vars are defined:

---
netbox:
    main:
        api_url: 'http://netbox:32783/api/dcim/devices'
        api_token: 'sometoken'
    group_by:
        # Default section in Netbox.
        default:
            - device_role
            - rack
        #   - platform

    # Use Netbox sections as host variables.
    hosts_vars:
         general:
              ...

    # Use Netbox sections from groups as group variables.
    group_vars:
        general:
            racks:
              site_name: site
            device-roles:
              represents_color: color

We can maintain the categories level for mapping the key values (i.e. general). Concerning the variables themselves, there might be several approaches.
Logically, I would search them among properties of the groups selected in group_by section - each for its group.
Variables device_role and rack can be searched for their proprieties on the following endpoints:
/api/dcim/device-roles and /api/dcim/racks, so the mapping must be handled.

@michaelklose
Copy link
Author

Technically I can't add something useful regarding your comment but Netbox is working on a similiar feature which is on the way to the official Netbox release. See netbox-community/netbox#1349 for more details.

@michaelklose
Copy link
Author

If you have similar requirements have a look at the inventory script located here: https://github.com/netbox-community/ansible_modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants