简体   繁体   English

如何用ansible设置现有的linux环境变量?

[英]How to set existing linux environment variables with ansible?

Ansible v2.4.0.0 on RHEL 6.x RHEL 6.x上的Ansible v2.4.0.0

I see How to set linux environment variables with ansible , but it deals with setting a NEW environment variable. 我看到如何使用ansible设置linux环境变量 ,但它涉及设置一个新的环境变量。 The solution does NOT work for me if I try to set my PATH variable. 如果我尝试设置PATH变量,该解决方案对我不起作用。 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? 我错过了什么?

This is the correct way to extend PATH variable for a single task: 这是为单个任务扩展PATH变量的正确方法:

- 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. environment本身不是一个动作,它是修改动作'(在我的例子中是shell )环境中的关键字。

I don't know why, but Konstantin Suvorov solution doesn't work for me. 我不知道为什么,但Konstantin Suvorov解决方案对我不起作用。

After some tests I used following Ansible part in combination with Vagrant (Ubuntu guest). 经过一些测试后,我将以下Ansible部分与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} . 这也适用于PATH ,通过将其添加到带有${PATH}的引号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM