You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解析Ansible输出的集群JVM配置并生成表格邮件通知?

问题解决:Ansible解析节点数据并生成表格邮件

语法错误修正

你遇到的模板报错源于Jinja2语法使用错误,修正方案如下:

  1. 批量提取节点名称:不能用nodes.*.name,需通过map(attribute='name')过滤列表元素属性:
- debug: msg="{{ jsoncontent.content.nodes | map(attribute='name') | list }}"
  1. 单个节点索引:nodes.[0].name多了一个多余的.,正确写法为:
- debug: msg="{{ jsoncontent.content.nodes[0].name }}"

额外说明:如果jsoncontent.content是已解析的结构化数据(比如通过uri模块获取的JSON返回值),无需添加from_yaml过滤器;仅当内容是YAML格式字符串时,才需要使用该过滤器解析。

生成表格并发送邮件示例

以下是将节点JVM配置整理为HTML表格并发送邮件的完整Playbook片段:

1. 假设节点数据结构

假设jsoncontent.content.nodes的格式如下:

[
  {"name": "node1", "jvm_config": {"heap_size": "4G", "metaspace_size": "512M"}},
  {"name": "node2", "jvm_config": {"heap_size": "8G", "metaspace_size": "1G"}}
]

2. 处理数据并生成HTML表格

- name: 提取节点JVM配置数据
  set_fact:
    node_jvm_data: "{{ jsoncontent.content.nodes | json_query('[].{节点名称: name, 堆内存: jvm_config.heap_size, 元空间: jvm_config.metaspace_size}') }}"

- name: 生成HTML表格文件
  template:
    src: jvm_table.j2
    dest: /tmp/jvm_config_table.html

对应的jvm_table.j2模板内容:

<table border="1">
  <tr>
    <th>节点名称</th>
    <th>堆内存</th>
    <th>元空间</th>
  </tr>
  {% for node in node_jvm_data %}
  <tr>
    <td>{{ node.节点名称 }}</td>
    <td>{{ node.堆内存 }}</td>
    <td>{{ node.元空间 }}</td>
  </tr>
  {% endfor %}
</table>

3. 发送HTML格式邮件

- name: 发送JVM配置汇总邮件
  mail:
    subject: 集群节点JVM配置汇总
    body: "{{ lookup('file', '/tmp/jvm_config_table.html') }}"
    to: admin@example.com
    from: ansible@example.com
    subtype: html

内容的提问来源于stack exchange,提问作者nonoob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 02:35:07