ansible - 如何使用 ansible.builtin.template 模块在文件中写入变量值?
问题描述
我想在文件中记录变量的内容。我是这样做的,
- name: write infraID to somefile.log
copy:
content: "{{ infraID.stdout }}"
dest: somefile.log
它奏效了。但是在copy
此处的文档中,我发现他们建议在这种情况下使用template
而不是。copy
对于高级格式或如果
content
包含变量,请使用 ansible.builtin.template 模块。
我在这里查看了模板模块文档中给出的示例。但是我无法弄清楚在我的场景中有效的东西。你能告诉我如何以推荐的方式正确地做到这一点吗?提前致谢 !!干杯!
解决方案
该template
模块没有content
属性。您必须创建一个包含模板的文件,例如:
模板/infra-id-模板
{{ infraID.stdoud }}
剧本
---
- hosts: localhost
tasks:
- name: Get infra ID
shell: echo "my-infra-id"
register: infraID
- name: Template the file
template:
src: infra-id-template
dest: infra-id-file
推荐阅读
- function - 如何通过函数控制 adobe animate 中的主时间线?
- python - 如何从 for 循环创建一个字典
- c++ - 这个对象被破坏两次是什么?
- javascript - 如何使用 js 删除 String 中的一定数量的选项卡?
- sql - 如何查找 IF 单行符合条件 ELSE 聚合组内的多行
- python - 如何在python中将一年中的整数天转换为格式mm/dd
- php - 更改 drupal 9 项目中的文件夹结构
- python - 如何矢量化这个(困难的)for循环?
- sql - 在 SQLite 中,将查询结果转置为计数值
- r - 过滤掉某个特定列的某个值以下的df的所有行