修复 Ansible Playbook 中 “no test named ‘success'” 错误

53次阅读
没有评论

问题描述

在使用 Ansible Playbook 配置 Windows Subsystem for Linux (WSL) 时,重新安装操作系统后需要再次运行 Playbook。然而,他遇到了以下错误信息:

The conditional check 'aptitude_installed is success' failed. The error was: no test named 'success'  line 1The error appears to have been in '/mnt/c/source/richardslater/workstation-setup/wsl/plays/wsl.yml': line 15, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.The offending line appears to be:  - name: ensure aptitude is installed    ^ here

该错误信息出现在 Playbook 中的一个任务中,该任务的目标是检查是否安装了 aptitude 包,如果已安装则跳过,否则安装 aptitude 包。

用户需要解决这个错误并继续成功运行 Playbook,同时他想知道在 Playbook 中应该如何正确地检查和安装软件包。

解决方案

请注意以下操作可能因版本差异而有所不同,请谨慎执行并在操作前备份重要数据。

步骤1:修复条件检查问题

错误信息中提到了 aptitude_installed is success 的条件检查失败,并且不存在名为 ‘success’ 的测试。这是错误的原因之一。在 Ansible 2.2 版本中,测试名应为 “succeeded” 或 “success”。我们需要修复这个条件检查。

  1. 打开 Playbook 文件,找到包含条件检查的任务,它应该类似于以下代码:
- name: check if aptitude is installed
  shell: dpkg-query -W -f='${Status}' aptitude | grep 'install ok installed'
  register: aptitude_installed
  failed_when: no
  changed_when: no
- name: ensure aptitude is installed
  command: apt-get -y install aptitude warn=False
  when: aptitude_installed is success
  1. 修改条件检查的语句,将 is success 改为 is succeededis success,这取决于你的 Ansible 版本。
when: aptitude_installed is succeeded  # 或 when: aptitude_installed is success

步骤2:改进任务的实现方式

在上述修复后,还有更好的方法来实现检查和安装软件包的任务。这里我们可以使用 Ansible 提供的 package 模块来处理软件包的安装。

  1. 替换原来的任务,使用 package 模块来安装 aptitude 软件包。
- name: Ensure aptitude is installed
  package:
    name: aptitude
    state: present

通过使用 package 模块,我们不需要手动检查软件包是否已安装,Ansible 会自动处理 idempotency(幂等性),确保软件包只会被安装一次。

注:如果你的 Playbook 主要针对 Ubuntu,还可以使用更专门的 apt 模块,具体取决于你的需求和目标操作系统。

正文完