2013-09-29 23:10:28 +00:00
Using Lookups
=============
2012-05-13 15:00:02 +00:00
2014-04-29 20:49:15 +00:00
Lookup plugins allow access of data in Ansible from outside sources. These plugins are evaluated on the Ansible control
2015-02-19 23:15:10 +00:00
machine, and can include reading the filesystem but also contacting external datastores and services.
2014-04-29 20:49:15 +00:00
These values are then made available using the standard templating system
2013-10-03 02:06:56 +00:00
in Ansible, and are typically used to load variables or templates with information from those systems.
2012-05-13 15:00:02 +00:00
2015-02-19 23:15:10 +00:00
.. note :: This is considered an advanced feature, and many users will probably not rely on these features.
2012-08-28 19:41:10 +00:00
2014-03-16 00:43:10 +00:00
.. note :: Lookups occur on the local computer, not on the remote computer.
2015-10-30 10:50:53 +00:00
.. note :: Lookups are executed with a cwd relative to the role or play, as opposed to local tasks which are executed with the cwd of the executed script.
2015-02-19 23:15:10 +00:00
.. note :: Since 1.9 you can pass wantlist=True to lookups to use in jinja2 template "for" loops.
2013-12-26 19:32:01 +00:00
.. contents :: Topics
2013-10-04 22:41:14 +00:00
.. _getting_file_contents:
2013-09-29 22:44:46 +00:00
Intro to Lookups: Getting File Contents
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-08-01 02:19:04 +00:00
2013-09-29 22:44:46 +00:00
The file lookup is the most basic lookup type.
2012-05-13 15:00:02 +00:00
2013-09-29 22:44:46 +00:00
Contents can be read off the filesystem as follows::
2012-05-13 15:00:02 +00:00
2016-02-15 12:53:34 +00:00
---
2012-05-13 15:00:02 +00:00
- hosts: all
vars:
2013-09-29 22:44:46 +00:00
contents: "{{ lookup('file', '/etc/foo.txt') }}"
2014-02-28 19:18:44 +00:00
2012-08-01 02:19:04 +00:00
tasks:
2014-02-28 19:18:44 +00:00
2013-09-29 22:44:46 +00:00
- debug: msg="the value of foo.txt is {{ contents }}"
2013-05-31 20:07:40 +00:00
2013-10-04 22:41:14 +00:00
.. _password_lookup:
2013-09-29 22:44:46 +00:00
The Password Lookup
2012-08-03 02:08:00 +00:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-05-13 15:00:02 +00:00
2014-02-27 22:44:21 +00:00
.. note ::
A great alternative to the password lookup plugin, if you don't need to generate random passwords on a per-host basis, would be to use :doc: `playbooks_vault` . Read the documentation there and consider using it first, it will be more desirable for most applications.
`` password `` generates a random plaintext password and stores it in
a file at a given filepath.
(Docs about crypted save modes are 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
2016-05-28 09:43:12 +00:00
up random passwords per host (which simplifies password management in 'host_vars' variables).
2012-05-13 15:00:02 +00:00
2013-09-29 22:44:46 +00:00
Generated passwords contain a random mix of upper and lowercase ASCII letters, the
2013-11-07 21:45:52 +00:00
numbers 0-9 and punctuation (". , : - _"). The default length of a generated password is 20 characters.
2013-09-29 22:44:46 +00:00
This length can be changed by passing an extra parameter::
2012-05-13 15:00:02 +00:00
---
- hosts: all
2013-09-29 22:44:46 +00:00
tasks:
2012-05-13 15:00:02 +00:00
2013-09-29 22:44:46 +00:00
# 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
2012-05-13 15:00:02 +00:00
2013-09-29 22:44:46 +00:00
(...)
2012-10-16 22:58:31 +00:00
2013-11-07 21:45:52 +00:00
.. note :: If the file already exists, no data will be written to it. If the file has contents, those contents will be read in as the password. Empty files cause the password to return as an empty string
2013-11-11 19:54:11 +00:00
Starting in version 1.4, password accepts a "chars" parameter to allow defining a custom character set in the generated passwords. It accepts comma separated list of names that are either string module attributes (ascii_letters,digits, etc) or are used literally::
---
- hosts: all
tasks:
# create a mysql user with a random password using only ascii letters:
- mysql_user: name={{ client }}
2014-06-17 10:57:29 +00:00
password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters') }}"
2013-11-11 19:54:11 +00:00
priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
# create a mysql user with a random password using only digits:
- mysql_user: name={{ client }}
2013-11-12 13:54:19 +00:00
password="{{ lookup('password', '/tmp/passwordfile chars=digits') }}"
2013-11-11 19:54:11 +00:00
priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
# create a mysql user with a random password using many different char sets:
- mysql_user: name={{ client }}
2014-06-17 10:58:07 +00:00
password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters,digits,hexdigits,punctuation') }}"
2013-11-11 19:54:11 +00:00
priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
(...)
2013-11-16 22:01:26 +00:00
To enter comma use two commas ',,' somewhere - preferably at the end. Quotes and double quotes are not supported.
2013-11-11 19:54:11 +00:00
2015-01-24 02:01:58 +00:00
.. _csvfile_lookup:
The CSV File Lookup
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 1.5
The `` csvfile `` lookup reads the contents of a file in CSV (comma-separated value)
format. The lookup looks for the row where the first column matches `` keyname `` , and
2015-09-15 23:49:38 +00:00
returns the value in the second column, unless a different column is specified.
2015-01-24 02:01:58 +00:00
The example below shows the contents of a CSV file named elements.csv with information about the
periodic table of elements::
Symbol,Atomic Number,Atomic Mass
H,1,1.008
He,2,4.0026
Li,3,6.94
Be,4,9.012
B,5,10.81
We can use the `` csvfile `` plugin to look up the atomic number or atomic of Lithium by its symbol::
- debug: msg="The atomic number of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=,') }}"
- debug: msg="The atomic mass of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=, col=2') }}"
The `` csvfile `` lookup supports several arguments. The format for passing
arguments is::
lookup('csvfile', 'key arg1=val1 arg2=val2 ...')
The first value in the argument is the `` key `` , which must be an entry that
appears exactly once in column 0 (the first column, 0-indexed) of the table. All other arguments are optional.
========== ============ =========================================================================================
Field Default Description
---------- ------------ -----------------------------------------------------------------------------------------
file ansible.csv Name of the file to load
col 1 The column to output, indexed by 0
2016-04-25 16:13:06 +00:00
delimiter TAB Delimiter used by CSV file. As a special case, tab can be specified as either TAB or \t.
default empty string Default return value if the key is not in the csv file
encoding utf-8 Encoding (character set) of the used CSV file (added in version 2.1)
2015-01-24 02:01:58 +00:00
========== ============ =========================================================================================
.. note :: The default delimiter is TAB, *not* comma.
2015-08-05 09:09:40 +00:00
.. _ini_lookup:
The INI File Lookup
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 2.0
The `` ini `` lookup reads the contents of a file in INI format (key1=value1).
This plugin retrieve the value on the right side after the equal sign ('=') of
a given section ([section]). You can also read a property file which - in this
case - does not contain section.
Here's a simple example of an INI file with user/password configuration::
[production]
# My production information
user=robert
pass=somerandompassword
[integration]
# My integration information
user=gertrude
pass=anotherpassword
We can use the `` ini `` plugin to lookup user configuration::
- debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}"
- debug: msg="User in production is {{ lookup('ini', 'user section=production file=users.ini') }}"
Another example for this plugin is for looking for a value on java properties.
Here's a simple properties we'll take as an example::
user.name=robert
user.pass=somerandompassword
You can retrieve the `` user.name `` field with the following lookup::
2015-09-14 18:50:09 +00:00
- debug: msg="user.name is {{ lookup('ini', 'user.name type=properties file=user.properties') }}"
2015-08-05 09:09:40 +00:00
The `` ini `` lookup supports several arguments like the csv plugin. The format for passing
arguments is::
lookup('ini', 'key [type=<properties|ini>] [section=section] [file=file.ini] [re=true] [default=<defaultvalue>]')
The first value in the argument is the `` key `` , which must be an entry that
appears exactly once on keys. All other arguments are optional.
========== ============ =========================================================================================
Field Default Description
---------- ------------ -----------------------------------------------------------------------------------------
type ini Type of the file. Can be ini or properties (for java properties).
file ansible.ini Name of the file to load
section global Default section where to lookup for key.
re False The key is a regexp.
default empty string return value if the key is not in the ini file
========== ============ =========================================================================================
.. note :: In java properties files, there's no need to specify a section.
2015-01-24 02:01:58 +00:00
2015-07-30 20:24:11 +00:00
.. _credstash_lookup:
The Credstash Lookup
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2015-10-07 16:40:50 +00:00
.. versionadded :: 2.0
2015-07-30 20:24:11 +00:00
Credstash is a small utility for managing secrets using AWS's KMS and DynamoDB: https://github.com/LuminalOSS/credstash
First, you need to store your secrets with credstash::
$ credstash put my-github-password secure123
my-github-password has been stored
Example usage::
---
- name: "Test credstash lookup plugin -- get my github password"
debug: msg="Credstash lookup! {{ lookup('credstash', 'my-github-password') }}"
You can specify regions or tables to fetch secrets from::
---
- name: "Test credstash lookup plugin -- get my other password from us-west-1"
debug: msg="Credstash lookup! {{ lookup('credstash', 'my-other-password', region='us-west-1') }}"
- name: "Test credstash lookup plugin -- get the company's github password"
debug: msg="Credstash lookup! {{ lookup('credstash', 'company-github-password', table='company-passwords') }}"
2016-08-11 17:23:06 +00:00
If you use the context feature when putting your secret, you can get it by passing a dictionary to the context option like this::
---
- name: test
hosts: localhost
vars:
context:
app: my_app
environment: production
tasks:
- name: "Test credstash lookup plugin -- get the password with a context passed as a variable"
debug: msg="{{ lookup('credstash', 'some-password', context=context) }}"
- name: "Test credstash lookup plugin -- get the password with a context defined here"
debug: msg="{{ lookup('credstash', 'some-password', context=dict(app='my_app', environment='production')) }}"
2015-07-30 20:24:11 +00:00
2015-10-07 16:40:50 +00:00
If you're not using 2.0 yet, you can do something similar with the credstash tool and the pipe lookup (see below)::
2015-07-30 20:24:11 +00:00
2015-10-07 16:40:50 +00:00
debug: msg="Poor man's credstash lookup! {{ lookup('pipe', 'credstash -r us-west-1 get my-other-password') }}"
2015-07-30 20:24:11 +00:00
2015-12-20 13:19:20 +00:00
.. _dns_lookup:
The DNS Lookup (dig)
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 1.9.0
.. warning :: This lookup depends on the `dnspython <http://www.dnspython.org/> `_
library.
The `` dig `` lookup runs queries against DNS servers to retrieve DNS records for
a specific name (*FQDN* - fully qualified domain name). It is possible to lookup any DNS record in this manner.
There is a couple of different syntaxes that can be used to specify what record
should be retrieved, and for which name. It is also possible to explicitly
specify the DNS server(s) to use for lookups.
In its simplest form, the `` dig `` lookup plugin can be used to retrieve an IPv4
address (DNS `` A `` record) associated with *FQDN* :
.. note :: If you need to obtain the `` AAAA `` record (IPv6 address), you must
specify the record type explicitly. Syntax for specifying the record
type is described below.
.. note :: The trailing dot in most of the examples listed is purely optional,
but is specified for completeness/correctness sake.
::
- debug: msg="The IPv4 address for example.com. is {{ lookup('dig', 'example.com.')}}"
In addition to (default) `` A `` record, it is also possible to specify a different
record type that should be queried. This can be done by either passing-in
additional parameter of format `` qtype=TYPE `` to the `` dig `` lookup, or by
appending `` /TYPE `` to the *FQDN* being queried. For example::
- debug: msg="The TXT record for gmail.com. is {{ lookup('dig', 'gmail.com.', 'qtype=TXT') }}"
- debug: msg="The TXT record for gmail.com. is {{ lookup('dig', 'gmail.com./TXT') }}"
If multiple values are associated with the requested record, the results will be
returned as a comma-separated list. In such cases you may want to pass option
`` wantlist=True `` to the plugin, which will result in the record values being
returned as a list over which you can iterate later on::
- debug: msg="One of the MX records for gmail.com. is {{ item }}"
with_items: "{{ lookup('dig', 'gmail.com./MX', wantlist=True) }}"
In case of reverse DNS lookups (`` PTR `` records), you can also use a convenience
syntax of format `` IP_ADDRESS/PTR `` . The following three lines would produce the
same output::
- debug: msg="Reverse DNS for 8.8.8.8 is {{ lookup('dig', '8.8.8.8/PTR') }}"
- debug: msg="Reverse DNS for 8.8.8.8 is {{ lookup('dig', '8.8.8.8.in-addr.arpa./PTR') }}"
- debug: msg="Reverse DNS for 8.8.8.8 is {{ lookup('dig', '8.8.8.8.in-addr.arpa.', 'qtype=PTR') }}"
By default, the lookup will rely on system-wide configured DNS servers for
performing the query. It is also possible to explicitly specify DNS servers to
query using the `` @DNS_SERVER_1,DNS_SERVER_2,...,DNS_SERVER_N `` notation. This
needs to be passed-in as an additional parameter to the lookup. For example::
- debug: msg="Querying 8.8.8.8 for IPv4 address for example.com. produces {{ lookup('dig', 'example.com', '@8.8.8.8') }}"
In some cases the DNS records may hold a more complex data structure, or it may
be useful to obtain the results in a form of a dictionary for future
processing. The `` dig `` lookup supports parsing of a number of such records,
with the result being returned as a dictionary. This way it is possible to
easily access such nested data. This return format can be requested by
passing-in the `` flat=0 `` option to the lookup. For example::
- debug: msg="XMPP service for gmail.com. is available at {{ item.target }} on port {{ item.port }}"
with_items: "{{ lookup('dig', '_xmpp-server._tcp.gmail.com./SRV', 'flat=0', wantlist=True) }}"
Take note that due to the way Ansible lookups work, you must pass the
`` wantlist=True `` argument to the lookup, otherwise Ansible will report errors.
Currently the dictionary results are supported for the following records:
.. note :: *ALL* is not a record per-se, merely the listed fields are available
for any record results you retrieve in the form of a dictionary.
========== =============================================================================
Record Fields
---------- -----------------------------------------------------------------------------
*ALL* owner, ttl, type
A address
AAAA address
CNAME target
DNAME target
DLV algorithm, digest_type, key_tag, digest
DNSKEY flags, algorithm, protocol, key
DS algorithm, digest_type, key_tag, digest
HINFO cpu, os
LOC latitude, longitude, altitude, size, horizontal_precision, vertical_precision
MX preference, exchange
NAPTR order, preference, flags, service, regexp, replacement
NS target
NSEC3PARAM algorithm, flags, iterations, salt
PTR target
RP mbox, txt
SOA mname, rname, serial, refresh, retry, expire, minimum
SPF strings
SRV priority, weight, port, target
SSHFP algorithm, fp_type, fingerprint
TLSA usage, selector, mtype, cert
TXT strings
========== =============================================================================
2013-10-04 22:41:14 +00:00
.. _more_lookups:
2012-10-16 22:58:31 +00:00
2013-09-29 22:44:46 +00:00
More Lookups
2013-07-15 04:28:02 +00:00
`` ` ` ` ` ` ` ` ` ``
2013-12-08 21:05:42 +00:00
Various *lookup plugins* allow additional ways to iterate over data. In :doc: `Loops <playbooks_loops>` you will learn
2013-09-29 22:44:46 +00:00
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.
2013-06-14 15:13:38 +00:00
2013-09-29 22:44:46 +00:00
Here are some examples::
2013-02-02 16:51:25 +00:00
---
- hosts: all
tasks:
2013-07-15 17:50:48 +00:00
- debug: msg="{{ lookup('env','HOME') }} is an environment variable"
2013-02-02 16:51:25 +00:00
2013-07-15 17:50:48 +00:00
- debug: msg="{{ item }} is a line from the result of this command"
2013-02-02 16:51:25 +00:00
with_lines:
- cat /etc/motd
2013-07-15 17:50:48 +00:00
- debug: msg="{{ lookup('pipe','date') }} is the raw result of running this command"
2013-02-02 16:51:25 +00:00
2015-02-16 03:37:58 +00:00
# redis_kv lookup requires the Python redis package
2013-07-15 17:50:48 +00:00
- debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey"
2013-02-02 16:51:25 +00:00
2015-01-25 20:20:49 +00:00
# dnstxt lookup requires the Python dnspython package
2013-07-15 17:50:48 +00:00
- debug: msg="{{ lookup('dnstxt', 'example.com') }} is a DNS TXT record for example.com"
2013-02-02 16:51:25 +00:00
2013-07-15 17:50:48 +00:00
- debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template"
2012-10-16 22:51:10 +00:00
2015-10-30 09:34:47 +00:00
# loading a json file from a template as a string
- debug: msg="{{ lookup('template', './some_json.json.j2', convert_data=False) }} is a value from evaluation of this template"
2014-10-02 20:45:46 +00:00
- debug: msg="{{ lookup('etcd', 'foo') }} is a value from a locally running etcd"
2015-09-18 12:51:03 +00:00
# shelvefile lookup retrieves a string value corresponding to a key inside a Python shelve file
- debug: msg="{{ lookup('shelvefile', 'file=path_to_some_shelve_file.db key=key_to_retrieve') }}
2015-02-20 14:06:17 +00:00
# The following lookups were added in 1.9
2015-01-27 22:48:39 +00:00
- debug: msg="{{item}}"
2015-01-27 22:25:55 +00:00
with_url:
2015-07-11 15:33:28 +00:00
- 'https://github.com/gremlin.keys'
2015-01-27 22:25:55 +00:00
2015-02-20 14:06:17 +00:00
# outputs the cartesian product of the supplied lists
- debug: msg="{{item}}"
with_cartesian:
- list1
- list2
- list3
2013-04-05 17:00:23 +00:00
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
2013-04-21 02:48:20 +00:00
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:56:56 +00:00
2013-02-02 17:19:21 +00:00
tasks:
2014-02-28 19:18:44 +00:00
2013-04-12 22:21:09 +00:00
- debug: msg="motd value is {{ motd_value }}"
2013-02-02 17:19:21 +00:00
2013-10-05 16:31:16 +00: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