Ansible - 将注册的变量保存到文件 [英] Ansible - Save registered variable to file
问题描述
如何将已注册的变量保存到文件中?我从教程:
How would I save a registered Variable to a file? I took this from the tutorial:
- hosts: web_servers
tasks:
- shell: /usr/bin/foo
register: foo_result
ignore_errors: True
- shell: /usr/bin/bar
when: foo_result.rc == 5
我如何将 foo_result
变量保存到文件中,例如foo_result.log
使用 ansible?
How would I save foo_result
variable to a file e.g. foo_result.log
using ansible?
推荐答案
感谢 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.
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
这篇关于Ansible - 将注册的变量保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!