{
"dashboard": {{ item.json | indent(width=2) | trim }} ,{{ "\n" }}
{%- if 'folder_uid' in item %}
"folderUid": "{{ item.folder_uid }}",{{ "\n" }}
{%- endif %}
"overwrite": true
}
上面的 item.json 变量包含仪表板 json 数据我们尝试了以下所有方法
所以,我们正在尝试#2。谁能帮助我们如何用 {{ '{{' }} 替换 {{ 并用 {{ '}}' }} 模式替换 }} ?问题是我们无法进行简单的文本替换,因为替换 {{ 也会引入 }}。
“以 JSON 文件导出的 grafana 仪表板”
shell> cat test-152.json
{
"dashboard": "region {{ region }} state {{ state }}"
}
将 JSON 数据包含在字典中,例如
- set_fact:
content: "{{ lookup('file', 'test-152.json') }}"
创建字典内容
content:
dashboard: region {{ region }} state {{ state }}
接下来创建一个Ansible模板并引用其属性
“仪表板 JSON(该)可能具有仪表板变量引用,例如 {{region }}、{{state }})”
shell> cat test-152.txt.j2
# Grafana dashboard created by Ansible
# Created {{ '%Y-%m-%d' | strftime }}
{{ content.dashboard }}
Ansible 任务
- template:
src: test-152.txt.j2
dest: test-152.txt
创建文件
shell> cat test-152.txt
# Grafana dashboard created by Ansible
# Created 2021-08-05
region {{ region }} state {{ state }}
使模板适合您的需求。
模板模块 包括 variable_end_string
和
variable_start_string
参数,可能会帮助您解决此问题。例如,您可以将
{{
和
}}
替换为
{{{{
和
}}}}
。