'Is it possible to configure Azure Windows VMs using Ansible on Azure DevOps Microsoft Hosted Ubuntu agents?

We try to configure an Azure VM using an Azure DevOps pipeline. We first create the machine using Terraform and then we need to configure it. Right now the pipeline is functional when we use a customized Ubuntu Azure DevOps agent (a VM we setup ourselves in Azure).

We prefer to use a Microsoft Hosted Ubuntu Agent. When we try to run our pipeline using the Microsoft Hosted Ubuntu agent we fail with a message "winrm or requests is not installed".

We have done a lot of research and attempts to install the needed components, but none have been fruitful.

All the examples and documentation on the internet we can find don't mention our specific use case. Ansible configuration of Windows VMs in Azure from a Microsoft Hosted Ubuntu agent. Isn't it possible for some reason?

If it is, any pointers in the right direction will be much appreciated!

The error we see in the Azure DevOps pipeline is this:


ansible-playbook -vvvv -i inventory/hosts.cfg main.yml --extra-vars '{"customer_name": "<REMOVED>" }'
ansible-playbook [core 2.12.5]
  config file = None
  configured module search path = ['/home/vsts/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vsts/.local/lib/python3.8/site-packages/ansible
  ansible collection location = /home/vsts/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/vsts/.local/bin/ansible-playbook
  python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
  jinja version = 2.10.1
  libyaml = True
No config file found; using defaults
setting up inventory plugins
host_list declined parsing /home/vsts/work/1/s/ansible/inventory/hosts.cfg as it did not pass its verify_file() method
auto declined parsing /home/vsts/work/1/s/ansible/inventory/hosts.cfg as it did not pass its verify_file() method
yaml declined parsing /home/vsts/work/1/s/ansible/inventory/hosts.cfg as it did not pass its verify_file() method
Parsed /home/vsts/work/1/s/ansible/inventory/hosts.cfg inventory source with ini plugin
Loading collection ansible.windows from /home/vsts/.local/lib/python3.8/site-packages/ansible_collections/ansible/windows
Loading collection community.windows from /home/vsts/.local/lib/python3.8/site-packages/ansible_collections/community/windows
redirecting (type: modules) ansible.builtin.win_service to ansible.windows.win_service
redirecting (type: modules) ansible.builtin.win_service to ansible.windows.win_service
redirecting (type: modules) ansible.builtin.win_service to ansible.windows.win_service
redirecting (type: modules) ansible.builtin.win_service to ansible.windows.win_service
redirecting (type: modules) ansible.builtin.win_service to ansible.windows.win_service
Loading callback plugin default of type stdout, v2.0 from /home/vsts/.local/lib/python3.8/site-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: main.yml *************************************************************
Positional arguments: main.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/home/vsts/work/1/s/ansible/inventory/hosts.cfg',)
extra_vars: ('{"customer_name": "<REMOVED>"}',)
forks: 5
1 plays in main.yml

PLAY [windows:pro] *********************************************************

TASK [Gathering Facts] *********************************************************
task path: /home/vsts/work/1/s/ansible/main.yml:1
redirecting (type: modules) ansible.builtin.setup to ansible.windows.setup
Using module file /home/vsts/.local/lib/python3.8/site-packages/ansible_collections/ansible/windows/plugins/modules/setup.ps1
Pipelining is enabled.
**fatal: [51.144.125.149]: FAILED! => {
    "msg": "winrm or requests is not installed: No module named 'winrm'"
}**

PLAY RECAP *********************************************************************
51.144.125.149             : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

We tried to fix the problem by installing various potentially relevant components in the pipeline just before running the ansible-playbook command, for instance this one

pip3 install pywinrm

Later, based on input on this SO question we tried this in the pipeline:

python3 -m pip install --ignore-installed pywinrm
find / -name winrm.py
ansible-playbook -vvv -i inventory/hosts.cfg main.yml 

The find command finds winrm.py here:

/opt/pipx/venvs/ansible-core/lib/python3.8/site-packages/ansible/plugins/connection/winrm.py

The ansible-playbook configuration we are using is:

    ansible-playbook [core 2.12.5]
    config file = None
    configured module search path = 
    ['/home/vsts/.ansible/plugins/modules', 
    '/usr/share/ansible/plugins/modules']
    ansible python module location = /opt/pipx/venvs/ansible- 
    core/lib/python3.8/site-packages/ansible
    ansible collection location = 
    /home/vsts/.ansible/collections:/usr/share/ansible/collections
    executable location = /opt/pipx_bin/ansible-playbook
    python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 
    9.4.0]
    jinja version = 3.1.2
    libyaml = True
    No config file found; using defaults

The error we get is:

    task path: /home/vsts/work/1/s/ansible/main.yml:1
    redirecting (type: modules) ansible.builtin.setup to 
    ansible.windows.setup
    Using module file /opt/pipx/venvs/ansible- 
    core/lib/python3.8/site- 
    packages/ansible_collections/ansible/windows/plugins/modules/
    setup.ps1
    Pipelining is enabled.
    fatal: [13.73.148.141]: FAILED! => {
        "msg": "winrm or requests is not installed: No module named 
    'winrm'"
    }


Solution 1:[1]

you can try solution in RedHat knowledgebase

https://access.redhat.com/solutions/3356681

Last comment suggestion (replace yum with apt commands)

I was getting this error even if python2-winrm version 0.3.0 is already installed via yum

yum list installed | grep winrm python2-winrm.noarch
0.3.0-1.el7 @epel

pip install "pywinrm>=0.2.2" only resulted in "Requirement already satisfied"

I ran this to resolve the error -

  1. yum autoremove python2-winrm.noarch
    
  2. pip install "pywinrm>=0.2.2"

Then ping: pong worked just fine over https, port=5986

ram@thinkred1cartoon$ ansible all -i hosts.txt -m win_ping 172.16.96.135 | SUCCESS => { "changed": false, "ping": "pong" }

conversely, if you don't want to run command 1, then command 2 won't work for you. In that case, run command 3

3 ) pip install --ignore-installed "pywinrm>=0.2.2"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1