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

Alternative filter #13

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

velotiger
Copy link

Hi, here's another suggestion for the filter module aci.py:

aci.py relies upon alternating dicts/lists down the inventory tree.
However, depending on the tool chain to create the tree (conversion
of spreadsheets, …), I thought it might be useful if could get around
this restriction. Therefore I rewrote the filter so that it can handle
an arbitrary sequence of dicts/lists long the path. The results for
inventories with alternating dicts/lists are unchanged. I named
the new filter aci_listify2 so that it can co-exist with the standard
version.
I hope you'll find this contribution useful.

Comments/explanations added to the code.
Optimizations:
* Use closure for listify function to encapsulate internal vars.
* Eliminated some intermediate vars.
* Replaced last loop by direct key lookup.
The original filter needs alternating dicts/lists in the inventory. This
alternative filter accepts lists and dicts in any order. The result for an
inventory with alternating dicts/lists is unchanged.

NEW: plugins/filter/aci2.py
MOD: plugins/filter/aci2.py
This fixes a bug in commit ad32ff5:
Attributes in intermediate items (neither top-level nor bottom) do not
appear in output. E.g. if you look for keywords «tenant», «vrf», «bd»,
«subnet» some attributes for «vrf» might be missing because they were
evaluated after the cache had been transferred to the result list.
To avoid this, all flat key/value pairs are evaluated before any dict
or list at the same level.

MOD: plugins/filter/aci2.py
* You can append a regex to each key (separated by an =–sign).
* Only keys whose name-attribute matches the regex will be included
  in the result.
* If the regex is omitted, all keys will be included (same
  behavior as before – backwards compatible).
* Example:
    "{{ aci_topology|aci_listify2('access_policy',
      'interface_policy_profile=.+998',
      'interface_selector') }}"
  All paths in the output match interface policy profiles that
  end in «998».
  E.g. interface selectors below a non-matching interface policy
  profile will be suppressed from the output.

MOD: plugins/filter/aci2.py
@velotiger
Copy link
Author

Here's (commit fddcc0e) an enhancement to the listifier: You can specify a regex behind each key to filter the name attribute. Only entries whose name attribute match the regex will be added to the output. If no regex is specified, the listifier works as usual (backwards compatible). Example:
"{{ aci_topology|aci_listify2('access_policy', 'interface_policy_profile=.+998', 'interface_selector') }}"

MOD: plugins/filter/aci2.py
Do not hard-code the attribute used for regex matching of
keys. The variable «NameAttr» specifies the name of this
attribute.

MOD: plugins/filter/aci2.py
Fixes this issue: Program ended with error if name-attribut's value
was not a string (used pattern matching on a non-string object).

MOD: plugins/filter/aci2.py
* Short description of alternative filter.
* One more example how to use the alternative filter (dynamically
  specify match pattern for keys at runtime).

MOD: README.md
MOD: plugins/filter/aci2.py
@velotiger
Copy link
Author

velotiger commented Apr 9, 2021

Updated description and documentation in commit velotiger@f560ced
Also created release 1.0.0 (for the alternative filter mostly): velotiger@63e1054

No functional changes or fixes.

Add more patterns to .gitignore

MOD: .gitignore
MOD: plugins/filter/aci2.py
* Get rid of extra loop that deletes flat key/value pairs
  after copying them to the cache.

MOD: plugins/filter/aci2.py
Instances of objects can be organized in lists (as before)
or dicts (new).
* This is useful if you need direct lookups by key.
* This is partly incompatible to previous versions!
  Nested dicts and lists in any order down the object
  tree are not supported any more. This feature was not
  not useful and therefore has been dropped.

MOD: README.md
MOD: plugins/filter/aci2.py
* Use a closure to set fixed values once for all
  (avoid setting them in every new call of the filter).
Previously scalar attributes have been deleted after evaluation.
This simplified further processing of lists and dicts.
However, if the object tree uses anchors and aliases, deleting
attributes invalidates the aliases.
* This is a backport of
  commit eed6b9a
  to the old listify filter (aci.py).
* Add an example for a subnet to the example inventory.

Ref: CiscoDevNet/ansible-aci#517 (comment)
Accetable attribute values: Replace negated check for list or
dict by explicit check for scalar types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant