You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ansible win_package卸载Chrome返回rc=19但卸载成功的问题求助

卸载Google Chrome时Ansible win_package模块返回rc=19但软件已成功卸载的问题

在卸载Google Chrome时,执行Ansible的win_package任务(名称为"Uninstall program with route")返回rc=19,尽管软件已经成功卸载,但任务仍报错。找不到该返回码的相关说明,求解答原因。

环境信息

  • Ansible版本:2.9.27
  • Python版本:2.7.5
  • Ansible配置文件路径:/etc/ansible/ansible.cfg
  • 模块搜索路径:[u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  • Ansible Python模块路径:/usr/lib/python2.7/site-packages/ansible
  • 可执行文件路径:/usr/bin/ansible
  • 版本限制:必须为win_package指定.exe路径,否则模块执行失败

复现情况

手动通过PowerShell执行卸载命令时,同样会得到rc=19的返回码,命令如下:

$process = Start-Process -FilePath $exe -ArgumentList $params -Wait -PassThru
$process.WaitForExit() 
$exitCode = $process.ExitCode

Playbook内容

---
- hosts: all
  gather_facts: false
  vars:
    aplication_name: "Google Chrome"
  tasks:
    - name: Bloque de desinstalacion
      block:
        - name: obtain id and uninstall string by aplication name
          ansible.windows.win_powershell:
            script: |
              $package_info = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* , HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
              Where-Object { $_.DisplayName -like "*{{aplication_name}}*" } | ForEach-Object { "$($_.PSChildName)\n$($_.UninstallString)" }
              
              Write-Output $package_info
          register: package_info

        - name: extract product_id y path
          set_fact:
            product_id: "{{ package_info.output[0].split('\\n')[0] }}"
            uninstall_path: "{{ package_info.output[0].split('\\n')[1].split('--')[0] | regex_replace('^\\s+|\\s+$', '') | regex_replace('^\"|\"$', '')  }}"

        - name: Uninstall program with route
          win_package:
            path: '{{uninstall_path}}'
            product_id: "{{product_id}}"
            arguments: '--uninstall --multi-install --system-level --force-uninstall'
            state: absent
          register: status
          ignore_errors: true
        
        - name: "debug uninstall_path"
          debug:
            msg: '{{ uninstall_path }}'
          when: primer_intento_desintalacion.failed

问题原因及解决建议

rc=19是Google Chrome卸载程序本身返回的退出码,并非Ansible模块的错误码。这个退出码通常表示卸载操作已完成,但存在一些非致命的清理残留或警告情况,比如部分临时文件未完全删除、某些关联组件已提前卸载等,但核心的Chrome程序已经被成功移除。

针对Ansible任务报错的问题,旧版本的win_package模块(2.9.x)会严格校验卸载程序的退出码,只要不是标准的成功码(如0)就会判定任务失败。可以通过以下方式处理:

  1. 忽略特定退出码:在win_package任务中添加failed_when条件,仅当退出码不是0或19时才标记任务失败:
    - name: Uninstall program with route
      win_package:
        path: '{{uninstall_path}}'
        product_id: "{{product_id}}"
        arguments: '--uninstall --multi-install --system-level --force-uninstall'
        state: absent
      register: status
      failed_when: status.rc not in [0, 19]
    
  2. 验证卸载结果:在任务后添加检查步骤,确认Chrome是否真的被卸载(比如检查注册表项是否存在、程序目录是否删除),以此作为任务成功的判定依据,而非依赖退出码。

另外,注意到playbook中最后一个debug任务的when条件引用了未定义的变量primer_intento_desintalacion,这会导致额外错误,建议修正为status.failed。

内容的提问来源于stack exchange,提问作者lauliett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 03:46:14