ansible.builtin.vars (v2.16.5) — lookup

Lookup templated value of variables

| "added in version" 2.5 of ansible.builtin"

Authors: Ansible Core Team

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

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

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Show value of 'variablename'
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar) }}"
  vars:
    variablename: hello
    myvar: ename
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Show default empty since i dont have 'variablnotename'
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar, default='')}}"
  vars:
    variablename: hello
    myvar: notename
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Produce an error since i dont have 'variablnotename'
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar)}}"
  ignore_errors: True
  vars:
    variablename: hello
    myvar: notename
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: find several related variables
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Access nested variables
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar).sub_var }}"
  ignore_errors: True
  vars:
    variablename:
        sub_var: 12
    myvar: ename
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: alternate way to find some 'prefixed vars' in loop
  ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'ansible_play_' + item) }}"
  loop:
    - hosts
    - batch
    - hosts_all

Inputs

    
_terms:
    description: The variable names to look up.
    required: true

default:
    description:
    - 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.

Outputs

_value:
  description:
  - value of the variables requested.
  elements: raw
  type: list