来自 Ansible 的 Slack 消息以纯文本形式发送 json

Slack message from Ansible is being sent as plaint text json

我正在使用 Ansible 通过 ansible guidelines 向 Slack 发送消息,但消息没有格式化。例如,如果我有

- name: "Slack test"
    slack:
      token: "abc123"
      channel: "some_channel"
      color: good
      msg: '{"text": "This is a line of text.\nAnd this is another one."}'

在我的 Ansible 任务中,然后它将 post 未格式化的 json {"text": "This is a line of text.\nAnd this is another one."} 发送到 Slack 频道。我怎样才能像 Slack's message formatting guide 那样格式化 JSON 消息?

我认为您没有使用正确的 Ansible 语法。

根据您链接的文档,msg 属性 应该直接包含消息的文本,而不是具有附加属性的 JSON 结构。

所以这应该是一个更正的例子:

- name: "Slack test"
    slack:
      token: "abc123"
      channel: "some_channel"
      color: good
      msg: "This is a line of text.\nAnd this is another one."

要为您的文本添加格式,您应该能够在消息 属性 中使用 Slack 标记。粗体示例:

- name: "Slack test"
        slack:
          token: "abc123"
          channel: "some_channel"
          color: good
          msg: "This is a *bold line* of text.\nAnd this is another one."