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

使用Ansible在RHEL8卸载Docker失败:无法下载包求助

在RHEL 8上用Ansible卸载Docker时遇镜像下载失败错误,但手动执行yum remove正常

问题描述

在RHEL 8主机上通过Ansible卸载Docker及相关组件时,触发如下错误:

fatal: [server1]: FAILED! => {"changed": false, "msg": "Failed to download packages: containers-common-1:1.2.2-10.module+el8.4.0+11311+9da8acfb.x86_64: Cannot download, all mirrors were already tried without success", "results": []}

但使用Ansible用户SSH登录目标主机,手动执行yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine podman runc可成功完成卸载。

我的Ansible Playbook代码:

---
- name: Remove PodMan
  gather_facts: true
  hosts: all
  become: yes
  tasks:
  - name: uninstall podman
    package:
      name:
        - docker 
        - docker-client 
        - docker-client-latest 
        - docker-common 
        - docker-latest 
        - docker-latest-logrotate 
        - docker-logrotate 
        - docker-engine 
        - podman 
        - runc
      state: absent

尝试替换为yum模块后,错误完全一致。我的Ansible版本信息:

ansible --version
ansible [core 2.14.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Jun  6 2023, 07:39:01) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

解决方案

1. 跳过损坏依赖链(谨慎使用)

Ansible的yum/package模块默认会处理依赖关系,卸载时可能尝试下载依赖包(如containers-common),但镜像源不可用导致失败。可添加skip_broken: yes参数跳过无法处理的依赖:

- name: uninstall target packages
  yum:
    name:
      - docker 
      - docker-client 
      - docker-client-latest 
      - docker-common 
      - docker-latest 
      - docker-latest-logrotate 
      - docker-logrotate 
      - docker-engine 
      - podman 
      - runc
    state: absent
    skip_broken: yes

2. 直接调用yum命令执行卸载

既然手动执行有效,可通过command模块模拟手动操作,绕开Ansible模块的依赖处理逻辑:

- name: uninstall packages via yum command
  command: yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine podman runc
  args:
    warn: false  # 关闭Ansible对使用command替代yum模块的警告

3. 先清理yum缓存并刷新源

在卸载前清理缓存、刷新镜像源,确保目标主机yum状态正常:

- name: clean yum cache and refresh repos
  yum:
    update_cache: yes
  ignore_errors: yes  # 刷新失败不中断后续任务

- name: uninstall target packages
  yum:
    name:
      - docker 
      - docker-client 
      - docker-client-latest 
      - docker-common 
      - docker-latest 
      - docker-latest-logrotate 
      - docker-logrotate 
      - docker-engine 
      - podman 
      - runc
    state: absent

4. 检查执行环境差异

手动SSH与Ansible执行的环境变量(如代理、yum配置路径)可能不同,可先输出环境变量对比:

- name: print yum related env vars
  command: echo $HTTP_PROXY $HTTPS_PROXY $YUM_CONFIG
  register: env_output

- name: show env output
  debug:
    var: env_output.stdout

若发现代理或配置不一致,可在任务中指定环境变量:

- name: uninstall packages with correct env
  yum:
    name: [...]
    state: absent
  environment:
    HTTP_PROXY: "http://your-proxy:port"
    HTTPS_PROXY: "http://your-proxy:port"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 01:55:25