在Ansible中从bash命令的命令输出设置环境变量
发布时间:2020-12-16 01:36:49 所属栏目:安全 来源:网络整理
导读:我想在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: "e
我想在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的值.这就是系统的工作原理. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |