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 
参数
| 参数 | 注释 | 
|---|---|
| 设置队列为自动删除。 选择 
 | |
| 消息的主体。 如果指定了  | |
| 在通过 SSL 连接到 RabbitMQ 服务器期间使用的 CA 文件。 如果指定了此选项,则还必须指定 *certfile* 和 *keyfile*。 | |
| 用于建立 SSL 连接的客户端证书。 如果指定了此选项,则还必须指定 *cafile* 和 *keyfile*。 | |
| 主体的内容类型。 默认:  | |
| 将队列设置为持久化。 选择 
 | |
| 要向其发布消息的交换机。 如果指定了  | |
| 将队列设置为独占。 选择 
 | |
| 要与消息一起发布的标头字典。 默认:  | |
| RabbitMQ 服务器主机名或 IP。 | |
| 用于建立 SSL 连接的客户端密钥。 如果指定了此选项,则还必须指定 *cafile* 和 *certfile*。 | |
| RabbitMQ 密码。 | |
| RabbitMQ 服务器端口。 | |
| 要使用的协议。 选择 
 | |
| 要向其发布消息的队列。如果未指定队列,RabbitMQ 将返回一个随机队列名称。 如果指定了  | |
| 路由键。 | |
| 要上传到队列的文件。如果未定义 content_type(保留为默认值),则会尝试自动 MIME 类型检测。 如果指定了  文件名将添加到发布到 RabbitMQ 的消息的标头中。键是  | |
| 用于连接到 RabbitMQ 服务器的 URL 连接字符串。 url 和 host/port/user/pass/vhost 是互斥的,请使用其中一个,但不能同时使用两者。 | |
| RabbitMQ 用户名。 | |
| 要定向的虚拟主机。 如果需要默认虚拟主机,请使用  | 
注释
注意
- 此模块需要 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。 返回: 成功 示例:  | 
