从 ansible 发送自适应 Microsoft 团队 Webhook,并使用具有动态行数的表

问题描述 投票:0回答:1

我想为 MS Teams webhook 创建一个自适应卡。数据来自 api 调用的结果,即 json。数据有 2 列,但行数是动态的。

下面我有一个模板和剧本。

模板-adaptive_card_template.json.j2:

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "text": "Dynamic Table Example",
      "size": "Medium",
      "weight": "Bolder"
    },
    {
      "type": "Container",
      "items": [
        {
          "type": "TextBlock",
          "text": "Column 1",
          "weight": "Bolder"
        },
        {
          "type": "TextBlock",
          "text": "Column 2",
          "weight": "Bolder"
        }
        {% raw %}
        {{ table_rows_placeholder }}
        {% endraw %}
      ]
    }
  ]
}

Playbook - webhook.yml:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - name: Get json from web api
      delegate_to: localhost
      uri:
        url: "web_url_website_api"
      method: GET
      headers:
        Content-Type: "application/json"
      register: json_data

    - name: Load template
      slurp:
        src: adaptive_card_template.json.j2
      register: adaptive_card_json

    - name: Replace table rows placeholder
      set_fact:
      adaptive_card_json: "{{ adaptive_card_json | combine({'body': adaptive_card_json['body'] | replace('{{ table_rows_placeholder }}', json_data)}) }}"

    - name: Send Adaptive Card to Teams Webhook
      uri:
        url: webhook_url
        method: POST
        body_format: json
        body: "{{ adaptive_card_json | to_json }}"
      delegate_to: localhost

错误:

创建自适应卡 JSON 给出错误:该任务包含一个带有未定义变量的选项。错误是:“dict object”没有属性

json ansible
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.