Automatically include environment variables in an Ansible playbook

Viewed 659

I have an Ansible playbook that performs various tasks based on environment variables that have been set on the system running Ansible. For example:

- hosts: all
  tasks:
  - name: Download allowlist
    command: "some_command {{ lookup('env', 'ALLOWLIST_URL') }}"
    when: "lookup('env', 'ALLOWLIST_URL') is defined"

Currently, I provide all the environment variables from outside the playbook, through a wrapper script:

export ALLOWLIST_URL="..."
export SOME_OTHER_VAR="..."
ansible-playbook -i hosts playbook.yaml

However, I'd like to be able to include these environment variables automatically into Ansible, either through a command line switch, an additional task in the playbook, or another means, so that I can get rid of the wrapper entirely.

To clarify, I'd prefer a way of doing something like this (this is not a real command and only intended to demonstrate what I'm looking for):

ansible-playbook --include-env-vars ENVFILE -i hosts playbook.yml

Or, if it's within the playbook, something like this (again, include_env_vars is not a real module and only intended as a demonstration):

tasks:
- name: Include env vars
  include_env_vars:
    from: ENVFILE

Is such a thing possible?

0 Answers
Related