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

如何在AltLinux v10.2上通过Ansible apt_rpm模块指定包版本

问题:Ansible在AltLinux v10.2指定版本安装python3-module-yaml失败

问题详情

目标主机为AltLinux Server v10.2,使用Ansible package 模块(实际调用apt_rpm模块)安装指定版本python3-module-yaml=6.0.1时执行失败,报错信息如下:

fatal: [test]: FAILED! => changed=false 
  msg: '''apt-get -y install python3-module-yaml=6.0.1'' failed: '

原playbook示例:

---
- hosts: all
  become: true

tasks:
- name: Install my package
  package:
    name: python3-module-yaml=6.0.1
    state: present
    update_cache: true

但直接在目标主机执行apt-get install python3-module-yaml=6.0.1可成功完成安装,查阅community.general.apt_rpm模块官方文档未找到相关版本指定示例,寻求解决方法或替代方案。


解决方法

方案1:显式调用community.general.apt_rpm模块

避免package模块的自动适配逻辑可能带来的格式问题,直接使用apt_rpm模块指定版本:

---
- hosts: all
  become: true
  tasks:
    - name: Install specified python3-module-yaml version
      community.general.apt_rpm:
        name: python3-module-yaml=6.0.1
        state: present
        update_cache: yes

方案2:直接执行apt-get原生命令(兜底方案)

利用已验证可行的系统命令,通过command模块执行,同时模拟模块的状态判断逻辑:

---
- hosts: all
  become: true
  tasks:
    - name: Install package via native apt-get
      command: apt-get -y install python3-module-yaml=6.0.1
      args:
        warn: false
      register: install_output
      changed_when: "'Setting up python3-module-yaml' in install_output.stdout"
      failed_when: install_output.rc != 0
  • warn: false 关闭Ansible对直接调用包管理命令的警告
  • changed_when 用于判断操作是否产生了变更(对应模块的changed状态)
  • failed_when 根据命令返回码判断是否执行失败

方案3:升级community.general集合

旧版本的community.general集合可能对AltLinux的apt-rpm版本格式支持不足,先升级集合再重试:

ansible-galaxy collection install community.general --upgrade

内容的提问来源于stack exchange,提问作者Никита Буднов

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 22:48:25