mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.8 KiB
ReStructuredText
116 lines
3.8 KiB
ReStructuredText
11 years ago
|
Using Lookups
|
||
|
=============
|
||
13 years ago
|
|
||
11 years ago
|
.. contents::
|
||
|
:depth: 2
|
||
|
|
||
11 years ago
|
Lookup plugins allow access of data in Ansible from outside sources. This can include the filesystem
|
||
|
but also external datastores. These values are then made available using the standard templating system
|
||
11 years ago
|
in Ansible, and are typically used to load variables or templates with information from those systems.
|
||
13 years ago
|
|
||
11 years ago
|
.. note:: This is considered an advanced feature, and many users will probably not rely on these features.
|
||
12 years ago
|
|
||
11 years ago
|
.. _getting_file_contents:
|
||
|
|
||
11 years ago
|
Intro to Lookups: Getting File Contents
|
||
|
```````````````````````````````````````
|
||
12 years ago
|
|
||
11 years ago
|
The file lookup is the most basic lookup type.
|
||
13 years ago
|
|
||
11 years ago
|
Contents can be read off the filesystem as follows::
|
||
13 years ago
|
|
||
|
- hosts: all
|
||
|
vars:
|
||
11 years ago
|
contents: "{{ lookup('file', '/etc/foo.txt') }}"
|
||
12 years ago
|
tasks:
|
||
11 years ago
|
- debug: msg="the value of foo.txt is {{ contents }}"
|
||
12 years ago
|
|
||
11 years ago
|
.. _password_lookup:
|
||
|
|
||
11 years ago
|
The Password Lookup
|
||
12 years ago
|
```````````````````
|
||
13 years ago
|
|
||
11 years ago
|
``password`` generates a random plaintext password and store it in
|
||
|
a file at a given filepath. Support for crypted save modes (as with vars_prompt) is pending. If the
|
||
|
file exists previously, it will retrieve its contents, behaving just like with_file. Usage of variables like "{{ inventory_hostname }}" in the filepath can be used to set
|
||
|
up random passwords per host (what simplifies password management in 'host_vars' variables).
|
||
13 years ago
|
|
||
11 years ago
|
Generated passwords contain a random mix of upper and lowercase ASCII letters, the
|
||
|
numbers 0-9 and punctuation (". , : - _"). The default length of a generated password is 30 characters.
|
||
|
This length can be changed by passing an extra parameter::
|
||
13 years ago
|
|
||
|
---
|
||
|
- hosts: all
|
||
|
|
||
11 years ago
|
tasks:
|
||
13 years ago
|
|
||
11 years ago
|
# create a mysql user with a random password:
|
||
|
- mysql_user: name={{ client }}
|
||
|
password="{{ lookup('password', 'credentials/' + client + '/' + tier + '/' + role + '/mysqlpassword length=15') }}"
|
||
|
priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
|
||
13 years ago
|
|
||
11 years ago
|
(...)
|
||
12 years ago
|
|
||
11 years ago
|
.. _more_lookups:
|
||
12 years ago
|
|
||
11 years ago
|
More Lookups
|
||
12 years ago
|
````````````
|
||
|
|
||
11 years ago
|
.. note:: This feature is very infrequently used in Ansible. You may wish to skip this section.
|
||
12 years ago
|
|
||
11 years ago
|
.. versionadded:: 0.8
|
||
12 years ago
|
|
||
11 years ago
|
Various *lookup plugins* allow additional ways to iterate over data. In `playbooks_loops` you will learn
|
||
|
how to use them to walk over collections of numerous types. However, they can also be used to pull in data
|
||
|
from remote sources, such as shell commands or even key value stores. This section will cover lookup
|
||
|
plugins in this capacity.
|
||
12 years ago
|
|
||
11 years ago
|
Here are some examples::
|
||
12 years ago
|
|
||
|
---
|
||
|
- hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
11 years ago
|
- debug: msg="{{ lookup('env','HOME') }} is an environment variable"
|
||
12 years ago
|
|
||
11 years ago
|
- debug: msg="{{ item }} is a line from the result of this command"
|
||
12 years ago
|
with_lines:
|
||
|
- cat /etc/motd
|
||
|
|
||
11 years ago
|
- debug: msg="{{ lookup('pipe','date') }} is the raw result of running this command"
|
||
12 years ago
|
|
||
11 years ago
|
- debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey"
|
||
12 years ago
|
|
||
11 years ago
|
- debug: msg="{{ lookup('dnstxt', 'example.com') }} is a DNS TXT record for example.com"
|
||
12 years ago
|
|
||
11 years ago
|
- debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template"
|
||
12 years ago
|
|
||
12 years ago
|
As an alternative you can also assign lookup plugins to variables or use them
|
||
|
elsewhere. This macros are evaluated each time they are used in a task (or
|
||
12 years ago
|
template)::
|
||
12 years ago
|
|
||
|
vars:
|
||
12 years ago
|
motd_value: "{{ lookup('file', '/etc/motd') }}"
|
||
12 years ago
|
|
||
12 years ago
|
tasks:
|
||
12 years ago
|
- debug: msg="motd value is {{ motd_value }}"
|
||
12 years ago
|
|
||
11 years ago
|
.. 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
|
||
|
|
||
|
|
||
|
|