通过使用
-vvvv
参数运行剧本,我可以了解幕后发生的情况,例如ansible-playbook myplaybook.yaml -vvvv
。我可以将各个模块导入到 Python 程序中,向它们传递参数,然后在 IDE 中单步执行。这很方便。
我真正想要的是使用 PyCharm 打开 Ansible playbook,并使用调试器逐行单步执行,以了解所有变量的状态。由于 Ansible 是用 Python 编写的,我认为应该有一种方法可以在 Python IDE 中运行 Ansible playbook。
有人知道该怎么做吗?
将 ansible 安装到您的 virtualenv 中,或者以其他方式进行安装,以便 PyCharm 可以在
ansible
中看到 bin
文件;遗憾的是,由于它们有 164 行 python 而不仅仅是 __main__
,因此您不能将其调用为 python -m ansible.cli.playbook
或任何此类细节
创建一个运行配置,其中“Python脚本”指向
.../bin/ansible
或.../bin/ansible-playbook
等
为了方便起见,您还可以在 PyCharm 中打开
bin/ansible
,导航到 if __name__
行,然后点击左侧装订线中的“播放三角形”,尽管这样做会导致 python 进程在 bin
中启动
目录,这可能有点烦人
在您选择的 ansible python 文件中设置断点
启动调试器
请注意,在很多情况下,ansible 会花很多钱来做事情,据我所知,PyCharm 的调试器不会遵循子进程或其朋友,因此如果出现以下情况,您将不得不跳过一些麻烦您希望能够调试这些风格的模块,包括您自己的任何脚本 library/*.py
或其他。
strategy: debug
并与
ansible调试器
DEBUG INFO:
/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/kubespray-defaults/tasks/main.yaml:2
2|- name: Configure defaults
3| debug:
4| msg: "Check roles/kubespray-defaults/defaults/main.yml"
5| tags:
6| - always
7|
8|# do not run gather facts when bootstrap-os in roles
9|- name: set fallback_ips
10| import_tasks: fallback_ips.yml
11| when:
Saturday 25 May 2024 23:07:13 +0800 (0:00:00.101) 10:20:04.700 *********
TASK [kubespray-defaults : Configure defaults] *****************************************************************************************************************************************************************
ok: [test1] => {
"msg": "Check roles/kubespray-defaults/defaults/main.yml"
}
Aiansible(CN) => result._result
{'msg': 'Check roles/kubespray-defaults/defaults/main.yml', '_ansible_verbose_always': True, '_ansible_no_log': False, 'changed': False}
Aiansible(CN) => bt
0:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/playbooks/ansible_version.yml:11=>Check 2.11.0 <= Ansible version < 2.13.0
1:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/playbooks/ansible_version.yml:20=>Check that python netaddr is installed
2:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/playbooks/ansible_version.yml:28=>Check that jinja is not too old (install via pip)
3:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:2=>download : prep_download | Set a few facts
4:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:8=>download : prep_download | On localhost, check if passwordless root is possible
5:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:23=>download : prep_download | On localhost, check if user has access to the container runtime without using sudo
6:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:38=>download : prep_download | Parse the outputs of the previous commands
7:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:48=>download : prep_download | Check that local user is in group or can become root
8:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:59=>download : prep_download | Register docker images info
9:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:68=>download : prep_download | Create staging directory on remote node
10:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/prep_download.yml:78=>download : prep_download | Create local cache for files and images on control node
11:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/main.yml:10=>download : download | Get kubeadm binary and list of required images
12:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/download/tasks/main.yml:19=>download : download | Download files / images
13:/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/roles/kubespray-defaults/tasks/main.yaml:2=>kubespray-defaults : Configure defaults
Aiansible(CN) => a
msg: Check roles/kubespray-defaults/defaults/main.yml