如何设置Linux环境变量

时间:2021-10-24 10:54:16

Hi I am trying to find out how to set environment variable with Ansible.

大家好,我正在研究如何用Ansible来设置环境变量。

something that a simple shell command like this:

像这样简单的shell命令:

EXPORT LC_ALL=C

tried as shell command and got an error tried using the environment module and nothing happend.

尝试使用shell命令,并尝试使用环境模块,但没有发生任何意外。

what am I missing

我失踪

3 个解决方案

#1


82  

There are multiple ways to do this and from your question it's nor clear what you need.

有多种方法可以做到这一点,从你的问题来看,你并不清楚你需要什么。

1. If you need environment variable to be defined PER TASK ONLY, you do this:

1。如果您需要为每个任务定义环境变量,您可以这样做:

- hosts: dev
  tasks:
    - name: Echo my_env_var
      shell: "echo $MY_ENV_VARIABLE"
      environment:
        MY_ENV_VARIABLE: whatever_value

    - name: Echo my_env_var again
      shell: "echo $MY_ENV_VARIABLE"

Note that MY_ENV_VARIABLE is available ONLY for the first task, environment does not set it permanently on your system.

注意,MY_ENV_VARIABLE只对第一个任务可用,环境不会在系统上永久地设置它。

TASK: [Echo my_env_var] ******************************************************* 
changed: [192.168.111.222] => {"changed": true, "cmd": "echo $MY_ENV_VARIABLE", ... "stdout": "whatever_value"}

TASK: [Echo my_env_var again] ************************************************* 
changed: [192.168.111.222] => {"changed": true, "cmd": "echo $MY_ENV_VARIABLE", ... "stdout": ""}

Hopefully soon using environment will also be possible on play level, not only task level as above. There's currently a pull request open for this feature on Ansible's GitHub: https://github.com/ansible/ansible/pull/8651

希望很快就能在游戏级别上使用环境,而不仅仅是上面提到的任务级别。目前,Ansible的GitHub上有一个针对这个特性的拉请求:https://github.com/ansible/ansible/pull/8651

UPDATE: It's now merged as of Jan 2, 2015.

更新:自2015年1月2日起合并。

2. If you want permanent environment variable + system wide / only for certain user

2。如果您想要永久环境变量+系统范围/仅针对特定用户

You should look into how you do it in your Linux distribution / shell, there are multiple places for that. For example in Ubuntu you define that in files like for example:

您应该研究如何在Linux发行版/ shell中执行它,有很多地方可以实现它。例如在Ubuntu中,你在文件中定义它,例如:

  • ~/.profile
  • ~ / . profile
  • /etc/environment
  • /etc/environment
  • /etc/profile.d directory
  • /etc/profile.d目录
  • ...

You will find Ubuntu docs about it here: https://help.ubuntu.com/community/EnvironmentVariables

你会在这里找到Ubuntu文档:https://help.ubuntu.com/community/EnvironmentVariables。

After all for setting environment variable in ex. Ubuntu you can just use lineinfile module from Ansible and add desired line to certain file. Consult your OS docs to know where to add it to make it permanent.

毕竟,要在Ubuntu中设置环境变量,你只需要从Ansible中使用lineinfile模块,然后在某个文件中添加所需的行。请咨询您的操作系统文档,了解在何处添加它以使其永久存在。

#2


15  

I didnot have enough reputation to comment and hence am adding a new answer. Gasek answer is quite correct. Just one thing, if you are updating the bash_profile file or the /etc/profile, those changes would be reflected only after you do a new login. In case, you set the env variable and then want to use it in the subsequent tasks in the same playbook. Consider adding those environment variables in the .bashrc file. I guess the reason behind this is the login and the non-login shells .Ansible, while executing different tasks reads the parameters from a .bashrc file instead of the bash_profile or the /etc/profile.

我没有足够的声誉去评论,因此我正在添加一个新的答案。加塞克的回答相当正确。只有一件事,如果您正在更新bash_profile文件或/etc/profile文件,那么只有在您进行新的登录之后才能反映这些更改。在这种情况下,您设置了env变量,然后希望在以后的任务中使用它。考虑在.bashrc文件中添加这些环境变量。我猜这背后的原因是登录和非登录shell .Ansible,同时执行不同的任务从.bashrc文件中读取参数,而不是bash_profile或/etc/ profilefile。

