開發與維運

Ansible性能優化

Ansible性能優化
1、優化前的準備--收集數據
任務計時插件:"ansible-profile"
Github 地址:https://github.com/jlafon/ansible-profile

cd /etc/ansible 
mkdir callback_plugins 
cd callback_plugins 
wget https://raw.githubusercontent.com/jlafon/ansible-profile/master/callback_plugins/profile_tasks.py


sed -i s#'\#callback_whitelist = timer, mail'#'callback_whitelist = profile_tasks'#g /etc/ansible/ansible.cfg

2、關閉Gathering Facts

playbook文件中加上 "gather_facts: no"

---
- hosts: web
  gather_facts: no
  remote_user: root

3、ssh pipelining --加速ansible執行速度
ssh pipelining 默認是關閉,之所以默認關閉是為了兼容不同的 sudo 配置,主要是 requiretty 選項。
如果使用sudo,必須關閉requiretty
打開此選項可以減少 ansible 執行沒有傳輸時 ssh 在被控機器上執行任務的連接數。

#開啟pipelining;/etc/ansible/ansible.cfg
pipelining = False  --> pipelining = true

4、Controlpersist
ControlPersist 特性需要高版本的 SSH 才支持,CentOS 6 默認是不支持的,如果需要使用,需要自行升級 openssh。ControlPersist 即持久化 socket,一次驗證,多次通信。並且只需要修改 ssh 客戶端就行,也就是 Ansible 機器即可。

# cat ~/.ssh/config 
Host * 
 Compression yes 
 ServerAliveInterval 60 
 ServerAliveCountMax 5 
 ControlMaster auto 
 ControlPath <a href="mailto:~/.ssh/sockets/%25r@%25h-%25p"><code>~/.ssh/sockets/%r@%h-%p</code></a>
 ControlPersist 4h

Leave a Reply

Your email address will not be published. Required fields are marked *