ansible优化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible优化相关的知识,希望对你有一定的参考价值。

参考技术A 在playbook执行过程中,ansible收集facts变量是很耗时的一个步骤,如果我们确定play中没有用到fact变量信息,可以直接将其关闭,关闭获取 facts 很简单,只需要在 playbook 文件中加上“ gather_facts: no ”即可。
我们可以给play单独添加一个 setup 模块,并通过 gather_subset 参数严格控制 facts 的收集种类,这样既拿到了我们需要的fact变量又提高了 ansible 的执行效率, gather_subset 参数的默认值为all。

forks用来设置同一时刻与目的主机连接数,也可以理解为主机并行数,默认值比较保守为5。在生产中,多数情况下我们会更改这个参数。如果控制节点的CPU和网络性能够用,设置几十上百个也是可以的
在ansible.cfg设置forks的全局默认值:

ansible使用ssh协议和被管控主机通信,开启长连接后会有一个established的连接
openssh5.6 以后的版本支持了 multiplexing ,如果管控机命令行 执行ssh -V得到的版本号大于5.6 就可以设置长连接

开启「 pipelining 」特性实际上是通过 减少ssh连接次数 ,从而 缩短ansible执行时间 。
在 部署大规模服务器或引用模块非常多 时,开启「pipelining」特性会给ansible带来显著的性能提升。

/etc/ansible/ansible.cfg 的 pipelining 参数设置为 True 即可,该参数默认值是False。

缺点: 如果我们要开启pipelining特性,要么 playbook 不使用 sudo 越权功能,要么取消 sudo 的「 requiretty 」特性。

Strategy

strategy的作用范围是一个play,通过设置不同参数,控制一个play内所有任务的执行策略。

设置方法为更改ansible.cfg里的strategy参数,默认值为linear,可选参数为free
linear :

free :

如果未为poll指定值,则默认轮询值为10秒
异步时间限制没有默认值。如果不使用'async'关键字,则任务将同步运行,这是Ansible的默认设置。

在异步执行任务时,需要注意那些有依赖性的任务。对于那些对资源要求占有排它锁的任务,如yum,不应该将Poll的间隔设置为0。如果设置为0,很可能会导致资源阻塞。

总结来说,大概有以下一些场景需要使用到ansible的异步特性:

当然也有一些场景不适合使用异步特性:

异步优化
参考链接

ansible 优化相关

[default]
callback_whitelist = profile_tasks
# The best way I’ve found to time the execution of Ansible playbooks is by enabling the profile_tasks callback. This callback is included with Ansible and all you need to do to enable it is add callback_whitelist = profile_tasks to the [defaults] section of your ansible.cfg:

[ssh_connection] 
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
#The first thing to check is whether SSH multiplexing is enabled and used. This gives a tremendous speed boost because Ansible can reuse opened SSH sessions instead of negotiating new one (actually more than one) for every task. Ansible has this setting turned on by default. It can be set in configuration file as follows:

[ssh_connection]
pipelining = True
# You can enable pipelining by simply adding pipelining = True to the [ssh_connection]area of your ansible.cfg or by by using the ANSIBLE_PIPELINING and ANSIBLE_SSH_PIPELINING environment variables.

[defaults]
strategy_plugins = /path/to/mitogen-0.2.5/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
# Enabling Mitogen for Ansible is as simple as downloading and extracting the plugin, then adding 2 lines to the [defaults] section of your ansible.cfg

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o PreferredAuthentications=publickey
#PreferredAuthentications  It is an SSH-client setting which informs server about preferred authentication methods. By default Ansible uses: -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey

[defaults]
forks = 20

[defaults]
internal_poll_interval = 0.001
#When module is executed on remote host, Ansible starts to poll for its result. The lower is interval between poll attempts, the higher is CPU load on Ansible control host. But we want to have CPU available for greater forks number (see above). You can tweak poll interval in  ansible.cfg

gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = /dev/shm/ansible_fact_cache
 

以上是关于ansible优化的主要内容,如果未能解决你的问题,请参考以下文章

Ansible之优化提升执行效率

自动化运维工具Ansible(12)调试与优化 Ansible

无法在 ECS 优化的 AMI 中安装 Ansible

Ansible优化

Ansible性能调优

ansible系列8-SSH连接和执行性能优化