Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environmental variables using Ansible

I need to set the variables like JAVA_HOME and update PATH. There are a number of ways of doing this. One way is to update the /etc/environment variable and include a line for JAVA_HOME using the lineinfile module and then run the command source /etc/environment directly on the guest OS (CentOS in my case).

Another way is to execute the export command e.g.

export JAVA_HOME=/usr/java/jre1.8.0_51 export PATH=$PATH:$JAVA_HOME 

Is there a cleaner way to do this as all these require manipulating files and running commands directly on the OS to update the environment variables?

like image 279
Deepak Shenoy Avatar asked Aug 02 '15 18:08

Deepak Shenoy


People also ask

How do you set an environment variable in Ansible?

You can use the environment keyword at the play, block, or task level to set an environment variable for an action on a remote host. With this keyword, you can enable using a proxy for a task that does http requests, set the required environment variables for language-specific version managers, and more.

Can Ansible use environment variables?

Ansible Environment variables are used to set the environment variable for action on the remote host using environment keyword, which can be set at the playbook level or the task level and which doesn't affect the ansible configuration file or the environment set for the user and it doesn't include automatically to the ...

How do you set environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do you assign variables in Ansible?

You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.


2 Answers

Yes, there is a cleaner way. You can set environment variables per task:

  tasks:   - shell: echo JAVA_HOME is $JAVA_HOME     environment:       JAVA_HOME: /usr/java/jre1.8.0_51     register: shellout   - debug: var=shellout 

Output:

TASK: [shell echo JAVA_HOME is $JAVA_HOME] **********************************  changed: [localhost]  TASK: [debug var=shellout] ****************************************************  ok: [localhost] => {     "var": {         "shellout": {             "changed": true,              "cmd": "echo JAVA_HOME is \"$JAVA_HOME\"",              "delta": "0:00:00.005797",              "end": "2015-08-07 06:32:47.295061",              "invocation": {                 "module_args": "echo JAVA_HOME is \"$JAVA_HOME\"",                  "module_name": "shell"             },              "rc": 0,              "start": "2015-08-07 06:32:47.289264",              "stderr": "",              "stdout": "JAVA_HOME is /usr/java/jre1.8.0_51",              "stdout_lines": [                 "JAVA_HOME is /usr/java/jre1.8.0_51"             ],              "warnings": []         }     } } 

If you set the environment variable like above in a task, it is only available for this specific task. In subsequent tasks it does not exist unless you define it again.

Though you can define env vars per play as well:

- hosts:   - localhost   gather_facts: no   environment:     JAVA_HOME: /usr/java/jre1.8.0_51   tasks:      ... 

Now it's gonna be available for all tasks of this play.

See Setting the Environment and FAQ: How can I set the PATH or any other environment variable for a task or entire playbook? in the docs.


Another example with a script task:

  tasks:   - script: /tmp/script.sh     environment:       JAVA_HOME: /usr/java/jre1.8.0_51     register: shellout   - debug: var=shellout 

Where the script simply has this content:

#!/bin/sh  echo JAVA_HOME is $JAVA_HOME 
like image 124
udondan Avatar answered Sep 20 '22 16:09

udondan


I found that a workaround to do this was to use the lineinfile command in Ansible:

- name: Set JAVA_HOME   lineinfile: dest=/etc/environment state=present regexp='^JAVA_HOME' >      line='JAVA_HOME=/opt/jre1.8.0_51/bin' 

While this is not ideal, it allows you to create new environmental variables. Of course, you should use variables to construct your directory path. I have included the explicit path to simplify my example.

like image 30
Deepak Shenoy Avatar answered Sep 21 '22 16:09

Deepak Shenoy