Ansible task for updating/cleaning up dbatools failing

us flag

I keep the Installation of the PowerShell module updated on my MSSQL Servers with a simple Ansible playbook which includes the following task:

#################### UPDATE DBATOOLS ####################
    - name: uppdate dbatools
      win_shell: |
        if ([Net.ServicePointManager]::SecurityProtocol -match "Tls12") {
            write-host "OK: tls 12 active"
                [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
                write-host "SET: tls 12 was activated"

        if ((Get-PSRepository).name -notLike "PSGallery") {
            write-host "SET: PSGallery registering"
            Register-PSRepository -Default
                else {
                write-host "OK: PSGallery existing"

        Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
        write-host "SET: updating dbatools"
        Update-Dbatools -Cleanup -Confirm:$false
      # Same as on install except for install command at the end
      when: is_installed.stdout.find('true') != -1 ## used "find", because stdout contains /r/n as it is a list / find easier then cleaning var

This Task does work but it returns the following error, every time it has to cleanup an old version of dbatools:

fatal: [server.domain.local]: FAILED! => {"changed": false, "module_stderr": "#< CLIXML\r\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 4294967295}

I use ignore_errors: yes on it so it won't spoil my results, but ultimately I'd like to solve this issue reliably.

If I execute this command directly via remote connection on any host, it works without errors. When leaving the -Cleanup option away it also works without errors but old versions of dbatools will not been deleted and it would add up over time.

Any ideas how this error can correctly be handled on Ansible?

Error Message with high verbosity:

redirecting (type: modules) ansible.builtin.win_shell to
Using module file /runner/requirements_collections/ansible_collections/ansible/windows/plugins/modules/win_shell.ps1
Pipelining is enabled.
<SQL-Server-IP> ESTABLISH WINRM CONNECTION FOR USER: ansible-service-user on PORT 5986 TO SQL-Server-IP
EXEC (via pipeline wrapper)
fatal: [SQL-Server-FQDN]: FAILED! => {
    "changed": false,
    "module_stderr": "#< CLIXML\r\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 4294967295
Henrik Pingel avatar
in flag
Execute ansible-playbook with `-vvv` to get verbose output. It should contain the exact error.
Manu avatar
us flag
@HenrikPingel we use AWX and I've set verbosity on the highest setting (4, Connection Debug). Have added the output into my post, but I guess this won't bring us forward.
us flag

I have now changed the tasks from win_shell to This module is able to handle the warning generated by the cleanup process:

"warning": [
    "The version '1.1.97' of module 'dbatools' is currently in use. Retry the operation after closing the applications.",
    "Unable to remove dbatools version [1.1.97] due to: \n\tSystem.Exception: Module 'dbatools' is in currently in use or you don't have the required permissions."

Based on this I will perform the Update-Dbatools command without the -cleanup option. Afterward I will add a second task for cleaning up old dbatools versions.

Why this wouldn't appear when performing the update/cleanup manually is a mystery to me.


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.