Setting Target Machine Environment Variables

Hello, Ansible newbie here. I seem to be having a hard time understanding how to set target machine environmental variables, even after trying the several example solutions that I’ve found around the web.

Here are a few techniques that I’ve tried:

*As a separate play

  • name: “try this env”
    shell: export APP_HTTP_PORT=8081

*As part of the application kick-up command:

  • name: “Start example Node.js app.”
    command: sudo forever -l ~/log.log -e ~/err.log -o /out.log /var/www/appfolder/index.js
    environment:
    APP_HTTP_PORT: “8001”

*As part of a host definition (variable)

  • hosts: ec2
    name: configuration play
    user: ec2-user
    become: yes
    gather_facts: true

environment: “{{ app_environment }}”

*As part of a host definition (direct)

  • hosts: ec2
    name: configuration play
    user: ec2-user
    become: yes
    gather_facts: true

environment:
APP_HTTP_PORT: “8001”

In each case, my application evaluates for the presence of specific env variables, and based on the logs I have, the variables are not available to the application. When using debug mode (-vvvv) I do indeed see the env variables in my console, and I see the variable being output with an expected value when I issue an “echo” command from the playbook - but not when I ssh into the server and run the identical commands. The app’s logs indicate missing variables as well. I hope someone here would be able to point me in the right direction - thanks!!

each task is a different session, env vars created in task1, will not
be seen in task2 (if you login from one terminal and expot a variable,
you won't see it when you login from a 2nd terminal).

Use the environment: directive at play/block/task level if you want
your tasks to have a specific set environment. for example:
http://docs.ansible.com/ansible/playbooks_environment.html