2018-04-20 00:02:30 +00:00
.. _playbooks_lookups:
2017-01-18 02:55:03 +00:00
Lookups
-------
2012-05-13 15:00:02 +00: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 03:32:34 +00: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 19:41:10 +00:00
2017-09-23 03:19:50 +00:00
.. note ::
- Lookups occur on the local computer, not on the remote computer.
2019-04-08 16:25:03 +00:00
- They are executed within 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 04:14:27 +00:00
- You can pass wantlist=True to lookups to use in jinja2 template "for" loops.
2017-09-25 22:37:33 +00:00
- Lookups are an advanced feature. You should have a good working knowledge of Ansible plays before incorporating them.
2015-02-19 23:15:10 +00:00
2017-01-10 20:46:17 +00: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 16:17:33 +00:00
2013-12-26 19:32:01 +00: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 03:32:34 +00:00
.. _lookups_and_loops:
2013-10-04 22:41:14 +00: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 03:32:34 +00:00
Lookups and loops
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-08-01 02:19:04 +00:00
2017-10-11 04:15:25 +00:00
*lookup plugins* are a way to query external data sources, such as shell commands or even key value stores.
2018-04-11 22:37:33 +00: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 15:00:02 +00: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 03:32:34 +00:00
.. _lookups_and_variables:
2014-02-28 19:18:44 +00: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 03:32:34 +00:00
Lookups and variables
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-02-28 19:18:44 +00: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 03:32:34 +00: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 17:19:21 +00:00
vars:
2013-04-12 22:21:09 +00:00
motd_value: "{{ lookup('file', '/etc/motd') }}"
2013-02-02 17:19:21 +00:00
tasks:
2018-02-03 11:31:12 +00:00
- debug:
msg: "motd value is {{ motd_value }}"
2013-02-02 17:19:21 +00:00
2018-03-14 19:44:21 +00:00
For more details and a complete list of lookup plugins available, please see :ref: `plugins_lookup` .
2017-10-11 04:15:25 +00:00
2013-10-05 16:31:16 +00:00
.. seealso ::
2018-03-14 19:44:21 +00:00
:ref: `working_with_playbooks`
2013-10-05 16:31:16 +00:00
An introduction to playbooks
2018-03-14 19:44:21 +00:00
:ref: `playbooks_conditionals`
2013-10-05 16:31:16 +00:00
Conditional statements in playbooks
2018-03-14 19:44:21 +00:00
:ref: `playbooks_variables`
2013-10-05 16:31:16 +00:00
All about variables
2018-03-14 19:44:21 +00:00
:ref: `playbooks_loops`
2013-10-05 16:31:16 +00:00
Looping in playbooks
2018-07-21 13:48:47 +00:00
`User Mailing List <https://groups.google.com/group/ansible-devel> `_
2013-10-05 16:31:16 +00:00
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel