几天前我开始学习 Ansible 和 Jenkins。我被一个特定的问题难住了,尽管付出了很多努力,但还是无法解决。我有一个非常基本的剧本,如果我直接从命令行运行它,它就可以正常运行。
Ansible 和 Jenkins 安装在同一个 Ubutu 虚拟机上。
我想通过 Jenkins 运行它,并且我成功了,但工作流程因错误而终止。这是控制台输出:
Started by user Jenkins Operador
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/Ansible-demo
[Ansible-demo] $ /usr/bin/ansible-playbook /home/operador/Ansible/playbooks/ping.yaml -f 5
PLAY [My first play] ***********************************************************
TASK [Gathering Facts] *********************************************************
fatal: [ubuntu-srv]: FAILED! => {"msg": "winrm or requests is not installed: cannot import name utils"}
PLAY RECAP *********************************************************************
ubuntu-srv : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
FATAL: command execution failed
hudson.AbortException: Ansible playbook execution failed
at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:262)
at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:232)
at jenkins.tasks.SimpleBuildStep.perform(SimpleBuildStep.java:123)
at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:80)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:816)
at hudson.model.Build$BuildExecution.build(Build.java:199)
at hudson.model.Build$BuildExecution.doRun(Build.java:164)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:524)
at hudson.model.Run.execute(Run.java:1899)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44)
at hudson.model.ResourceController.execute(ResourceController.java:107)
at hudson.model.Executor.run(Executor.java:449)
ERROR: Ansible playbook execution failed
Finished: FAILURE
我不明白为什么 playbook 在 Ansible 上完美运行,但当 Jenkins 调用时却失败了。
按照@john-mahowald的命令,运行此命令:
ansible -m python_requirements_info -a dependencies=winrm localhost
返回:
localhost | SUCCESS => {
"changed": false,
"mismatched": {},
"not_found": [
"winrm"
],
"python": "/usr/bin/python2",
"python_system_path": [
"/tmp/ansible_python_requirements_info_payload_PcEN_E/ansible_python_requirements_info_payload.zip",
"/usr/lib/python2.7",
"/usr/lib/python2.7/plat-x86_64-linux-gnu",
"/usr/lib/python2.7/lib-tk",
"/usr/lib/python2.7/lib-old",
"/usr/lib/python2.7/lib-dynload",
"/usr/local/lib/python2.7/dist-packages",
"/usr/lib/python2.7/dist-packages"
],
"python_version": "2.7.18 (default, Jul 1 2022, 12:27:04) \n[GCC 9.4.0]",
"valid": {}
}
不确定这是否有帮助,但看起来 pywinrm 仅适用于 Python3:
jenkins@ubuntu:/home/operador/Ansible/playbooks$ pip install pywinrm
Requirement already satisfied: pywinrm in /usr/local/lib/python3.8/dist-packages (0.4.3)
Requirement already satisfied: six in /usr/local/lib/python3.8/dist-packages (from pywinrm) (1.16.0)
Requirement already satisfied: requests-ntlm>=1.1.0 in /usr/local/lib/python3.8/dist-packages (from pywinrm) (1.1.0)
Requirement already satisfied: xmltodict in /usr/local/lib/python3.8/dist-packages (from pywinrm) (0.13.0)
Requirement already satisfied: requests>=2.9.1 in /usr/local/lib/python3.8/dist-packages (from pywinrm) (2.28.1)
Requirement already satisfied: ntlm-auth>=1.0.2 in /usr/local/lib/python3.8/dist-packages (from requests-ntlm>=1.1.0->pywinrm) (1.5.0)
Requirement already satisfied: cryptography>=1.3 in /usr/local/lib/python3.8/dist-packages (from requests-ntlm>=1.1.0->pywinrm) (38.0.3)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests>=2.9.1->pywinrm) (3.4)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.8/dist-packages (from requests>=2.9.1->pywinrm) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests>=2.9.1->pywinrm) (2022.9.24)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests>=2.9.1->pywinrm) (1.26.12)
Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.8/dist-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->pywinrm) (1.15.1)
Requirement already satisfied: pycparser in /usr/local/lib/python3.8/dist-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->pywinrm) (2.21)
答案1
根据错误提示,将 Python 模块 winrm 或请求安装到 Ansible 在控制器上使用的 Python 中。
为了进一步排除故障,此命令将尝试查找 winrm 模块,并打印出 Python 路径。从技术上讲,它使用模块 python,这可能与插件 python 不同,但这是一个起点。
ansible -m python_requirements_info -a dependencies=winrm localhost
内部失败消息来自 winrm 连接模块,用于管理Windows 主机。如果您不打算使用此功能,请检查ansible_connection 变量 以及与连接关键字和清单相关的所有其他内容。可能您之前在游戏或所有主机级别设置了 Windows 连接,但现在您拥有的似乎是 Linux 主机。