-
Notifications
You must be signed in to change notification settings - Fork 23.7k
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
Conversation
@@ -42,6 +42,7 @@ | |||
- sites | |||
- tenants | |||
- racks | |||
- tags |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 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? |
@s-hertel Refactoring PR created |
Could this be merged? |
@gundalow Could this be merged? |
rebuild_merge |
SUMMARY
Add tags grouping for NetBox
ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
ADDITIONAL INFORMATION
Now all group extractor returns a list of subgroups. Existing extractors return a list with a single element.