一、在企业环境中,如果管理的服务器越来越多,ansible的执行效率会变的很慢,可通过优化Ansible提高工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ansible执行变得缓慢,也需对OpenSSH进行优化。

具体优化方法:

1、Ansible SSH关闭密钥检测。

  修改ansible的配置文件/etc/ansible/ansible.cfg

host_key_checking = False

关闭检查

2、OpenSSH连接优化

  使用OpenSSH服务时,默认服务端配置文件UseDNS=YES,该选项会导致服务器根据客户端的IP地址进行DNS PTR反向解析。

关闭DNS解析:

[root@localhost ansible]# sed -i '/^GSSAPI/s/yes/no/g; /UseDNS/d; /Protocol/aUseDNS no' /etc/ssh/sshd_config
[root@localhost ansible]# systemctl restart sshd

3、SSH pipelining 加速Ansible

  SSH pipelinling 是一个加速 Ansible 执行速度的简单方法,SSH pipelinling 默认是关闭的。如果不适用sudo建议开启该选项,打开此选项可以减少Ansible执行没有文件传输时,SSH在被控机器上执行的连接数。

[root@localhost ansible]# vim /etc/ansible/ansible.cfg 
# By default, this option is disabled to preserve compatibility with
# sudoers configurations that have requiretty (the default on many distros).
#
pipelining = True

4、Ansible facts 缓存优化

  Ansible PlayBook在执行过程中,默认会执行Gather facts,如果不需要获取客户端的fact数据的话,可以关闭。

  关闭fact功能,在 PlayBook yaml 文件中加入:

gather_facts: no

5、ControlPersist SSH 优化

  CrontrolPersist 需要高版本的SSH支持,centos6默认不支持。

  CrontrolPersist 即持久化socket,一次验证证多次通信。

  只需要修改SSH客户端的配置,如果ansible以root登录,只需在客户端的 /root/.ssh/config 目录中添加以下代码:

HOST *
  Compression yes
  ServerAliveInterval 60
  ServerAliveCountMax 5
  ControlMaster auto
  ControlPath ~/.ssh/sockets/ % r@ % h- %p
  ControlPersist 4h
HOST *
  Compression yes             #是否压缩会话              
  ServerAliveInterval 60    #60s之内没有操作就会断开
  ServerAliveCountMax 5      #最大5个连接
  ControlMaster auto      #开启
  ControlPath ~/.ssh/sockets/ % r@ % h- %p  #会话文件路径
  ControlPersist 4h       #断开连接,会话文件保持4个小时

  

 

posted on 2023-01-03 13:56  草根920  阅读(568)  评论(0编辑  收藏  举报