ansible: 将 TCP 拥塞控制算法切换到 BBR 提取成一个 role

This commit is contained in:
zedhugh committed 2026-10-06 02:42:13 +08:00
1 parent d3dc97ad2b
commit a6f134eada
8 files changed
+68 -7

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
#+title: TCP 拥塞控制算法设置为 BBR
这个 role 是为了将服务器上的 linux 系统的 TCP 拥塞控制算法设置为 BBR 以提供更好的网络环境,
主要时提高吞吐量,降低延迟和提升弱网稳定性。
最开始是使用 =ansible= 的 =sysctl= 模块实现的,配置是写入 =/etc/sysctl.conf= 的,但后面发现重启机器后配置未生效。
查询原因发现是 =systemd-sysctl= 不会读取 =/etc/sysctl.conf= ,读取的是 =/etc/sysctl.d= 目录下的配置文件。
因此配置方式改为手动写入配置文件,不再直接使用 ansible 的 sysctl 模块。
+4
View File
@@ -0,0 +1,4 @@
---
- name: reload sysctl
command: sysctl --system
changed_when: false
+15
View File
@@ -0,0 +1,15 @@
---
- name: 启用 BBR
copy:
dest: /etc/sysctl.d/bbr.conf
content: |
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
mode: '0644'
owner: root
group: root
notify: reload sysctl
- name: 应用 sysctl 配置
command: sysctl --system
changed_when: false
+4
View File
@@ -0,0 +1,4 @@
---
- import_tasks: config.yaml
- import_tasks: module.yaml
- import_tasks: verify.yaml
+13
View File
@@ -0,0 +1,13 @@
---
- name: 开机加载 tcp_bbr 模块
copy:
dest: /etc/modules-load.d/bbr.conf
content: "tcp_bbr\n"
mode: '0644'
owner: root
group: root
- name: 立即加载 tcp_bbr 模块
modprobe:
name: tcp_bbr
state: present
+19
View File
@@ -0,0 +1,19 @@
---
- name: 获取可用的 TCP 拥塞控制算法列表
command: sysctl net.ipv4.tcp_available_congestion_control
register: available_cc
changed_when: false
- name: 确认 bbr 在可用列表中
fail:
msg: "BBR 不在可用的 TCP 拥塞控制算法列表中"
when: "'bbr' not in available_cc.stdout"
- name: 验证当前拥塞控制算法
command: sysctl net.ipv4.tcp_congestion_control
register: current_cc
changed_when: false
- name: 打印当前 TCP 拥塞控制算法
debug:
msg: "当前 TPC 拥塞控制算法: {{ current_cc.stdout }}"