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

Read the value of environment variables

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

Authors: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com>

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

Allows you to query the environment variables available on the controller when you invoked Ansible.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Basic usage
  ansible.builtin.debug:
    msg: "'{{ lookup('ansible.builtin.env', 'HOME') }}' is the HOME environment variable."
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Before 2.13, how to set default value if the variable is not defined.
        This cannot distinguish between USR undefined and USR=''.
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.env', 'USR')|default('nobody', True) }} is the user."
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Example how to set default value if the variable is not defined, ignores USR=''
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.env', 'USR', default='nobody') }} is the user."
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Set default value to Undefined, if the variable is not defined
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.env', 'USR', default=Undefined) }} is the user."
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Set default value to undef(), if the variable is not defined
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.env', 'USR', default=undef()) }} is the user."

Inputs

    
_terms:
    description:
    - Environment variable or list of them to lookup the values for.
    required: true

default:
    default: ''
    description: What return when the variable is undefined
    type: raw
    version_added: '2.13'
    version_added_collection: ansible.builtin

Outputs

_list:
  description:
  - Values from the environment variables.
  type: list