vars - Lookup templated value of variables

New in version 2.5.

Synopsis

  • Retrieves the value of an Ansible variable.

Parameters

Parameter Choices/Defaults Configuration Comments
_terms
required
The variable names to look up.
default
What to return if a variable is undefined.
If no default is set, it will result in an error if any of the variables is undefined.

Examples

- name: Show value of 'variablename'
  debug: msg="{{ lookup('vars', 'variabl' + myvar)}}"
  vars:
    variablename: hello
    myvar: ename

- name: Show default empty since i dont have 'variablnotename'
  debug: msg="{{ lookup('vars', 'variabl' + myvar, default='')}}"
  vars:
    variablename: hello
    myvar: notename

- name: Produce an error since i dont have 'variablnotename'
  debug: msg="{{ lookup('vars', 'variabl' + myvar)}}"
  ignore_errors: True
  vars:
    variablename: hello
    myvar: notename

- name: find several related variables
  debug: msg="{{ lookup('vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}"

- name: alternate way to find some 'prefixed vars' in loop
  debug: msg="{{ lookup('vars', 'ansible_play_' + item) }}"
  loop:
    - hosts
    - batch
    - hosts_all

Return Values

Common return values are documented here, the following are the fields unique to this lookup:

Key Returned Description
_value
valueof the variables requested.



Status

Author

  • Ansible Core

Hint

If you notice any issues in this documentation you can edit this document to improve it.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.6/plugins/lookup/vars.html