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

Ansible apt模块前序任务update_cache为何对后续安装任务无效

问题根因

  • Ansible的apt模块对update_cache: yes的执行做了场景优化:当任务的state设为absent(即卸载软件)时,由于卸载操作不需要依赖最新的软件源缓存,模块会直接跳过apt update的执行,哪怕你显式配置了update_cache: yes也不会生效。
  • 这就是你前序卸载任务配置的缓存更新没有实际运行,导致后续安装任务找不到软件包的原因。

修复方案

你可以选择两种常用的修复方式:

  1. 单独加一个前置的更新缓存任务,放在所有apt操作之前:
- name: update apt cache
  apt:
    update_cache: yes
    cache_valid_time: 3600 # 缓存1小时内有效,避免重复更新
  1. 给需要最新缓存的安装类任务单独加上update_cache: yes,而不是依赖卸载类任务的配置:
- name: misc packages
  apt:
    name:
      - qperf
      - fio
    state: present
    update_cache: yes

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 03:06:03