用于更新/清理 dbatools 的 Ansible 任务失败

用于更新/清理 dbatools 的 Ansible 任务失败

我保留安装dbatools.io使用简单的 Ansible 剧本在我的 MSSQL 服务器上更新了 PowerShell 模块,其中包括以下任务:

#################### UPDATE DBATOOLS ####################
    - name: uppdate dbatools
      win_shell: |
        if ([Net.ServicePointManager]::SecurityProtocol -match "Tls12") {
            write-host "OK: tls 12 active"
            }
                else{
                [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

此任务确实有效,但每次它必须清理旧版本的 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}

我使用ignore_errors: yes它,所以它不会破坏我的结果,但最终我想可靠地解决这个问题。

如果我在任何主机上通过远程连接直接执行此命令,它不会出错。如果忽略该-Cleanup选项,它也不会出错,但旧版本的 dbatools 不会被删除,并且会随着时间的推移而累积。

有什么想法可以在 Ansible 上正确处理此错误吗?

详细错误消息:

redirecting (type: modules) ansible.builtin.win_shell to ansible.windows.win_shell
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
}

答案1

我现在已经将任务从 改为win_shellansible.windows.win_powershell此模块能够处理清理过程生成的警告:

"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."

基于此,我将执行Update-Dbatools不带-cleanup选项的命令。之后,我将添加第二个任务来清理旧版本的 dbatools。

为什么在手动执行更新/清理时不会出现这种情况对我来说是一个谜。

相关内容