community.rabbitmq.rabbitmq_publish 模块 – 向 RabbitMQ 队列发布消息。

注意

此模块是 community.rabbitmq 集合(版本 1.3.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.rabbitmq。您需要进一步的要求才能使用此模块,请参阅 要求 以了解详细信息。

要在 playbook 中使用它,请指定:community.rabbitmq.rabbitmq_publish

概要

  • 使用阻塞连接在 RabbitMQ 队列上发布消息。

要求

在执行此模块的主机上需要以下要求。

  • pika

参数

参数

注释

auto_delete

布尔值

设置队列为自动删除。

选择

  • false ← (默认)

  • true

body

字符串

消息的主体。

如果指定了 src,则不能提供 body

cafile

字符串

在通过 SSL 连接到 RabbitMQ 服务器期间使用的 CA 文件。

如果指定了此选项,则还必须指定 *certfile* 和 *keyfile*。

certfile

字符串

用于建立 SSL 连接的客户端证书。

如果指定了此选项,则还必须指定 *cafile* 和 *keyfile*。

content_type

字符串

主体的内容类型。

默认: "text/plain"

durable

布尔值

将队列设置为持久化。

选择

  • false ← (默认)

  • true

exchange

字符串

要向其发布消息的交换机。

如果指定了 queue,则不能提供 exchange

exclusive

布尔值

将队列设置为独占。

选择

  • false ← (默认)

  • true

headers

字典

要与消息一起发布的标头字典。

默认: {}

host

字符串

RabbitMQ 服务器主机名或 IP。

keyfile

字符串

用于建立 SSL 连接的客户端密钥。

如果指定了此选项,则还必须指定 *cafile* 和 *certfile*。

password

字符串

RabbitMQ 密码。

port

整数

RabbitMQ 服务器端口。

proto

字符串

要使用的协议。

选择

  • "amqps"

  • "amqp"

queue

字符串

要向其发布消息的队列。如果未指定队列,RabbitMQ 将返回一个随机队列名称。

如果指定了 exchange,则不能提供 queue

routing_key

字符串

路由键。

src

别名: file

路径

要上传到队列的文件。如果未定义 content_type(保留为默认值),则会尝试自动 MIME 类型检测。

如果指定了 body,则不能提供 src

文件名将添加到发布到 RabbitMQ 的消息的标头中。键是 filename,值是文件名。

url

字符串

用于连接到 RabbitMQ 服务器的 URL 连接字符串。

urlhost/port/user/pass/vhost 是互斥的,请使用其中一个,但不能同时使用两者。

username

字符串

RabbitMQ 用户名。

vhost

字符串

要定向的虚拟主机。

如果需要默认虚拟主机,请使用 '%2F'

注释

注意

  • 此模块需要 pika python 库 https://pika.readthedocs.io/

  • Pika 是 AMQP 0-9-1 协议的纯 Python 实现,它试图保持与底层网络支持库的相对独立性。

  • 此模块针对 RabbitMQ 进行了测试。其他基于 AMQP 0.9.1 协议的服务器可能有效,但未经过测试/保证。

  • 证书身份验证已通过使用 https://rabbitmq.cn/ssl.html#automated-certificate-generation 创建的证书以及 RabbitMQ 配置变量 ssl_options.verify = verify_peer & ssl_options.fail_if_no_peer_cert = true 进行测试。

示例

- name: Publish to an exchange
  community.rabbitmq.rabbitmq_publish:
    exchange: exchange1
    url: "amqp://guest:guest@192.168.0.32:5672/%2F"
    body: "Hello exchange from ansible module rabbitmq_publish"
    content_type: "text/plain"

- name: Publish to an exchange with routing_key
  community.rabbitmq.rabbitmq_publish:
    exchange: exchange1
    routing_key: queue1
    url: "amqp://guest:guest@192.168.0.32:5672/%2F"
    body: "Hello queue via exchange routing_key from ansible module rabbitmq_publish"
    content_type: "text/plain"

- name: Publish a message to a queue with headers
  community.rabbitmq.rabbitmq_publish:
    url: "amqp://guest:guest@192.168.0.32:5672/%2F"
    queue: 'test'
    body: "Hello world from ansible module rabbitmq_publish"
    content_type: "text/plain"
    headers:
      myHeader: myHeaderValue

- name: Publish a file to a queue
  community.rabbitmq.rabbitmq_publish:
    url: "amqp://guest:guest@192.168.0.32:5672/%2F"
    queue: 'images'
    file: 'path/to/logo.gif'

- name: RabbitMQ auto generated queue
  community.rabbitmq.rabbitmq_publish:
    url: "amqp://guest:guest@192.168.0.32:5672/%2F"
    body: "Hello world random queue from ansible module rabbitmq_publish"
    content_type: "text/plain"

- name: Publish with certs
  community.rabbitmq.rabbitmq_publish:
    url: "amqps://guest:guest@192.168.0.32:5671/%2F"
    body: "Hello test queue from ansible module rabbitmq_publish via SSL certs"
    queue: 'test'
    content_type: "text/plain"
    cafile: 'ca_certificate.pem'
    certfile: 'client_certificate.pem'
    keyfile: 'client_key.pem'

返回值

常见的返回值记录在这里,以下是此模块特有的字段

描述

结果

字典

如果发布到交换机,结果包含状态msg,内容类型content_type,交换机名称exchange

以及路由键routing_key

如果发布到队列,结果包含状态msg,内容类型content_type和队列名称queue

返回: 成功

示例: "'result': { 'content_type': 'text/plain', 'msg': 'Successfully published to queue test', 'queue': 'test' }\n"

作者

  • John Imison (@Im0)