4

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?

  • Clarify the use case, please. The environment, you provide by the wrapper, is available to the localhost. Shall other hosts (- hosts: all) see this environment too? – Vladimir Botka Jul 10 '21 at 10:18
  • 1
    Just put them before the ansible call, separated by semicolons : `export ALLOWLIST_URL="..."; export SOME_OTHER_VAR="..."; ansible-playbook -i hosts playbook.yaml` Is that really any worse than command-line arguments? – Jack Jul 10 '21 at 21:10
  • Does [How to set Linux environment variables with Ansible](https://stackoverflow.com/questions/27733511/) answer your question? – U880D Jul 11 '21 at 07:37
  • 1
    @U880D I've realized that I would probably be better served by moving off from environment variables to native Ansible variables to fulfill this use case as there is `include_vars` for Ansible variables. –  Jul 11 '21 at 10:51

0 Answers0