Skip to content

Commit 2b7145d

Browse files
authored
Link to templating functions from the variables page (ansible#2258)
* Link to templating functions from the variables page Add query/q templating function to the working with playbooks page * fix typos, simplify example
1 parent a3aaa74 commit 2b7145d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

docs/docsite/rst/playbook_guide/playbooks_lookups.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Lookup plugins retrieve data from outside sources such as files, databases, key/
88

99
.. _lookups_and_variables:
1010

11-
Using lookups in variables
12-
==========================
11+
The lookup function
12+
===================
1313

14-
You can populate variables using lookups. Ansible evaluates the value each time it is executed in a task (or template).
14+
You can use the ``lookup`` function to populate variables dynamically. Ansible evaluates the value each time it is executed in a task (or template).
1515

1616
.. code-block:: yaml+jinja
1717

@@ -21,6 +21,30 @@ You can populate variables using lookups. Ansible evaluates the value each time
2121
- debug:
2222
msg: "motd value is {{ motd_value }}"
2323

24+
The first argument to the ``lookup`` function is required and specifies the name of the lookup plugin. If the lookup plugin is in a collection, the fully qualified name must be provided, since the :ref:`collections keyword<collections_keyword>` does not apply to lookup plugins.
25+
26+
The ``lookup`` function also accepts an optional boolean keyword ``wantlist``, which defaults to ``False``. When ``True``, the result of the lookup is ensured to be a list.
27+
28+
Refer to the lookup plugin's documentation to see plugin-specific arguments and keywords.
29+
30+
.. _lookups_and_variables_query:
31+
32+
The query/q function
33+
====================
34+
35+
This function is shorthand for ``lookup(..., wantlist=True)``. These are equivalent:
36+
37+
.. code-block:: yaml+jinja
38+
39+
block:
40+
- debug:
41+
msg: "{{ item }}"
42+
loop: "{{ lookup('ns.col.lookup_items', wantlist=True) }}"
43+
44+
- debug:
45+
msg: "{{ item }}"
46+
loop: "{{ q('ns.col.lookup_items') }}"
47+
2448
For more details and a list of lookup plugins in ansible-core, see :ref:`plugins_lookup`. You may also find lookup plugins in collections. You can review a list of lookup plugins installed on your control machine with the command ``ansible-doc -l -t lookup``.
2549

2650
.. seealso::

docs/docsite/rst/playbook_guide/playbooks_variables.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ This table gives examples of valid and invalid variable names:
4141

4242
.. _Python keywords: https://docs.python.org/3/reference/lexical_analysis.html#keywords
4343

44+
.. note:: Certain :ref:`variables<special_variables>` are defined internally, and cannot be defined by the user.
45+
46+
.. note:: You may want to avoid variable names that would overwrite Jinja2 global functions listed in :ref:`working_with_playbooks`, such as :ref:`lookup<lookups_and_variables>`, :ref:`query<lookups_and_variables_query>`, :ref:`q<lookups_and_variables_query>`, :ref:`now<templating_now>`, and :ref:`undef<templating_undef>`.
47+
4448
Simple variables
4549
================
4650

0 commit comments

Comments
 (0)