I started to learn Ansible and Jenkins a few days ago. I'm stuck on one particular problem and despite many efforts can't figure it out. I have a very basic playbook that runs OK if I run it directly from the command line.
Ansible and Jenkins are installed on the same Ubutu virtual machine.
I'd like to run it via Jenkins and I manage to do so but the workflow terminates in error. This is the console output:
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
I don't understand why playbook runs perfectly on Ansible but when invokved by Jenkins, it fails.
As per @john-mahowald's command, running this command:
ansible -m python_requirements_info -a dependencies=winrm localhost
Returns:
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": {}
}
Not sure if this helps but looks like pywinrm is intalled only for 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)