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

Add tags grouping in NetBox dynamic inventory plugin #45741

Merged
merged 1 commit into from
Oct 6, 2018

Conversation

remyleone
Copy link
Contributor

SUMMARY

Add tags grouping for NetBox

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME
  • lib/ansible/plugins/inventory/netbox.py
ANSIBLE VERSION
ansible 2.8.0.dev0 (netbox_tag_grouping d7c57941bf) last updated 2018/09/17 18:04:31 (GMT +200)
  config file = None
  configured module search path = [u'/Users/sieben/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/sieben/workspace/ansible/lib/ansible
  executable location = /Users/sieben/workspace/ansible/bin/ansible
  python version = 2.7.15 (default, Jun 17 2018, 12:46:58) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
ADDITIONAL INFORMATION

Now all group extractor returns a list of subgroups. Existing extractors return a list with a single element.

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 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. labels Sep 17, 2018
@@ -42,6 +42,7 @@
- sites
- tenants
- racks
- tags
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add all these manually rather than just extending constructed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@s-hertel I used what I knew. Do you have a tutorial exposing how to use constructed? I would like to see some examples in action.

Copy link
Contributor

@s-hertel s-hertel Sep 17, 2018

Choose a reason for hiding this comment

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

Fair enough! I'm not sure of a tutorial, but a lot of plugins are using it - the Google inventory plugin, virtualbox, aws_ec2, openshift, openstack... among others. You can actually use constructed with this plugin as is (though you need to make sure either the constructed or auto inventory plugin is enabled and create a second config file). For example...

I'm not sure what a host from this plugin looks like, but say a host has a tags host variable that is a dictionary of key value pairs, this could be the constructed config:

plugin: constructed
keyed_groups:
  - key: tags  # the top level host var - you can also do more complicated things like 'security_groups|json_query("[].group_id")' where security_groups is a host var that is a list of dictionaries containing a group_id key
    prefix: tag  # the prefix for each of these groups - by default there is no prefix

That config will create tag_Key_Value groups. Or if you only wanted to create groups for each Name tag in the format Name_Value:

plugin: constructed
keyed_groups:
  - key: tags.Name
    separator: ""  # defaults to _, so set to an empty string to have no initial _ separator if there is no prefix

Now to see the groups: ansible-inventory -i netboxconfig.yml -i constructedconfig.yml --graph

Adding constructed to this plugin will let people keep those constructed options in the netbox config file.

plugin: netbox
# netbox options
# also constructed options, like `keyed_groups` or `compose`

Then if you decided to implement it wouldn't be a large diff. First, you’d want to add constructed documentation fragment:

DOCUMENTATION = '''
    ...
    extends_documentation_fragment:
      - constructed


Then you’d need to extend the base class so you can use the Constructable methods:

from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
...
class InventoryModule(BaseInventoryPlugin, Constructable):

And then after adding each host to inventory you’d use use the constructed options to :


strict = self.get_options(‘strict’)

# Composed variables
self._set_composite_vars(self.get_option('compose'), host, hostname, strict=strict)


# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(self.get_option('groups'), host, hostname, strict=strict)

# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), host, hostname, strict=strict)

And then you won’t forever be adding a steady stream of new groups since people can group by any host vars that they want.

@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label Sep 17, 2018
@remyleone remyleone changed the title Add tags grouping Add tags grouping in NetBox dynamic inventory plugin Sep 18, 2018
@remyleone
Copy link
Contributor Author

@s-hertel Could this PR and other be accepted first to have the feature available? I will open a PR to refactor the code using Contructable. I would like it to be a separate PR so it can be used as a PR reference for new inventory plugin that would showcase how to use it and refactor existing code. What do you think about it?

@remyleone
Copy link
Contributor Author

@s-hertel Refactoring PR created

@remyleone
Copy link
Contributor Author

Could this be merged?

@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 Sep 27, 2018
@remyleone
Copy link
Contributor Author

@gundalow Could this be merged?

@gundalow
Copy link
Contributor

gundalow commented Oct 6, 2018

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 Oct 6, 2018
@ansibot ansibot merged commit 9ff25c1 into ansible:devel Oct 6, 2018
@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 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.

None yet

4 participants