root@xs4772:~# ansible all -m shell -a "cpupower frequency-info | grep 'governor' | head -n1"
xs4772 | CHANGED | rc=0 >>
available cpufreq governors: conservative ondemand userspace powersave performance schedutil
xs4773 | CHANGED | rc=0 >>
available cpufreq governors: conservative ondemand userspace powersave performance schedutil
xs4774 | CHANGED | rc=0 >>
available cpufreq governors: conservative ondemand userspace powersave performance schedutil
root@xs4772:~#
以下是一键调整为性能模式并固化配置的完整命令(适用于Debian/Ubuntu系,RHEL/CentOS系可参考注释调整):
1. 临时切换为性能模式(立即生效)
ansible all -m shell -a "cpupower frequency-set -g performance"
2. 永久固化配置(重启后生效)
方法1:通过cpupower服务持久化(推荐)
# 编辑cpupower配置文件,设置默认调速器为performance
ansible all -m copy -a "content='GOVERNOR=\"performance\"\n' dest=/etc/default/cpupower mode=0644"
# 启用并重启cpupower服务
ansible all -m service -a "name=cpupower state=restarted enabled=yes"
方法2:通过grub内核参数(备选,适合所有系统)
# 备份grub配置(防止出错)
ansible all -m shell -a "cp /etc/default/grub /etc/default/grub.bak"
# 修改grub配置,添加CPU调速器参数
ansible all -m replace -a "path=/etc/default/grub regexp='^(GRUB_CMDLINE_LINUX_DEFAULT=\".*)\$' replace='\1 cpufreq.default_governor=performance\"'"
# 更新grub配置(Debian/Ubuntu)
ansible all -m shell -a "update-grub"
# 若为RHEL/CentOS系,执行:
# ansible all -m shell -a "grub2-mkconfig -o /boot/grub2/grub.cfg"
3. 验证配置是否生效
# 查看当前调速器
ansible all -m shell -a "cpupower frequency-info | grep -oP 'governor \"\K\w+'"
# 查看cpupower配置文件
ansible all -m shell -a "cat /etc/default/cpupower"
说明:
- 方法1(cpupower服务):无需重启系统,配置立即永久生效,优先推荐。
- 方法2(grub参数):需重启系统才能生效,适合需要内核级固化的场景。
- 若需恢复平衡模式,只需将上述命令中的
performance改为schedutil即可。
执行完上述命令后,所有节点会永久保持性能模式,重启后也不会还原。