我需要设置环境变量以在启动时构建一些应用程序,我在playbook文件中使用vars
:
---
- hosts: all
sudo: yes
vars:
build_env:
VAR1: value1
VAR2: value2
VAR3: value3
并将environment
设置为任务以构建应用
- name: Build app
environment: "{{build_env}}"
command: "{{ item }} chdir={{app1}}"
with_items:
- npm install
- bower
- gulp
另外我需要向/etc/environment
添加相同的变量,但我不会在另一个任务中复制相同的变量,任何想法?
答案 0 :(得分:0)
ansible-playbook -c local -i 127.0.0.1,test.yml
test.yml:
---
- hosts: all
sudo: yes
vars:
build_env:
VAR1: value1
VAR2: value2
VAR3: value3
tasks:
- name: Build app
environment: "{{build_env}}"
shell: source /etc/environment; echo $VAR1 > ~/111
- name: Build app
environment: "{{build_env}}"
shell: source /etc/environment;env >> /root/111
经过测试的变种:
# cat /etc/environment
VAR1=2
export VALERY=x
export VAR2=ss
CCCCCCCCCC=xx
结果:
# cat ~/111
value1
HOSTNAME=38b07f75ca28
SHLVL=1
HOME=/root
VALERY=x
_=/usr/local/bin/ansible-playbook
TERM=xterm
VAR1=value1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
VAR2=value2
VAR3=value3
LANG=C
LC_MESSAGES=C
PWD=/
LC_ALL=C
Conclusiosn:
CCCCCCCCCC=xx
- 未导出
environment:
覆盖变量,因此您应该导出所有变量
/etc/environment