As an example, if i updated my path variable to include the custom binary in the .bash_profile file of the respective user and then did a source of the file. The next subsequent tasks wont recognize my command . However if you update in the .bashrc file, the command would work

例如,如果我更新了路径变量,将自定义二进制文件包含在相应用户的.bash_profile文件中,然后做一个文件源。接下来的任务无法识别我的命令。但是,如果您在.bashrc文件中更新,命令将会工作。

 - name: Adding the path in the bashrc files
   lineinfile: dest=/root/.bashrc line='export PATH=$PATH:path-to-mysql/bin' insertafter='EOF' regexp='export PATH=\$PATH:path-to-mysql/bin' state=present

-  - name: Source the bashrc file
   shell: source /root/.bashrc

 - name: Start the mysql client
   shell: mysql -e "show databases";

This would work, but had i done using profile files . the mysql -e"show databases" would have given an error.

这是可行的,但是我使用了概要文件。mysql -e“show databases”会出现错误。

- name: Adding the path in the Profile files
   lineinfile: dest=/root/.bash_profile line='export PATH=$PATH:{{install_path}}/{{mysql_folder_name}}/bin' insertafter='EOF' regexp='export PATH=\$PATH:{{install_path}}/{{mysql_folder_name}}/bin' state=present

 - name: Source the bash_profile file
   shell: source /root/.bash_profile

 - name: Start the mysql client
   shell: mysql -e "show databases";

**This one wont work **, if we have all these tasks in the same playbook

如果我们把所有这些任务都放在同一个剧本里,这个就行不通了。

#3


6  

For persistently setting environment variables, you can use one of the existing roles over at Ansible Galaxy. I recommend franklinkim.environment.

对于持续设置环境变量,您可以使用Ansible Galaxy中现有的角色之一。我建议franklinkim.environment。

Using ansible-galaxy:

使用ansible-galaxy:

$ ansible-galaxy install franklinkim.environment

Using requirements.yml:

使用requirements.yml:

- src: franklinkim.environment

Then in your playbook:

在你的剧本:

- hosts: all
  sudo: yes
  roles:
    - role: franklinkim.environment
      environment_config:
        NODE_ENV: staging
        DATABASE_NAME: staging

#1


82  

There are multiple ways to do this and from your question it's nor clear what you need.

有多种方法可以做到这一点,从你的问题来看,你并不清楚你需要什么。

1. If you need environment variable to be defined PER TASK ONLY, you do this:

1。如果您需要为每个任务定义环境变量,您可以这样做:

- hosts: dev
  tasks:
    - name: Echo my_env_var
      shell: "echo $MY_ENV_VARIABLE"
      environment:
        MY_ENV_VARIABLE: whatever_value

    - name: Echo my_env_var again
      shell: "echo $MY_ENV_VARIABLE"

Note that MY_ENV_VARIABLE is available ONLY for the first task, environment does not set it permanently on your system.

注意,MY_ENV_VARIABLE只对第一个任务可用,环境不会在系统上永久地设置它。

TASK: [Echo my_env_var] ******************************************************* 
changed: [192.168.111.222] => {"changed": true, "cmd": "echo $MY_ENV_VARIABLE", ... "stdout": "whatever_value"}

TASK: [Echo my_env_var again] ************************************************* 
changed: [192.168.111.222] => {"changed": true, "cmd": "echo $MY_ENV_VARIABLE", ... "stdout": ""}

Hopefully soon using environment will also be possible on play level, not only task level as above. There's currently a pull request open for this feature on Ansible's GitHub: https://github.com/ansible/ansible/pull/8651

希望很快就能在游戏级别上使用环境,而不仅仅是上面提到的任务级别。目前,Ansible的GitHub上有一个针对这个特性的拉请求:https://github.com/ansible/ansible/pull/8651

UPDATE: It's now merged as of Jan 2, 2015.

更新:自2015年1月2日起合并。

2. If you want permanent environment variable + system wide / only for certain user

2。如果您想要永久环境变量+系统范围/仅针对特定用户

