ansible.builtin.vars – Lookup templated value of variables

Note

This module is part of ansible-base and included in all Ansible installations. In most cases, you can use the short module name vars even without specifying the collections: keyword. Despite that, we recommend you use the FQCN for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

New in version 2.5: of ansible.builtin

Synopsis

  • Retrieves the value of an Ansible variable. Note: Only returns top level variable names.

Parameters

Parameter Choices/Defaults Configuration Comments
_terms
string / required
The variable names to look up.
default
string
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: Access nested variables
  debug: msg="{{ lookup('vars', 'variabl' + myvar).sub_var }}"
  ignore_errors: True
  vars:
    variablename:
        sub_var: 12
    myvar: ename

- 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
list / elements=any
success
value of the variables requested.



Authors

  • Ansible Core Team