我想在Ansible中将
shell命令的输出设置为环境变量.
我做了以下工作来实现它:
- name: copy content of config.json into variable shell: /bin/bash -l -c "cat /storage/config.json" register: copy_config tags: something - name: set config shell: "echo $TEMP_CONfig" environment: TEMP_CONfig: "{{copy_config}}" tags: something
但是在ansible运行之后,当我运行以下命令时:
echo ${TEMP_CONfig}
在我的终端,它给出了一个空的结果.
任何帮助,将不胜感激.
至少有两个问题:
- name: set config shell: "echo $TEMP_CONfig" environment: TEMP_CONfig: "{{copy_config.stdout}}" tags: something
- name: set config shell: "echo $TEMP_CONfig" environment: TEMP_CONfig: "{{copy_config.stdout}}" tags: something register: shell_echo - debug: var: shell_echo.stdout
>您永远无法以这种方式将变量传递给不相关的流程.因此,除非您在rc文件中注册结果(如〜/ .bash_profile,如果您使用Bash,这是在交互式登录时获取的),否则其他shell进程将无法看到TEMP_CONfig的值.这就是系统的工作原理.