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

Conversation

nikkytub
Copy link
Contributor

@nikkytub nikkytub commented Oct 21, 2018

SUMMARY

config-context addition in hostvars in netbox dynamic inventory
Fixed #47391

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

config-context in hostvars in netbox dynamic inventory

ANSIBLE VERSION
ansible 2.6.5
  config file = /User/ansible.cfg
  configured module search path = [u'/User/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.15 (default, Sep  5 2018, 14:52:04) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
ADDITIONAL INFORMATION

Before change

ansible-inventory -vvv -i netbox_inventory.yml --host random
ansible-inventory 2.6.5
  config file = /User/ansible.cfg
  configured module search path = [u'/User/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible-inventory
  python version = 2.7.15 (default, Sep  5 2018, 14:52:04) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Using /User/ansible.cfg as config file
Fetching: http://localhost:8000/api/dcim/sites/?limit=0
Fetching: http://localhost:8000/api/dcim/regions/?limit=0
Fetching: http://localhost:8000/api/tenancy/tenants/?limit=0
Fetching: http://localhost:8000/api/dcim/racks/?limit=0
Fetching: http://localhost:8000/api/dcim/device-roles/?limit=0
Fetching: http://localhost:8000/api/dcim/device-types/?limit=0
Fetching: http://localhost:8000/api/dcim/manufacturers/?limit=0
Fetching: http://localhost:8000/api/dcim/devices/?limit=0&role=aci-ipn
Fetching: http://localhost:8000/api/virtualization/virtual-machines/?limit=0&role=aci-ipn
Parsed /User/netbox_inventory.yml inventory source with netbox plugin
{
    "device_roles": [
        "ACI IPN"
    ],
    "device_types": [
        "random"
    ],
    "manufacturers": [
        "Cisco"
    ],
    "racks": [
        "abc"
    ],
    "sites": [
        "abc"
    ],
    "tenants": [
        "xyz"
    ]
}

After change

ansible-inventory -vvv -i netbox_inventory.yml --host random
ansible-inventory 2.6.5
  config file = /User/ansible.cfg
  configured module search path = [u'/User/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible-inventory
  python version = 2.7.15 (default, Sep  5 2018, 14:52:04) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Using /User/ansible.cfg as config file
Fetching: http://localhost:8000/api/dcim/sites/?limit=0
Fetching: http://localhost:8000/api/dcim/regions/?limit=0
Fetching: http://localhost:8000/api/tenancy/tenants/?limit=0
Fetching: http://localhost:8000/api/dcim/racks/?limit=0
Fetching: http://localhost:8000/api/dcim/device-roles/?limit=0
Fetching: http://localhost:8000/api/dcim/device-types/?limit=0
Fetching: http://localhost:8000/api/dcim/manufacturers/?limit=0
Fetching: http://localhost:8000/api/dcim/devices/?limit=0&role=aci-ipn
Fetching: http://localhost:8000/api/virtualization/virtual-machines/?limit=0&role=aci-ipn
Parsed /User/netbox_inventory.yml inventory source with netbox plugin
{
    "config_contexts": {
        "cc": {
            "net": {
                "vpn_asn": 66000
            }
        }
    },
    "device_roles": [
        "ACI IPN"
    ],
    "device_types": [
        "random"
    ],
    "manufacturers": [
        "Cisco"
    ],
    "racks": [
        "abc"
    ],
    "sites": [
        "abc"
    ],
    "tenants": [
        "xyz"
    ]
}

@ansibot
Copy link
Contributor

ansibot commented Oct 21, 2018

Hi @nikkytub, thank you for submitting this pull-request!

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Oct 21, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. inventory Inventory category needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. new_contributor This PR is the first contribution by a new community member. and removed new_contributor This PR is the first contribution by a new community member. labels Oct 21, 2018
@@ -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_contexts": self.extract_config_contexts,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it be config_context? There can be only one context per device.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Will change it.

def extract_config_contexts(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"
                ]

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. needs_triage Needs a first human triage before being processed. labels Oct 22, 2018
@nikkytub nikkytub changed the title Added config-contexts in hostvars Added config-context in hostvars Oct 22, 2018
@remyleone
Copy link
Contributor

LGTM

@ansibot ansibot added shipit This PR is ready to be merged by Core and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Oct 26, 2018
@ollybee
Copy link

ollybee commented Oct 26, 2018

Could this also be also extended to include services assigned to the device?
Adding this worked for me

    def extract_services(self, host):
        try:
            url = urljoin(self.api_endpoint, "/api/ipam/services/?device=" + str(host["name"]))
            device_lookup = self._fetch_information(url)
            return [device_lookup["results"]]
        except Exception:
            return

@nikkytub
Copy link
Contributor Author

nikkytub commented Oct 26, 2018

@ollybee Please create a feature request explaining what will it bring? Why is it necessary? and then please send a separate pull request.

@ansibot
Copy link
Contributor

ansibot commented Oct 27, 2018

@nikkytub this PR contains the following merge commits:

Please rebase your branch to remove these commits.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Oct 27, 2018

@nikkytub This PR was evaluated as a potentially problematic PR for the following reasons:

  • More than 50 changed files.
  • More than 50 commits.

Such PR can only be merged by human. Contact a Core team member to review this PR on IRC: #ansible-devel on irc.freenode.net

click here for bot help

@ansibot ansibot added merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Oct 27, 2018
@ansibot ansibot removed merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Oct 27, 2018
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed shipit This PR is ready to be merged by Core needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Oct 27, 2018
@nikkytub
Copy link
Contributor Author

@gundalow @FragmentedPacket Please review the changes

@FragmentedPacket
Copy link
Contributor

LGTM

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 10, 2018
@nikkytub
Copy link
Contributor Author

nikkytub commented Nov 11, 2018

@gundalow Could you please review and merge it?

@gundalow
Copy link
Contributor

rebuild_merge

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 12, 2018
@ansibot ansibot merged commit f3583ce into ansible:devel Nov 12, 2018
@nikkytub
Copy link
Contributor Author

@gundalow Thank you so much :)

mjmayer pushed a commit to mjmayer/ansible that referenced this pull request Nov 30, 2018
* Added config-contexts in hostvars

* Changed config-contexts to config-context in hostvars
Tomorrow9 pushed a commit to Tomorrow9/ansible that referenced this pull request Dec 4, 2018
* Added config-contexts in hostvars

* Changed config-contexts to config-context in hostvars
@mnaser
Copy link
Contributor

mnaser commented Dec 26, 2018

While this has merged, it has actually introduced a huge regression. In environments with large number of devices, it has to make a request for every single device in the environment which doesn't even complete in my case but gives a "too many open files" error here.

How can we revise this to be opt-in, or perhaps figure out a way to get the context data in the original list?

@nikkytub
Copy link
Contributor Author

@mnaser Thank you for your feedback. I will make changes to use it as an opt-in.

@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. inventory Inventory category support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

config-contexts-addition in hostvars in netbox dynamic inventory
7 participants