diff --git a/README.md b/README.md index 6c45a8d..58a82b8 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,15 @@ PEP440 is the schema used to describe the versions of Ansible. ### Filter plugins Name | Description --- | --- -ansible.utils.get_path|Get the value within a variable using a path. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_lookup.rst) -ansible.utils.to_paths|Convert complex objects to paths. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_paths_lookup.rst) +ansible.utils.get_path|Get value using path. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_lookup.rst) +ansible.utils.index_of|Find items in a list. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.netcommon.index_of_lookup.rst) +ansible.utils.to_paths|Convert objects to paths. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_paths_lookup.rst) ### Lookup plugins Name | Description --- | --- [ansible.utils.get_path](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_lookup.rst)|Retrieve the value in a variable using a path +[ansible.utils.index_of](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_lookup.rst)|Find the indicies of items in a list matching some criteria [ansible.utils.to_paths](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_paths_lookup.rst)|Flatten a complex object into a dictionary of paths and values diff --git a/docs/ansible.utils.get_path_lookup.rst b/docs/ansible.utils.get_path_lookup.rst index 649e046..f2355e8 100644 --- a/docs/ansible.utils.get_path_lookup.rst +++ b/docs/ansible.utils.get_path_lookup.rst @@ -140,7 +140,7 @@ Examples path: b.c.d[0] # TASK [ansible.builtin.set_fact] ************************************* - # ok: [nxos101] => changed=false + # ok: [nxos101] => changed=false # ansible_facts: # as_filter: '0' # as_lookup: '0' @@ -157,7 +157,7 @@ Examples look_for: a.b.c.d[0] # TASK [Retrieve a value deep inside all of the host's vars] ********** - # ok: [nxos101] => changed=false + # ok: [nxos101] => changed=false # ansible_facts: # as_filter: '0' # as_lookup: '0' @@ -180,7 +180,7 @@ Examples value: "{{ vars|ansible.utils.get_path(item) }}" # TASK [Get the paths for the object] ********************************* - # ok: [nxos101] => changed=false + # ok: [nxos101] => changed=false # ansible_facts: # paths: # a.b.c.d[0]: 0 @@ -189,13 +189,13 @@ Examples # a.b.c.e[1]: false # TASK [Retrieve the value of each path from vars] ******************** - # ok: [nxos101] => (item=a.b.c.d[0]) => + # ok: [nxos101] => (item=a.b.c.d[0]) => # msg: The value of path a.b.c.d[0] in vars is 0 - # ok: [nxos101] => (item=a.b.c.d[1]) => + # ok: [nxos101] => (item=a.b.c.d[1]) => # msg: The value of path a.b.c.d[1] in vars is 1 - # ok: [nxos101] => (item=a.b.c.e[0]) => + # ok: [nxos101] => (item=a.b.c.e[0]) => # msg: The value of path a.b.c.e[0] in vars is True - # ok: [nxos101] => (item=a.b.c.e[1]) => + # ok: [nxos101] => (item=a.b.c.e[1]) => # msg: The value of path a.b.c.e[1] in vars is False @@ -217,9 +217,9 @@ Examples - by_name['Ethernet1/2'].description # TASK [Get the description of several interfaces] ******************** - # ok: [nxos101] => (item=by_name['Ethernet1/1'].description) => + # ok: [nxos101] => (item=by_name['Ethernet1/1'].description) => # msg: Configured by Ansible - # ok: [nxos101] => (item=by_name['Ethernet1/2'].description) => + # ok: [nxos101] => (item=by_name['Ethernet1/2'].description) => # msg: Configured by Ansible Network diff --git a/docs/ansible.utils.index_of_lookup.rst b/docs/ansible.utils.index_of_lookup.rst new file mode 100644 index 0000000..ada5bb4 --- /dev/null +++ b/docs/ansible.utils.index_of_lookup.rst @@ -0,0 +1,504 @@ +.. _ansible.utils.index_of_lookup: + + +********************** +ansible.utils.index_of +********************** + +**Find the indicies of items in a list matching some criteria** + + +Version added: 1.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This lookup returns the indicies of items matching some criteria in a list +- When working with a list of dictionaries, the key to evaluate can be specified +- ``index_of`` is also available as a ``filter_plugin`` for convenience + + + + +Parameters +---------- + +.. raw:: html + +
Parameter | +Choices/Defaults | +Configuration | +Comments | +
---|---|---|---|
+
+ _terms
+
+
+ -
+ / required
+
+ |
+ + | ++ | +
+ The values below provided in the order
+ test , value , key . |
+
+
+ data
+
+
+ list
+ / required
+
+ |
+ + | ++ | +
+ A list of items to enumerate and test against
+ |
+
+
+ fail_on_missing
+
+
+ boolean
+
+ |
+
+
|
+ + | +
+ When provided a list of dictionaries, fail if the key is missing from one or more of the dictionaries
+ |
+
+
+ key
+
+
+ string
+
+ |
+ + | ++ | +
+ When the data provided is a list of dictionaries, run the test againt this dictionary key When using a
+ key , the data must only contain dictionaries See fail_on_missing below to determine the behaviour when the key is missing from a dictionary in the data |
+
+
+ test
+
+
+ string
+ / required
+
+ |
+ + | ++ | +
+ The name of the test to run against the list, a valid jinja2 test or ansible test plugin. Jinja2 includes the following tests http://jinja.palletsprojects.com/templates/#builtin-tests. An overview of tests included in ansible https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html
+ |
+
+
+ value
+
+
+ raw
+
+ |
+ + | ++ | +
+ The value used to test each list item against Not required for simple tests (eg:
+ true , false , even , odd ) May be a string , boolean , number , regular expesion dict etc, depending on the test used |
+
+
+ wantlist
+
+
+ boolean
+
+ |
+
+
|
+ + | +
+ When only a single entry in the
+ data is matched, that entries index is returned as an integer If set to True , the return value will always be a list, even if only a single entry is matched This can also be accomplised using query or q instead of lookup https://docs.ansible.com/ansible/latest/plugins/lookup.html |
+
Key | +Returned | +Description | +
---|---|---|
+
+ _raw
+
+
+ -
+
+ |
+ + |
+ One or more zero-based indicies of the matching list items
+ See
+ wantlist if a list is always required+ |
+