如何调试慢速的 `ansible`(和 `ansible-playbook`)?

How do I debug slow `ansible` (and `ansible-plabook`)?

我在管理具有特定角色的特定远程主机时速度非常慢。

我追踪了缓慢的根源,发现对于一个非常具体的变量(为该特定主机定义并由该特定角色使用),ansibleansible-playbook 占用了构建它需要 30 秒,而对于其他变量只需要几秒钟。

我已经激活了详细模式并检查了日志文件,但除了 ansible 对于有问题的变量在很长一段时间内完全无声(没有添加日志)之外没有任何异常。

我的问题:

这是打印有问题的变量 iptables 和另一个正常变量 ifcfg 的结果:

time ansible fw1 -m debug -a "msg={{iptables}}" -vvvvvv
...
real    0m25.097s
user    0m26.564s
sys 0m0.272s

time ansible fw1 -m debug -a "msg={{ifcfg}}"
...
real    0m2.584s
user    0m2.636s
sys 0m0.097s

这是第一个命令的 ansible 日志:

2021-06-04 09:53:18,239 p=22258 u=yo n=ansible | ansible 2.9.21
  config file = /path/to/ansible.cfg
  configured module search path = [u'/path/to/.libs']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0]
2021-06-04 09:53:18,239 p=22258 u=yo n=ansible | Using /path/to/ansible.cfg as config file
2021-06-04 09:53:18,239 p=22258 u=yo n=ansible | Reading vault password file: /path/to/ansible_vault_passwords
.
.
.
2021-06-04 09:53:18,472 p=22258 u=yo n=ansible | Decrypt of "/path/to/vault.yml" successful with secret=FileVaultSecret(filename='/path/to/ansible_vault_passwords') and vault_id=default
2021-06-04 09:53:44,250 p=22258 u=yo n=ansible | fw1 | SUCCESS => {
    "msg": {
        "allow": {
            "ping": true, 
            "ssl": true
        }, 
        "forward": {
            "enabled": true, 
            "http": {
                "khlass_prd_bc_to_khlass_prd_app": {
                    "enabled": false, 
                    "source": "11.5.40.0/24", 
                    "target": "11.5.20.0/24"
                }, 
                "khlass_prd_dmz_to_khlass_prd_app": {
                    "enabled": false, 
                    "source": "11.5.10.0/24", 
                    "target": "11.5.20.0/24"
                }
            }, 
            "https": {
                "khlass_prd_bc_to_khlass_prd_app": {
                    "enabled": true, 
                    "source": "11.5.40.0/24", 
                    "target": "11.5.20.0/24"
                }, 
                "khlass_prd_dmz_to_khlass_prd_app": {
                    "enabled": true, 
                    "source": "11.5.10.0/24", 
                    "target": "11.5.20.0/24"
                }
            }, 
            "mariadb": {
                "khlass_prd_app_to_khlass_prd_db": {
                    "enabled": true, 
                    "source": "11.5.20.0/24", 
                    "target": "11.5.30.0/24"
                }
            }, 
            "service": {
                "khlass_prd_app_to_bc_api": {
                    "enabled": true, 
                    "port": "8447", 
                    "source": "11.5.20.0/24", 
                    "target": "11.5.40.0/24"
                }
            }
        }, 
        "log": {
            "filtered": true
        }, 
        "remove_firewalld": true
    }
}
2021-06-04 09:53:44,250 p=22258 u=yo n=ansible | META: ran handlers
2021-06-04 09:53:44,250 p=22258 u=yo n=ansible | META: ran handlers

@Zeitounator的帮助下,我已经解决了这个问题。

iptables 变量是一个多层次的字典,我还有两个变量 netsifcfg 也是多层次的字典。

iptables 的许多值是使用 ifcfg 中的值设置的,这些值是使用 nets.

中的值设置的

我已经设法直接使用 nets 中的值在 iptables 中设置值,现在 iptables 的评估与其他变量一样快。