在 Ansible 中将变量写入文件 [英] Write variable to a file in Ansible

查看:39
本文介绍了在 Ansible 中将变量写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 URI 模块提取 JSON,并希望将接收到的内容写入文件.我能够获取内容并将其输出到调试器,因此我知道已收到内容,但我不知道编写文件的最佳做法.

I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do not know the best practice for writing files.

推荐答案

来自 tmoschou 的重要评论:

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 could use the copy module, with the content parameter:

- copy: content="{{ your_json_feed }}" dest=/path/to/destination/file

此处的文档:复制模块

这篇关于在 Ansible 中将变量写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