且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ansible - 将注册的变量保存到文件

更新时间:2023-01-16 14:24:48

感谢 tmoschou 将此评论添加到过时的接受答案:

Thanks to tmoschou for adding this comment to an outdated accepted answer:

As of Ansible 2.10, The documentation for ansible.builtin.copy says: 

If you need variable interpolation in copied files, use the
ansible.builtin.template module. Using a variable in the content field will
result in unpredictable output.

有关详细信息,请参阅this说明

For more details see this and an explanation

原答案:

您可以使用带有参数 content=copy 模块.

You can use the copy module, with the parameter content=.

我在这里给出了完全相同的答案:将变量写入Ansible 中的一个文件

I gave the exact same answer here: Write variable to a file in Ansible

在您的情况下,您似乎希望将此变量写入本地日志文件,因此您可以将其与 local_action 符号结合起来:

In your case, it looks like you want this variable written to a local logfile, so you could combine it with the local_action notation:

- local_action: copy content={{ foo_result }} dest=/path/to/destination/file