From a6f134eada167f7193a2263ca9d9403b12359689 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Tue, 6 Oct 2026 02:42:13 +0800 Subject: [PATCH] =?UTF-8?q?ansible:=20=E5=B0=86=20TCP=20=E6=8B=A5=E5=A1=9E?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E7=AE=97=E6=B3=95=E5=88=87=E6=8D=A2=E5=88=B0?= =?UTF-8?q?=20BBR=20=E6=8F=90=E5=8F=96=E6=88=90=E4=B8=80=E4=B8=AA=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ansible/ansible.cfg | 1 + ansible/playbooks/vps_debian_provision.yaml | 10 +++------- ansible/roles/bbr/README.org | 9 +++++++++ ansible/roles/bbr/handlers/main.yaml | 4 ++++ ansible/roles/bbr/tasks/config.yaml | 15 +++++++++++++++ ansible/roles/bbr/tasks/main.yaml | 4 ++++ ansible/roles/bbr/tasks/module.yaml | 13 +++++++++++++ ansible/roles/bbr/tasks/verify.yaml | 19 +++++++++++++++++++ 8 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 ansible/roles/bbr/README.org create mode 100644 ansible/roles/bbr/handlers/main.yaml create mode 100644 ansible/roles/bbr/tasks/config.yaml create mode 100644 ansible/roles/bbr/tasks/main.yaml create mode 100644 ansible/roles/bbr/tasks/module.yaml create mode 100644 ansible/roles/bbr/tasks/verify.yaml diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg index eb870e3..349fcee 100644 --- a/ansible/ansible.cfg +++ b/ansible/ansible.cfg @@ -1,2 +1,3 @@ [defaults] inventory = ./inventory.ini +roles_path = ./roles \ No newline at end of file diff --git a/ansible/playbooks/vps_debian_provision.yaml b/ansible/playbooks/vps_debian_provision.yaml index 5570432..10799d1 100644 --- a/ansible/playbooks/vps_debian_provision.yaml +++ b/ansible/playbooks/vps_debian_provision.yaml @@ -12,14 +12,10 @@ timezone: Asia/Shanghai ssh_public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" - tasks: - - name: 启用 BBR - sysctl: - name: net.ipv4.tcp_congestion_control - value: bbr - sysctl_set: yes - reload: yes + roles: + - role: bbr + tasks: - name: 设置时区 timezone: name: "{{ timezone }}" diff --git a/ansible/roles/bbr/README.org b/ansible/roles/bbr/README.org new file mode 100644 index 0000000..e992b94 --- /dev/null +++ b/ansible/roles/bbr/README.org @@ -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 模块。 diff --git a/ansible/roles/bbr/handlers/main.yaml b/ansible/roles/bbr/handlers/main.yaml new file mode 100644 index 0000000..629143c --- /dev/null +++ b/ansible/roles/bbr/handlers/main.yaml @@ -0,0 +1,4 @@ +--- +- name: reload sysctl + command: sysctl --system + changed_when: false diff --git a/ansible/roles/bbr/tasks/config.yaml b/ansible/roles/bbr/tasks/config.yaml new file mode 100644 index 0000000..3e2d342 --- /dev/null +++ b/ansible/roles/bbr/tasks/config.yaml @@ -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 diff --git a/ansible/roles/bbr/tasks/main.yaml b/ansible/roles/bbr/tasks/main.yaml new file mode 100644 index 0000000..5d54a0d --- /dev/null +++ b/ansible/roles/bbr/tasks/main.yaml @@ -0,0 +1,4 @@ +--- +- import_tasks: config.yaml +- import_tasks: module.yaml +- import_tasks: verify.yaml diff --git a/ansible/roles/bbr/tasks/module.yaml b/ansible/roles/bbr/tasks/module.yaml new file mode 100644 index 0000000..5669215 --- /dev/null +++ b/ansible/roles/bbr/tasks/module.yaml @@ -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 diff --git a/ansible/roles/bbr/tasks/verify.yaml b/ansible/roles/bbr/tasks/verify.yaml new file mode 100644 index 0000000..ac1ce2b --- /dev/null +++ b/ansible/roles/bbr/tasks/verify.yaml @@ -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 }}"