community.general.haproxy 模块 – 使用套接字命令启用、禁用和设置 HAProxy 后端服务器的权重

注意

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

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

要安装它,请使用:ansible-galaxy collection install community.general

要在 playbook 中使用它,请指定:community.general.haproxy

概要

  • 使用套接字命令启用、禁用、排空和设置 HAProxy 后端服务器的权重。

参数

参数

注释

agent

布尔值

在 community.general 1.0.0 中添加

禁用/启用代理检查(取决于 state 值)。

选项

  • false ← (默认)

  • true

backend

字符串

HAProxy 后端池的名称。

如果此参数未设置,它将自动检测。

drain

布尔值

等待直到服务器没有活动连接,或者直到由 wait_interval 和 wait_retries 确定的超时时间到达。

仅在状态更改为 MAINT 后继续。

这将覆盖 shutdown_sessions 选项。

选项

  • false ← (默认)

  • true

fail_on_not_found

布尔值

在尝试启用/禁用不存在的后端主机时失败。

选项

  • false ← (默认)

  • true

health

布尔值

在 community.general 1.0.0 中添加

禁用/启用健康检查(取决于 state 值)。

选项

  • false ← (默认)

  • true

host

字符串 / 必需

要更改的后端主机的名称。

shutdown_sessions

布尔值

禁用服务器时,立即终止附加到指定服务器的所有会话。

这可用于在服务器进入维护模式后终止长时间运行的会话。被 drain 选项覆盖。

选项

  • false ← (默认)

  • true

socket

路径

HAProxy 套接字文件的路径。

默认: "/var/run/haproxy.sock"

state

字符串 / 必需

提供的后端主机的所需状态。

请注意,drain 状态仅受 HAProxy 1.5 或更高版本支持。在 < 1.5 版本上使用时,它将被忽略。

选项

  • "disabled"

  • "drain"

  • "enabled"

wait

布尔值

state=enabled 时,等待直到服务器报告状态为 UP;当 state=disabled 时,状态为 MAINT;当 state=drain 时,状态为 DRAIN

选项

  • false ← (默认)

  • true

wait_interval

整数

两次重试之间等待的秒数。

默认: 5

wait_retries

整数

更改状态后检查状态的次数。

默认: 25

weight

字符串

传入的参数值。

如果值以 % 符号结尾,则新的权重将相对于最初配置的权重。

相对权重仅允许在 0 和 100% 之间,绝对权重允许在 0 和 256 之间。

属性

属性

支持

描述

check_mode

支持:不支持

可以在 check_mode 中运行,并在不修改目标的情况下返回更改状态预测。

diff_mode

支持:不支持

在差异模式下,将返回有关已更改内容(或可能需要在 check_mode 中更改)的详细信息。

备注

注意

  • 启用、禁用和排空命令受到限制,只能在配置为级别“admin”的套接字上发出。例如,您可以将行“stats socket /var/run/haproxy.sock level admin”添加到 haproxy.cfg 的 general 部分。请参见 http://haproxy.1wt.eu/download/1.5/doc/configuration.txt

  • 取决于 netcat (nc) 是否可用;您需要在可以使用此模块之前安装适合您操作系统的相应软件包。

示例

- name: Disable server in 'www' backend pool
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    backend: www

- name: Disable server in 'www' backend pool, also stop health/agent checks
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    health: true
    agent: true

- name: Disable server without backend pool name (apply to all available backend pool)
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'

- name: Disable server, provide socket file
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    socket: /var/run/haproxy.sock
    backend: www

- name: Disable server, provide socket file, wait until status reports in maintenance
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    socket: /var/run/haproxy.sock
    backend: www
    wait: true

# Place server in drain mode, providing a socket file.  Then check the server's
# status every minute to see if it changes to maintenance mode, continuing if it
# does in an hour and failing otherwise.
- community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    socket: /var/run/haproxy.sock
    backend: www
    wait: true
    drain: true
    wait_interval: 60
    wait_retries: 60

- name: Disable backend server in 'www' backend pool and drop open sessions to it
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    backend: www
    socket: /var/run/haproxy.sock
    shutdown_sessions: true

- name: Disable server without backend pool name (apply to all available backend pool) but fail when the backend host is not found
  community.general.haproxy:
    state: disabled
    host: '{{ inventory_hostname }}'
    fail_on_not_found: true

- name: Enable server in 'www' backend pool
  community.general.haproxy:
    state: enabled
    host: '{{ inventory_hostname }}'
    backend: www

- name: Enable server in 'www' backend pool wait until healthy
  community.general.haproxy:
    state: enabled
    host: '{{ inventory_hostname }}'
    backend: www
    wait: true

- name: Enable server in 'www' backend pool wait until healthy. Retry 10 times with intervals of 5 seconds to retrieve the health
  community.general.haproxy:
    state: enabled
    host: '{{ inventory_hostname }}'
    backend: www
    wait: true
    wait_retries: 10
    wait_interval: 5

- name: Enable server in 'www' backend pool with change server(s) weight
  community.general.haproxy:
    state: enabled
    host: '{{ inventory_hostname }}'
    socket: /var/run/haproxy.sock
    weight: 10
    backend: www

- name: Set the server in 'www' backend pool to drain mode
  community.general.haproxy:
    state: drain
    host: '{{ inventory_hostname }}'
    socket: /var/run/haproxy.sock
    backend: www

作者

  • Ravi Bhure (@ravibhure)