Score:0

Ansible and Jenkins: "winrm is not installed: cannot import name utils

gd flag

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)
Score:1
cn flag

As the error suggests, install Python modules winrm or requests, to the Python that Ansible uses on the controller.

To troubleshoot further, this command will attempt to find winrm module, and also print out the Python path. Technically, it uses the module python which could be different than the plugin python, but it is a starting point.

ansible -m python_requirements_info -a dependencies=winrm localhost

The inner failed message comes from a winrm connection module, used to manage Windows hosts. If you did not intend to use this, check ansible_connection variable and everything else having to do with connection key words and inventory. Possible that you previously set up Windows connections at a play or all hosts level, but now you have what appears to be a Linux host.

Okrx avatar
gd flag
Thanks for your comment. I run the command and included the output in the post. If I run Windows playbooks directly from the Ansble console they all work fine. Can it be possible that Jenkins can't access Pywinrm module?
John Mahowald avatar
cn flag
Confirm which Python your ansible-playbook and its plugins are using `/usr/bin/ansible-playbook --version` will output that. You appear to have a python2 for your ansible package, and a pip3 environment with python3. Which can work, but you need to be intentional about what python you install things to. Also, you haven't directly answered the question of if you intend to remote into Windows hosts.
Okrx avatar
gd flag
So.. following what you said in your comment and to make my life easier I decided it'd be easier to have only 1 version of python, not 2 of them. As Ansible was already using python2 I removed python3 and pip3 and made sure pywinrm is installed via pip2. And it worked! Thank you very much!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.