Ansible로 Linux 환경 변수를 설정하는 방법
안녕하세요, Ansible로 환경 변수를 설정하는 방법을 찾으려고합니다.
다음과 같은 간단한 쉘 명령이 있습니다.
EXPORT LC_ALL=C
쉘 명령으로 시도했지만 환경 모듈을 사용하여 오류가 발생했지만 아무 일도 일어나지 않았습니다.
내가 뭘 놓치고 있니
이를 수행하는 방법에는 여러 가지가 있으며 귀하의 질문에서 필요한 것이 무엇인지 명확하지 않습니다.
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"
참고 MY_ENV_VARIABLE
첫 번째 작업에 사용할 수는 environment
시스템에 영구적으로 설정하지 않습니다.
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": ""}
위와 같은 작업 수준뿐만 아니라 플레이 수준environment
에서도 곧 사용할 수 있기를 바랍니다 . 현재 Ansible의 GitHub : https://github.com/ansible/ansible/pull/8651 에이 기능에 대한 풀 요청이 열려 있습니다.
업데이트 : 2015 년 1 월 2 일 현재 병합되었습니다.
2. 영구 환경 변수 + 시스템 전체를 원할 경우 / 특정 사용자 만
Linux 배포판 / 셸에서 수행하는 방법을 살펴보아야합니다. 여기에는 여러 위치가 있습니다. 예를 들어 Ubuntu에서는 예를 들어 다음과 같은 파일에서 정의합니다.
~/.profile
/etc/environment
/etc/profile.d
예배 규칙서- ...
여기에서 Ubuntu 문서를 찾을 수 있습니다 : https://help.ubuntu.com/community/EnvironmentVariables
결국 ex에서 환경 변수를 설정하십시오. Ubuntu에서는 lineinfile
Ansible의 모듈을 사용 하고 특정 파일에 원하는 줄을 추가 할 수 있습니다. 영구적으로 만들기 위해 추가 할 위치를 알아 보려면 OS 문서를 참조하십시오.
나는 논평 할만한 충분한 명성을 얻지 못했기 때문에 새로운 답변을 추가하고 있습니다.
Gasek 대답은 아주 정확합니다. 한 가지, .bash_profile
파일 또는을 업데이트하는 경우 /etc/profile
이러한 변경 사항은 새 로그인을 수행 한 후에 만 반영됩니다. env 변수를 설정 한 다음 동일한 플레이 북의 후속 작업에서 사용하려는 경우입니다. .bashrc
파일 에 해당 환경 변수를 추가하는 것을 고려 하십시오.
그 이유는 로그인 및 비 로그인 쉘 때문이라고 생각합니다.
Ansible은 다른 작업을 실행하는 동안 또는 .bashrc
대신 파일 에서 매개 변수를 읽습니다 ..bash_profile
/etc/profile
예를 들어, .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";
이 일 것입니다 , 하지만 난 프로필 파일을 사용하여 수행 하였다. 는 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";
이것은 작동 실 거예요 , 우리는 같은 작전에서 모든 작업이있는 경우.
환경 변수를 지속적으로 설정하려면 Ansible Galaxy에서 기존 역할 중 하나를 사용할 수 있습니다. franklinkim.environment 권장 합니다.
ansible-galaxy 사용 :
$ ansible-galaxy install franklinkim.environment
requirements.yml 사용 :
- src: franklinkim.environment
그런 다음 플레이 북에서 :
- hosts: all
sudo: yes
roles:
- role: franklinkim.environment
environment_config:
NODE_ENV: staging
DATABASE_NAME: staging
Here's a quick local task to set key/values on /etc/environment
(which is system-wide, all users):
- name: populate /etc/environment
lineinfile:
dest: "/etc/environment"
state: present
regexp: "^{{ item.key }}"
line: "{{ item.key }}={{ item.value}}"
with_items: "{{ os_environment }}"
and the vars for it:
os_environment:
- key: DJANGO_SETTINGS_MODULE
value : websec.prod_settings
- key: DJANGO_SUPER_USER
value : admin
and, yes, if you ssh out and back in, env
shows the new environment variables.
This is the best option. As said Michal Gasek (first answer), since the pull request was merged (https://github.com/ansible/ansible/pull/8651), we are able to set permanent environment variables easily by play level.
- hosts: all
roles:
- php
- nginx
environment:
MY_ENV_VARIABLE: whatever_value
참고URL : https://stackoverflow.com/questions/27733511/how-to-set-linux-environment-variables-with-ansible
'program story' 카테고리의 다른 글
Java에서 문자열 배열 초기화 (0) | 2020.09.24 |
---|---|
@Autowired 및 정적 메서드 (0) | 2020.09.24 |
파이썬에서 int ()의 직관적이지 않은 동작 (0) | 2020.09.23 |
2보다 큰 MySQL 수 발생 (0) | 2020.09.23 |
Android 외부 저장소에 파일 저장 (0) | 2020.09.23 |