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 后端服务器的权重。 
参数
| 参数 | 注释 | 
|---|---|
| HAProxy 后端池的名称。 如果此参数未设置,它将自动检测。 | |
| 等待直到服务器没有活动连接,或者直到由 wait_interval 和 wait_retries 确定的超时时间到达。 仅在状态更改为  这将覆盖 shutdown_sessions 选项。 选项 
 | |
| 在尝试启用/禁用不存在的后端主机时失败。 选项 
 | |
| 要更改的后端主机的名称。 | |
| 禁用服务器时,立即终止附加到指定服务器的所有会话。 这可用于在服务器进入维护模式后终止长时间运行的会话。被 drain 选项覆盖。 选项 
 | |
| HAProxy 套接字文件的路径。 默认:  | |
| 提供的后端主机的所需状态。 请注意, 选项 
 | |
| 当  选项 
 | |
| 两次重试之间等待的秒数。 默认:  | |
| 更改状态后检查状态的次数。 默认:  | |
| 传入的参数值。 如果值以  相对权重仅允许在 0 和 100% 之间,绝对权重允许在 0 和 256 之间。 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持:不支持 | 可以在  | |
| 支持:不支持 | 在差异模式下,将返回有关已更改内容(或可能需要在  | 
备注
注意
- 启用、禁用和排空命令受到限制,只能在配置为级别“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
