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

Added config-context in hostvars #47394

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/ansible/plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def group_extractors(self):
"device_roles": self.extract_device_role,
"platforms": self.extract_platform,
"device_types": self.extract_device_type,
"config_context": self.extract_config_context,
"manufacturers": self.extract_manufacturer
}

Expand Down Expand Up @@ -246,6 +247,14 @@ def extract_device_role(self, host):
except Exception:
return

def extract_config_context(self, host):
try:
url = urljoin(self.api_endpoint, "/api/dcim/devices/" + str(host["id"]))
device_lookup = self._fetch_information(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing another fetch? When the host is fetched, the config_context attribute already exists in the host

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because host shows only local context not the rendered context. For example-

random1": {
                "config_context": [
                    null
                ],
                "device_roles": [
                    "ACI IPN"
                ],
                "device_types": [
                    "random1"
                ],
                "manufacturers": [
                    "Cisco"
                ],
                "racks": [
                    "XYZ"
                ],
                "sites": [
                    "ABC"
                ],
                "tenants": [
                    "ABC"
                ]
            },
            "random": {
                "config_context": [
                    {
                        "cc": {
                            "net": {
                                "vpn_asn": 66000
                            }
                        }
                    }
                ],
                "device_roles": [
                    "ACI IPN"
                ],
                "device_types": [
                    "random"
                ],
                "manufacturers": [
                    "Cisco"
                ],
                "racks": [
                    "XYZ"
                ],
                "sites": [
                    "ABC"
                ],
                "tenants": [
                    "ABC"
                ]

However, if we would like to see the rendered context from the source contexts I had to do another fetch.

"random1": {
                "config_context": [
                    {
                        "cc": {
                            "net": {
                                "vpn_asn": 65000
                            }
                        }
                    }
                ],
                "device_roles": [
                    "ACI IPN"
                ],
                "device_types": [
                    "random1"
                ],
                "manufacturers": [
                    "Cisco"
                ],
                "racks": [
                    "XYZ"
                ],
                "sites": [
                    "ABC"
                ],
                "tenants": [
                    "ABC"
                ]
            },
            "random": {
                "config_context": [
                    {
                        "cc": {
                            "net": {
                                "vpn_asn": 66000
                            }
                        }
                    }
                ],
                "device_roles": [
                    "ACI IPN"
                ],
                "device_types": [
                    "random"
                ],
                "manufacturers": [
                    "Cisco"
                ],
                "racks": [
                    "XYZ"
                ],
                "sites": [
                    "ABC"
                ],
                "tenants": [
                    "ABC"
                ]

return [device_lookup["config_context"]]
except Exception:
return

def extract_manufacturer(self, host):
try:
return [self.manufacturers_lookup[host["device_type"]["manufacturer"]["id"]]]
Expand Down