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

Ansible如何仅在指定版本未安装时装包 解决首次运行stdout缺失报错

问题原因

首次运行时mono未安装,mono --version命令执行失败,注册的current_mono变量不会生成stdout字段,直接引用就会触发变量不存在的报错。

修复方案

你可以通过Ansible的default过滤器给字段设置默认空值兼容异常场景,也可以结合返回码判断让逻辑更严谨:

- name: Check for existing mono installation
  command: "mono --version"
  register: current_mono
  ignore_errors: true
  changed_when: false # 优化项:检查类任务不会被标记为系统变更

- name: Running "make install" for Mono
  command: make install
  args: 
    chdir: "{{ mono_install_dir }}"
  become: yes
  # 两种判断逻辑二选一即可:
  # 方案1:最简适配,stdout不存在时默认取空字符串
  when: "mono_version|string not in current_mono.stdout | default('')"
  # 方案2:更严谨的逻辑,命令执行失败/版本不匹配都触发安装
  # when: current_mono.rc != 0 or (mono_version|string not in current_mono.stdout)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 16:36:09