[英]Ansible set environment variable from a file and access it within the playbook
I have the below Ansible script which runs on localhost我有以下在本地主机上运行的 Ansible 脚本
- name: set env
shell: ". /tmp/testenv"
- name: get env
debug:
msg: "{{ lookup('env','TEST') }}"
In above script I'm trying to source the file and access the environment variables using the lookup.在上面的脚本中,我尝试使用查找来获取文件并访问环境变量。 But it seems like environment variable is not set.但似乎没有设置环境变量。 Is there anyway I can get this to work?无论如何我可以让它工作吗?
The shell command is one process and the lookup looks in the environment of the Ansible process, which is a different one. shell 命令是一个进程,查找在 Ansible 进程的环境中查找,这是一个不同的进程。 You have to echo the environment variable in your shell command and register the result.您必须在 shell 命令中回显环境变量并注册结果。
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: set env
shell: "echo 'export TEST=test' > /tmp/testenv"
- name: check env
shell: ". /tmp/testenv; echo $TEST"
register: result
- name: get env
debug:
msg: "{{ result.stdout }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.