aabouzaid / netbox-as-ansible-inventory

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

Fixed ungrouped inventory host assignment. Resolves #19 and Fixes #32 #33

Closed LuPo closed 6 years ago

LuPo commented 6 years ago

group called "ungrouped" is indeed a default group implicit in an Ansible inventory.

With this pull request I propose to

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 849310bca89a01b492f1f6e4b082c419dfd7a981 on LuPo:devel into a2d03de1371fc64e10e65b0ac5ff87714b4db3af on AAbouZaid:devel.

LuPo commented 6 years ago

With this PR I propose not only to add hosts to a generic group when no group is selected, but as well those which are not in a chosen group. Below is the case with hosts grouped by rack_name.

{
    "_meta": {
        "hostvars": {
            "10002": {
                "rack_name": "4200001",
                "MAC": "bb:00:bb:00:00:bb"
            },
            "10003": {
                "rack_name": "4200001",
                "MAC": "cc:00:cc:00:00:cc"
            },
            "10005": {
                "rack_name": "4200003",
                "MAC": "dd:00:dd:00:00:dd"
            },
            "10010": {
                "MAC": "ee:00:ee:00:ee:ee"
            },
            "10011": {
                "MAC": "aa:bb:aa:bb:00:00"
            },
            "10012": {
                "MAC": "00:00:00:00:00:00"
            },
            "10013": {
                "rack_name": "4200003",
                "MAC": "be:be:be:be:be:be"
            }
        }
    },
    "4200001": [
        "10002",
        "10003"
    ],
    "4200003": [
        "10005",
        "10013"
    ],
    "ungrouped": [
        "10010",
        "10011",
        "10012"
    ]
}

Previously those hosts has not been grouped if not only in _meta. This were leading to not being able to list all hosts like in this example: ansible all -i netbox.py --list-hosts

  hosts (4):
    10005
    10013
    10002
    10003

ansible ungrouped -i netbox.py --list-hosts

 [WARNING]: Could not match supplied host pattern, ignoring: ungrouped

 [WARNING]: No hosts matched, nothing to do

  hosts (0):

With this PR the results are shown as follows: ansible all -i netbox.py --list-hosts

  hosts (7):
    10010
    10011
    10012
    10002
    10003
    10005
    10013

ansible ungrouped -i netbox.py --list-hosts

  hosts (3):
    10010
    10011
    10012
LuPo commented 6 years ago

19 is being addressed as well directly in this PR.

Should had I submit a PR for it separately?

aabouzaid commented 6 years ago

Thanks a lot @LuPo for your effort :+1: