在Ansible中从bash命令的命令输出设置环境变量

我想在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}

在我的终端,它给出了一个空的结果.

任何帮助,将不胜感激.

至少有两个问题:

>您应该将copy_config.stdout作为变量传递

- 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的值.这就是系统的工作原理.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...