Chris F
Chris F

Reputation: 16673

How to set existing linux environment variables with ansible?

Ansible v2.4.0.0 on RHEL 6.x

I see How to set linux environment variables with ansible, but it deals with setting a NEW environment variable. The solution does NOT work for me if I try to set my PATH variable. I do...

- name: Add /usr/other/bin to PATH dir to use the git binary there
  environment:
#    PATH: /usr/other/bin:{{ ansible_env.PATH }}
    PATH: /usr/other/bin:{{ lookup('env','PATH') }}

I've tried both ways, shown above, commenting out one vs. the other, and I get syntax errors in both cases. What am I missing?

Upvotes: 8

Views: 17077

Answers (2)

hesch
hesch

Reputation: 134

I don't know why, but Konstantin Suvorov solution doesn't work for me.

After some tests I used following Ansible part in combination with Vagrant (Ubuntu guest).

-name: Creating environment variable via file in /etc/profile.d
 args:
   chdir: /etc/profile.d
 shell: echo "export M2_HOME=/home/vagrant/maven" > env.sh

This works with PATH, too, by adding it into the quotes with ${PATH}.

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68229

This is the correct way to extend PATH variable for a single task:

- name: Execute task with extended PATH
  shell: echo $PATH
  environment:
    PATH: "/usr/other/bin:{{ ansible_env.PATH }}"

environment is not an action by itself, it's a keyword to modify actions' (shell in my example) environment.

Upvotes: 21

Related Questions