如何加快ansible?

how to speed up ansible?

我们最近实施了ansible。不知何故,我们发现它真的很慢,想知道如何加快速度。 所以,我做了一些 stracing,发现 /etc/resolv.conf 和 /etc/hosts 相关,然后似乎是反向 DNS 问题如下:

18:32:13.961094 IP 192.168.22.2.44206 > 192.168.23.2.53: 12243+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:18.966199 IP 192.168.22.2.44206 > 192.168.23.2.53: 12243+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:18.967019 IP 192.168.23.2.53 > 192.168.22.2.44206: 12243 NXDomain* 0/0/0 (45)
18:32:18.967986 IP 192.168.22.2.44057 > 192.168.23.2.53: 19913+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:23.973096 IP 192.168.22.2.44057 > 192.168.23.2.53: 19913+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:23.976649 IP 192.168.23.2.53 > 192.168.22.2.44057: 19913 NXDomain* 0/0/0 (45)

18:32:23.977230 IP 192.168.22.2.44997 > 192.168.23.2.53: 8795+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:28.982280 IP 192.168.22.2.44997 > 192.168.23.2.53: 8795+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:28.982980 IP 192.168.23.2.53 > 192.168.22.2.44997: 8795 NXDomain* 0/0/0 (45)
18:32:28.983550 IP 192.168.22.2.38603 > 192.168.23.2.53: 41907+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:33.988665 IP 192.168.22.2.38603 > 192.168.23.2.53: 41907+ PTR? xxx.xxx.29.115.in-addr.arpa. (45)
18:32:33.989414 IP 192.168.23.2.53 > 192.168.22.2.38603: 41907 NXDomain* 0/0/0 (45)

如你所见,做PTR要20s?按节点查询。(真实节点IP替换为xxx.xxx)。 那么,为什么 ansible 必须进行反向 DNS 查询?我们可以简单地禁用它吗?怎么样?

顺便说一句,仅供参考,我们在 ansible.cfg 内更改了默认值 "ControlPersist=30s" 更长的时间,此后确实有很大帮助。但是发起连接时的反向DNS查询延迟也是绝对不能接受的。

感谢@leucos 和@CalleDybedahl,这个问题终于引出了ssh gssapi 认证方式。在ansible.cfg内追加"GSSAPIAuthentication=no"到ssh_args后,就没有PTR查询了。 仅供参考,我们也在 ~/.ssh/config 中创建它,因为我们的环境中根本没有实施 GSSAPI 身份验证。

在尝试加速 Ansible 时,这是我的观察结果。

SSH 尝试身份验证的顺序很重要。一般在 Ansible 中,我们使用 Public Key Authentication。 因此在 /etc/ansible/ansible.cfg 中添加 PreferredAuthentications 条目后,速度提高了将近 17 倍。

ssh_args = -o ControlMaster=auto -o ControlPersist=60m -o PreferredAuthentications=publickey

结果如下。

[admin@SON-DOCM ~]$ time ansible -m ping 2405:200:xxx:xxxx::6:40
2405:200:xxx:xxxx::6:40 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

real    0m31.897s
user    0m1.307s
sys     0m0.248s
[admin@SON-DOCM ~]$ vi /etc/ansible/ansible.cfg
[admin@SON-DOCM ~]$ time ansible -m ping 2405:200:xxx:xxxx::6:40
2405:200:xxx:xxxx::6:40 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

real    0m1.882s
user    0m0.757s
sys     0m0.172s