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

Ansible Playbook中Shell模块执行失败问题求助

解决Ansible Playbook执行Shell/Command模块报错问题

你遇到的核心问题是本地Ansible环境版本冲突:同时安装了旧版全量Ansible包(ansible==2.9.27)和新版ansible-core(ansible-core==2.15.8),两者的模块路径、依赖逻辑相互干扰,导致Playbook执行时错误调用了已被移除的旧模块,而非正常的Shell/Command模块。

1. 清理冲突的环境

先彻底卸载所有冲突的Ansible相关包,避免版本混杂:

pip uninstall -y ansible ansible-core

2. 选择单一版本方案安装

方案一:保留旧版全量Ansible(适合依赖旧模块场景)

仅安装ansible==2.9.27,确保环境纯净:

pip install ansible==2.9.27

验证安装:

ansible --version

此时你的原Playbook可以正常执行,因为2.9版本中ec2_facts和shell模块均为内置可用。

方案二:升级到新版ansible-core(推荐长期维护)

新版Ansible采用核心包+集合的架构,需要单独安装所需的AWS集合:

  1. 安装ansible-core:
pip install ansible-core==2.15.8
  1. 安装AWS官方集合(替代旧的ec2_facts模块):
ansible-galaxy collection install amazon.aws
  1. 修改Playbook,将旧的ec2_facts替换为新版模块:
---
- hosts: all
  become: yes
  gather_facts: yes
  tasks:
    - name: Gather EC2 facts
      amazon.aws.ec2_instance_info:
        filters:
          private-ip-address: "{{ ansible_default_ipv4.address }}"
- hosts: all
  become: yes
  tasks:
    - name: my task
      shell: uptime
  1. 验证环境:
ansible --version

确认输出中只有ansible-core版本,无旧版ansible包干扰。

3. 额外排查点

  • 检查项目目录或全局的ansible.cfg文件,确保module_path未指向旧版Ansible的模块目录,若有则清空该配置项。
  • 重新测试基础连接:ansible all -m ping,确认实例连接正常后再执行Playbook。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:52:16