You should look into how you do it in your Linux distribution / shell, there are multiple places for that. For example in Ubuntu you define that in files like for example:

您应该研究如何在Linux发行版/ shell中执行它,有很多地方可以实现它。例如在Ubuntu中,你在文件中定义它,例如:

  • ~/.profile
  • ~ / . profile
  • /etc/environment
  • /etc/environment
  • /etc/profile.d directory
  • /etc/profile.d目录
  • ...

You will find Ubuntu docs about it here: https://help.ubuntu.com/community/EnvironmentVariables

你会在这里找到Ubuntu文档:https://help.ubuntu.com/community/EnvironmentVariables。

After all for setting environment variable in ex. Ubuntu you can just use lineinfile module from Ansible and add desired line to certain file. Consult your OS docs to know where to add it to make it permanent.

毕竟,要在Ubuntu中设置环境变量,你只需要从Ansible中使用lineinfile模块,然后在某个文件中添加所需的行。请咨询您的操作系统文档,了解在何处添加它以使其永久存在。

#2


15  

I didnot have enough reputation to comment and hence am adding a new answer. Gasek answer is quite correct. Just one thing, if you are updating the bash_profile file or the /etc/profile, those changes would be reflected only after you do a new login. In case, you set the env variable and then want to use it in the subsequent tasks in the same playbook. Consider adding those environment variables in the .bashrc file. I guess the reason behind this is the login and the non-login shells .Ansible, while executing different tasks reads the parameters from a .bashrc file instead of the bash_profile or the /etc/profile.

我没有足够的声誉去评论,因此我正在添加一个新的答案。加塞克的回答相当正确。只有一件事,如果您正在更新bash_profile文件或/etc/profile文件,那么只有在您进行新的登录之后才能反映这些更改。在这种情况下,您设置了env变量,然后希望在以后的任务中使用它。考虑在.bashrc文件中添加这些环境变量。我猜这背后的原因是登录和非登录shell .Ansible,同时执行不同的任务从.bashrc文件中读取参数,而不是bash_profile或/etc/ profilefile。

As an example, if i updated my path variable to include the custom binary in the .bash_profile file of the respective user and then did a source of the file. The next subsequent tasks wont recognize my command . However if you update in the .bashrc file, the command would work

例如,如果我更新了路径变量,将自定义二进制文件包含在相应用户的.bash_profile文件中,然后做一个文件源。接下来的任务无法识别我的命令。但是,如果您在.bashrc文件中更新,命令将会工作。

 - name: Adding the path in the bashrc files
   lineinfile: dest=/root/.bashrc line='export PATH=$PATH:path-to-mysql/bin' insertafter='EOF' regexp='export PATH=\$PATH:path-to-mysql/bin' state=present

-  - name: Source the bashrc file
   shell: source /root/.bashrc

 - name: Start the mysql client
   shell: mysql -e "show databases";

This would work, but had i done using profile files . the mysql -e"show databases" would have given an error.

这是可行的,但是我使用了概要文件。mysql -e“show databases”会出现错误。

- name: Adding the path in the Profile files
   lineinfile: dest=/root/.bash_profile line='export PATH=$PATH:{{install_path}}/{{mysql_folder_name}}/bin' insertafter='EOF' regexp='export PATH=\$PATH:{{install_path}}/{{mysql_folder_name}}/bin' state=present

 - name: Source the bash_profile file
   shell: source /root/.bash_profile

 - name: Start the mysql client
   shell: mysql -e "show databases";

**This one wont work **, if we have all these tasks in the same playbook

如果我们把所有这些任务都放在同一个剧本里,这个就行不通了。

#3


6  

For persistently setting environment variables, you can use one of the existing roles over at Ansible Galaxy. I recommend franklinkim.environment.

对于持续设置环境变量,您可以使用Ansible Galaxy中现有的角色之一。我建议franklinkim.environment。

Using ansible-galaxy:

使用ansible-galaxy:

$ ansible-galaxy install franklinkim.environment

Using requirements.yml:

使用requirements.yml:

- src: franklinkim.environment

Then in your playbook:

在你的剧本:

- hosts: all
  sudo: yes
  roles:
    - role: franklinkim.environment
      environment_config:
        NODE_ENV: staging
        DATABASE_NAME: staging