[英]Ansible set environment variable from a file and access it within the playbook
我有以下在本地主機上運行的 Ansible 腳本
- name: set env
shell: ". /tmp/testenv"
- name: get env
debug:
msg: "{{ lookup('env','TEST') }}"
在上面的腳本中,我嘗試使用查找來獲取文件並訪問環境變量。 但似乎沒有設置環境變量。 無論如何我可以讓它工作嗎?
shell 命令是一個進程,查找在 Ansible 進程的環境中查找,這是一個不同的進程。 您必須在 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.