2017-01-18 03:55:03 +01:00
Lookups
-------
2012-05-13 17:00:02 +02:00
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
Lookup plugins allow access to outside data sources. Like all templating, these plugins are evaluated on the Ansible control machine, and can include reading the filesystem as well as contacting external datastores and services. This data is then made available using the standard templating system in Ansible.
2012-08-28 21:41:10 +02:00
2017-09-23 05:19:50 +02:00
.. note ::
- Lookups occur on the local computer, not on the remote computer.
2017-09-26 00:37:33 +02:00
- They are executed with in the directory containing the role or play, as opposed to local tasks which are executed with the directory of the executed script.
2017-11-22 05:14:27 +01:00
- You can pass wantlist=True to lookups to use in jinja2 template "for" loops.
2017-09-26 00:37:33 +02:00
- Lookups are an advanced feature. You should have a good working knowledge of Ansible plays before incorporating them.
2015-02-20 00:15:10 +01:00
2017-01-10 21:46:17 +01:00
.. warning :: Some lookups pass arguments to a shell. When using variables from a remote/untrusted source, use the `|quote` filter to ensure safe usage.
2017-01-10 17:17:33 +01:00
2013-12-26 20:32:01 +01:00
.. contents :: Topics
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
.. _lookups_and_loops:
2013-10-05 00:41:14 +02:00
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
Lookups and loops
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-08-01 04:19:04 +02:00
2017-10-11 06:15:25 +02:00
*lookup plugins* are a way to query external data sources, such as shell commands or even key value stores.
2017-11-22 05:14:27 +01:00
Before Ansible 2.5, lookups were mostly used indirectly in `` with_<lookup `` constructs for looping. Starting with Ansible version 2.5, lookups are used more explicitly as part of Jinja2 expressions fed into the `` loop `` keyword.
2012-05-13 17:00:02 +02:00
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
.. _lookups_and_variables:
2014-02-28 20:18:44 +01:00
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
Lookups and variables
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-02-28 20:18:44 +01:00
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
One way of using lookups is to populate variables. These macros are evaluated each time they are used in a task (or template)::
2013-02-02 18:19:21 +01:00
vars:
2013-04-13 00:21:09 +02:00
motd_value: "{{ lookup('file', '/etc/motd') }}"
2013-02-02 18:19:21 +01:00
tasks:
2018-02-03 12:31:12 +01:00
- debug:
msg: "motd value is {{ motd_value }}"
2013-02-02 18:19:21 +01:00
2017-10-11 06:15:25 +02:00
For more details and a complete list of lookup plugins available, please see :doc: `plugins/lookup` .
2013-10-05 18:31:16 +02:00
.. seealso ::
:doc: `playbooks`
An introduction to playbooks
:doc: `playbooks_conditionals`
Conditional statements in playbooks
:doc: `playbooks_variables`
All about variables
:doc: `playbooks_loops`
Looping in playbooks
`User Mailing List <http://groups.google.com/group/ansible-devel> `_
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel