mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07:00
parent
ac1fbbeabc
commit
476d1f818e
3 changed files with 47 additions and 6 deletions
|
@ -65,6 +65,30 @@ You can combine lookups with :ref:`playbooks_filters`, :ref:`playbooks_tests` an
|
||||||
- "{{lookup('sequence', 'end=42 start=2 step=2')|map('log', 4)|list)}}"
|
- "{{lookup('sequence', 'end=42 start=2 step=2')|map('log', 4)|list)}}"
|
||||||
- ['a', 'c', 'd', 'c']
|
- ['a', 'c', 'd', 'c']
|
||||||
|
|
||||||
|
.. _query:
|
||||||
|
|
||||||
|
query
|
||||||
|
+++++
|
||||||
|
|
||||||
|
.. versionadded:: 2.5
|
||||||
|
|
||||||
|
In Ansible 2.5, a new jinja2 function called ``query`` was added for invoking lookup plugins. The difference between ``lookup`` and ``query`` is largely that ``query`` will always return a list.
|
||||||
|
The default behavior of ``lookup`` is to return a string of comma separated values. ``lookup`` can be explicitly configured to return a list using ``wantlist=True``.
|
||||||
|
|
||||||
|
This was done primarily to provide an easier and more consistent interface for interacting with the new ``loop`` keyword, while maintaining backwards compatibiltiy with other uses of ``lookup``.
|
||||||
|
|
||||||
|
The following examples are equivalent::
|
||||||
|
|
||||||
|
lookup('dict', dict_variable, wantlist=True)
|
||||||
|
|
||||||
|
query('dict', dict_variable)
|
||||||
|
|
||||||
|
As demonstrated above the behavior of ``wantlist=True`` is implicit when using ``query``.
|
||||||
|
|
||||||
|
Additionally, ``q`` was introduced as a shortform of ``query``::
|
||||||
|
|
||||||
|
q('dict', dict_variable)
|
||||||
|
|
||||||
|
|
||||||
.. _lookup_plugins_list:
|
.. _lookup_plugins_list:
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@ If you need to skip the whole task depending on the loop variable being defined,
|
||||||
If using a dict in a loop::
|
If using a dict in a loop::
|
||||||
|
|
||||||
- command: echo {{ item.key }}
|
- command: echo {{ item.key }}
|
||||||
loop: "{{ lookup('dict', mydict|default({})) }}"
|
loop: "{{ query('dict', mydict|default({})) }}"
|
||||||
when: item.value > 5
|
when: item.value > 5
|
||||||
|
|
||||||
.. _loading_in_custom_facts:
|
.. _loading_in_custom_facts:
|
||||||
|
@ -251,7 +251,7 @@ The following example shows how to template out a configuration file that was ve
|
||||||
template:
|
template:
|
||||||
src: "{{ item }}"
|
src: "{{ item }}"
|
||||||
dest: /etc/myapp/foo.conf
|
dest: /etc/myapp/foo.conf
|
||||||
loop: "{{lookup('first_found', { 'files': myfiles, 'paths': mypaths})}}"
|
loop: "{{ query('first_found', { 'files': myfiles, 'paths': mypaths}) }}"
|
||||||
vars:
|
vars:
|
||||||
myfiles:
|
myfiles:
|
||||||
- "{{ansible_distribution}}.conf"
|
- "{{ansible_distribution}}.conf"
|
||||||
|
|
|
@ -80,7 +80,7 @@ To loop over a dict, use the ``dict2items`` :ref:`dict_filter`::
|
||||||
- name: create a tag dictionary of non-empty tags
|
- name: create a tag dictionary of non-empty tags
|
||||||
set_fact:
|
set_fact:
|
||||||
tags_dict: "{{ (tags_dict|default({}))|combine({item.key: item.value}) }}"
|
tags_dict: "{{ (tags_dict|default({}))|combine({item.key: item.value}) }}"
|
||||||
with_items: "{{ tags|dict2items }}"
|
loop: "{{ tags|dict2items }}"
|
||||||
vars:
|
vars:
|
||||||
tags:
|
tags:
|
||||||
Environment: dev
|
Environment: dev
|
||||||
|
@ -105,10 +105,27 @@ For example, using the 'nested' lookup, you can combine lists::
|
||||||
priv: "{{ item[1] }}.*:ALL"
|
priv: "{{ item[1] }}.*:ALL"
|
||||||
append_privs: yes
|
append_privs: yes
|
||||||
password: "foo"
|
password: "foo"
|
||||||
loop: "{{ lookup('nested', [ 'alice', 'bob' ], [ 'clientdb', 'employeedb', 'providerdb' ]) }}"
|
loop: "{{ query('nested', [ 'alice', 'bob' ], [ 'clientdb', 'employeedb', 'providerdb' ]) }}"
|
||||||
|
|
||||||
.. note:: ``with_`` loops are actually a combination of things ``with_`` + ``lookup()``, even ``items`` is a lookup. ``loop`` can be used in the same way as shown above.
|
.. note:: ``with_`` loops are actually a combination of things ``with_`` + ``lookup()``, even ``items`` is a lookup. ``loop`` can be used in the same way as shown above.
|
||||||
|
|
||||||
|
|
||||||
|
Using lookup vs query with loop
|
||||||
|
```````````````````````````````
|
||||||
|
|
||||||
|
In Ansible 2.5 a new jinja2 function was introduced named :ref:`query`, that offers several benefits over ``lookup`` when using the new ``loop`` keyword.
|
||||||
|
|
||||||
|
This is described more in the lookup documentation, however, ``query`` provides a more simple interface and a more predictable output from lookup plugins, ensuring better compatibility with ``loop``.
|
||||||
|
|
||||||
|
In certain situations the ``lookup`` function may not return a list which ``loop`` requires.
|
||||||
|
|
||||||
|
The following invocations are equivalent, using ``wantlist=True`` with ``lookup`` to ensure a return type of a list::
|
||||||
|
|
||||||
|
loop: "{{ query('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb']) }}"
|
||||||
|
|
||||||
|
loop: "{{ lookup('nested', ['alice', 'bob'], ['clientdb', 'employeedb', 'providerdb'], wantlist=True) }}"
|
||||||
|
|
||||||
|
|
||||||
.. _do_until_loops:
|
.. _do_until_loops:
|
||||||
|
|
||||||
Do-Until Loops
|
Do-Until Loops
|
||||||
|
@ -224,12 +241,12 @@ There is also a specific lookup plugin ``inventory_hostnames`` that can be used
|
||||||
# show all the hosts in the inventory
|
# show all the hosts in the inventory
|
||||||
- debug:
|
- debug:
|
||||||
msg: "{{ item }}"
|
msg: "{{ item }}"
|
||||||
loop: "{{ lookup('inventory_hostnames', 'all') }}"
|
loop: "{{ query('inventory_hostnames', 'all') }}"
|
||||||
|
|
||||||
# show all the hosts matching the pattern, ie all but the group www
|
# show all the hosts matching the pattern, ie all but the group www
|
||||||
- debug:
|
- debug:
|
||||||
msg: "{{ item }}"
|
msg: "{{ item }}"
|
||||||
loop: "{{ lookup('inventory_hostnames', 'all!www') }}"
|
loop: "{{ query('inventory_hostnames', 'all!www') }}"
|
||||||
|
|
||||||
More information on the patterns can be found on :doc:`intro_patterns`
|
More information on the patterns can be found on :doc:`intro_patterns`
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue